[m-dev.] ui arguments in closures

Mark Brown mark at csse.unimelb.edu.au
Mon Jun 23 16:30:30 AEST 2008


On 23-Jun-2008, Ralph Becket <rafe at csse.unimelb.edu.au> wrote:
> This is pertinent to G12.
> 
> I have a predicate
> 
> :- pred output_environment(solver::in, env::in, ps::ui, io::di, io::uo) is det.
> 
> output_environment(Solver, Env, PS, !IO) :-
>     map.foldl(output_env_var(Solver, PS), Env, !IO).	% --- Line 1023 ---
> 
> :- pred output_env_var(solver::in, ps::ui, name::in, value::in, io::di, io::uo)
> 	is det.
> 
> ...
> 
> The compiler is complaining thus:
> flatzinc_solver.m:1023: In clause for `output_environment(in, in, ui, di, uo)':
> flatzinc_solver.m:1023:   in argument 2 of call to predicate
> flatzinc_solver.m:1023:   `flatzinc_solver.output_env_var'/6:
> flatzinc_solver.m:1023:   mode error: variable `PS' has instantiatedness
> flatzinc_solver.m:1023:   `ground',
> flatzinc_solver.m:1023:   expected instantiatedness was `unique'.
> 
> Do I take it that ui arguments in closures are treated as ground by the
> compiler?  If so, is this a bug or is there a reason for this behaviour?

It is not a bug -- the PS argument is no longer unique.  That is, the
compiler can't guarantee that you won't call the closure after somebody
else has destructively updated PS.

To do a foldl with a ui parameter, you need a version like this:

	:- pred foldl_ui(pred(S, T, A, A)::in(pred(ui, in, in, out) is det),
		S::ui, list(T)::in, A::in, A::out) is det.

	foldl_ui(Pred, PS, Ts, !Acc) :-
	    (
	        Ts = []
	    ;
	        Ts = [T0 | Ts0],
	        Pred(PS, T0, !Acc),
	        foldl_ui(P, PS, Ts0, !Acc)
	    ).

(I know this is over a list, not a map, but you get the picture.)

Cheers,
Mark.

--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at csse.unimelb.edu.au
Administrative Queries: owner-mercury-developers at csse.unimelb.edu.au
Subscriptions:          mercury-developers-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the developers mailing list