[mercury-users] Locally defined recursive procedures in Mercury

Gregory D. Weber gdweber at indiana.edu
Sun Jun 5 05:24:20 AEST 2005


Is it possible to define a recursive procedure within another
procedure, similar to Scheme's letrec or CommonLisp's labels?  
Clearly it is not possible in what seems the "obvious"
way to me.  For example:

:- pred example_set_insert_list(list(example)::in, example_set::in,
				example_set::out, list(example)::out) is det.
%% Insert each example from list Examples into Set1, making Set2.
%% Post: Overflows is a list, possibly empty,  of the examples removed
%%       from Set1 to make room for those in List.
example_set_insert_list(Examples, Set1, Set2, Overflow) :-
	Loop = (pred(Exs::in, S1::in, S3::out, O1::in, O2::out) % line 716
	       is det :-
	       (
		 Exs = [],
		 S3 = S1,
		 O2 = O1
	       ;
		 Exs = [H | T],
		 example_set_insert(H, S1, S2, Mremoved),
		 (
		   Mremoved = yes(Removed),
		   Loop(T, S2, S3, [Removed | O1], O2) 
		 ;
		   Mremoved = no,
		   Loop(T, S2, S3, O1, O2)
		 )
	       )),
	Loop(Examples, Set1, Set2, [], Overflow).  %% line 733

icn.m:733: In clause for `example_set_insert_list(in, in, out, out)':
icn.m:733:   mode error in conjunction. The next 2 error messages
icn.m:733:   indicate possible causes of this error.
icn.m:716: In clause for `example_set_insert_list(in, in, out, out)':
icn.m:716:   mode error: variable `Loop' has instantiatedness `free',
icn.m:716:   expected instantiatedness for non-local variables
icn.m:716:   of lambda goals is `ground'.
icn.m:733: In clause for `example_set_insert_list(in, in, out, out)':
icn.m:733:   in argument 1 (i.e. the predicate term) of higher-order predicate call:
icn.m:733:   mode error: variable `Loop' has instantiatedness `free',
icn.m:733:   expecting higher-order pred inst (of arity 5).

I understand that Loop can't be recursive because Loop must be bound in the
pred expression.  Unification does not provide the equivalent of letrec.

Greg

-- 
Gregory D. Weber             http://mypage.iu.edu/~gdweber/
                             Telephone (765) 973-8420
Associate Professor of Computer Science; Chair, Informatics Committee
Indiana University East
2325 Chester Boulevard, Richmond, Indiana 47374-1289, U.S.A.
----
Plain text is the document format that maximizes readability and
minimizes hassle and hazard.  It is the format of the official documents
defining Internet protocols (http://www.rfc-editor.org/).
--------------------------------------------------------------------------
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