[mercury-users] Newbie Q on type parameters
Ondrej Bojar
bojar at csse.unimelb.edu.au
Tue Aug 28 09:56:11 AEST 2007
Dear Tom,
when passing (or accepting) higher order terms, the input mode must not be plain
'in' but rather a higher-order mode that describes the mode of the higher order
predicate.
Try changing:
:- pred solve(pred(T,T)::in, pred(T)::in, T::in, list(T)::out) is nondet.
to
:- pred solve(pred(T,T)::in(pred(in, out) is nondet),
pred(T)::in(pred(in) is semidet),
T::in, list(T)::out) is nondet.
and similarly for :- pred breadthfirst declaration.
Same holds for generating predicates on the fly and returning them as higher
order terms, e.g.:
:- pred make_int_to_string_rendering_predicate(input_config::in,
pred(int, string)::out(pred(in, out) is det)) is det.
Cheers, Ondrej.
Tom Davies wrote:
> Hi,
>
> I'm trying to convert a working Mercury program to a more flexible one
> which uses type parameters and higher order predicates.
>
> I'm getting error messages which I don't understand.
>
> In particular, I get:
>
> solve.m:030: In clause for `breadthfirst(in, in, in, out)':
> solve.m:030: in argument 1 (i.e. the predicate term) of higher-order
> solve.m:030: predicate call:
> solve.m:030: mode error: variable `Goal' has instantiatedness `ground',
> solve.m:030: expecting higher-order pred inst (of arity 1).
>
> for:
>
> ...
> :- pred goal(node).
> :- mode goal(in) is semidet.
> goal(Situation) :-
> Situation = g.
>
> :- pred solve(pred(T,T)::in, pred(T)::in, T::in, list(T)::out) is nondet.
> solve(Succ, Goal, Start, Solution) :-
> breadthfirst(Succ, Goal, [[Start]], Solution).
>
> :- pred breadthfirst(pred(T,T)::in, pred(T)::in, list(list(T))::in,
> list(T)::out) is nondet.
> breadthfirst(Succ, Goal, [[Node | Path] | _], [Node | Path]) :-
> call(Goal,Node). % <---- line 30
> ...
>
> Thanks for any advice, the full text of the program is below.
>
> Tom
>
>
> :- module solve.
> :- interface.
> :- import_module io.
> :- pred main(io::di, io::uo) is cc_multi.
> :- implementation.
> :- import_module list.
> :- import_module solutions.
>
> :- type node ---> a; b; c; d; e; f; g.
> :- pred s(node,node).
> :- mode s(in,out) is nondet.
> s(a,b).
> s(a,c).
> s(b,d).
> s(b,e).
> s(d,f).
> s(e,g).
>
> :- pred goal(node).
> :- mode goal(in) is semidet.
> goal(Situation) :-
> Situation = g.
>
> :- pred solve(pred(T,T)::in, pred(T)::in, T::in, list(T)::out) is nondet.
> solve(Succ, Goal, Start, Solution) :-
> breadthfirst(Succ, Goal, [[Start]], Solution).
>
> :- pred breadthfirst(pred(T,T)::in, pred(T)::in, list(list(T))::in,
> list(T)::out) is nondet.
> breadthfirst(Succ, Goal, [[Node | Path] | _], [Node | Path]) :-
> call(Goal,Node).
>
> breadthfirst(Succ, Goal, [Path | Paths], Solution) :-
> trace [io(!IO)] (io.write_list([Path | Paths], "---\n", io.print, !IO),
> io.nl(!IO)),
> extend(Succ, Path, NewPaths),
> append(Paths, NewPaths, Paths1),
> breadthfirst(Succ, Goal, Paths1, Solution).
>
> :- pred extend(pred(T,T)::in,list(T)::in,list(list(T))::out) is nondet.
> extend(Succ, [Node | Path], NewPaths) :-
> solutions(
> pred(X::out) is nondet :-
> (
> X = [NewNode, Node | Path],
> call(Succ, Node, NewNode),
> not member(NewNode, [Node | Path])
> ),
> NewPaths).
>
> /*
> * Note that the solution returned by solve starts with the final goal
> node, so
> * we reverse it before printing it.
> */
> main(!IO) :-
> if solve(s,goal,a,S) then
> reverse(S,S1), print(S1,!IO)
> else
> print("Failed\n",!IO).
>
>
> --------------------------------------------------------------------------
> mercury-users mailing list
> Post messages to: mercury-users at csse.unimelb.edu.au
> Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
> Subscriptions: mercury-users-request at csse.unimelb.edu.au
> --------------------------------------------------------------------------
--------------------------------------------------------------------------
mercury-users mailing list
Post messages to: mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions: mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------
More information about the users
mailing list