[mercury-users] full and empty lists

Dave Slutzkin dave_slutzkin at yahoo.com
Mon Jan 14 23:00:41 AEDT 2002


 --- David Overton <dmo at cs.mu.OZ.AU> wrote: > On Sun, Jan 13, 2002 at
10:52:37PM +1100, Dave Slutzkin wrote:
> > 
> > empty_timeline = [].
> > full_timeline([_|_]).
> 
> You may want to declare empty_timeline as
> 
> :- func empty_timeline = (timeline::out(empty_list)) is det.
> 
> where
> 
> :- inst empty_list = bound([]).
> 
> which is more precise.

Right.  I suppose if I'm specifying 'non-empty' then I should also specify
'empty'.
 
> > :- pred crop_timeline(timeline::in,time::in,
> > timeline::out) is det.
> > crop_timeline(Line,T,NewLine) :-
> >     Line = empty_timeline ,
> >     NewLine = empty_timeline
> > ;
> >     full_timeline(Line) ,
> >     % we know we have a non-empty list here
> >     E = first_event(Line) ,
> > .
> 
> If the function first_event/1 is declared to take a non_empty_list as
> input, i.e.
> 
> 	:- mode first_event(ne_tl_in) = out is det.
> 
> then it (and other calls after it) will be able to use the information
> that Line is non-empty.

Exactly.  So full_timeline was supposed to let the compiler realise that it
was a non-empty list so that then first_event could be ne_tl_in, and
therefore deterministic.
 
> What _mode_ benefits do you get from using empty_timeline and
> full_timeline rather than [] and [_|_] ?

The mode benefits only come from full_timeline 'promising' a non-empty list. 
The other benefits I was hoping for were modularity, and implementation
hiding.  So I could theoretically change the implementation of timeline from
list to something else (which is likely) without having to change any
existing code.
 
> As Ralph pointed out, the determinism analysis won't currently detect
> that empty_timeline and full_timeline are mutually exclusive and
> exhaustive (although there is someone working on allowing you to promise
> this).  However, as far as _modes_ are concerned, using the constructors
> directly instead of wrapping them in appropriately declared predicates
> should make no difference.

So if I have:

    Line = [_|_]

then I still get the mode benefit?  I suppose the compiler still knows that
it must be a non_empty_list...  I hadn't tried that, but that would be a
part-solution to my problem.  (And I'll try it as soon as I get back to my
machine.)

So I've got the mode benefits, but still sorta without the hidden
implementation that would be the ultimate.

thanks,

Dave.

http://my.yahoo.com.au - My Yahoo!
- It's My Yahoo! Get your own!
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list