[mercury-users] full and empty lists
Dave Slutzkin
dave_slutzkin at yahoo.com
Sun Jan 13 22:52:37 AEDT 2002
Hi all.
I have a type 'timeline' (which should probably be a
typeclass - at the moment it isn't), implemented using
the standard 'list' module. But I've tried to put
wrappers around all of the functions that access it so
I can change it easily later. I have a snippet of
code that looks like this:
:- mode ne_tl_io == ground >> non_empty_list.
:- mode ne_tl_in == non_empty_list >> non_empty_list.
:- func empty_timeline = (timeline::out) is det.
:- pred full_timeline(timeline::ne_tl_io) is semidet.
empty_timeline = [].
full_timeline([_|_]).
Bascially - I was hoping that empty_timeline and
full_timeline could be used in a switch statement the
same way as [] and [_|_].
Also, after a call to full_timeline, I was hoping that
the compiler would realise that I had a non-empty list
and so I could save this checking in many later
predicates. Another snippet:
:- 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) ,
.
.
.
Note the use of empty_timeline and full_timeline in
the switch (are they guards?). And also that
first_event can be deterministic, because it takes a
non-empty list so will always succeed.
Well, it doesn't work. The compiler still sees this
code as nondeterministic - I quote:
event.m:108: In the return value of call to function
`event:empty_timeline/0':
event.m:108: unification with `Line' can fail.
event.m:111: call to
`full_timeline((event:ne_tl_io))' can fail.
However, it still works when I replace empty_timeline
with [] and full_timeline with [_|_] - although
without the mode benefits of non_empty_list.
Is there anything I can do about this? Does anyone
even understand the problem? Otherwise I'll have to
go back to the less modular, less deterministic
version.
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