[mercury-users] Pred defns
Ralph Becket
rwab1 at cam.sri.com
Wed Mar 25 23:20:55 AEDT 1998
Peter Phelan wrote on 25 Mar:
> Hello again
>
> I'm confused by pred definitions in Mercury.
>
> The following is snipped from calculator.m in samples.
>
> :- pred fullexpr(expr::out, list(char)::in, list(char)::out)
> is semidet.
> fullexpr(X) -->
> expr(X),
> ['\n'].
>
> How come the declarative definition takes just one argument,
> whilst there are 3 in the type declaration?
>
> It's something to do with input/output, but this is also
> difficult to understand.
With the best will in the world, RTFM. Have a look at
http://www.cs.mu.oz.au/research/mercury/doc/reference_manual_2.html#SEC11
Often one writes predicates that take some `state' as input and
produce a transformed `state' as output. When you're doing a lot of
IO, for example, this can be a real pain:
say_hi(Mum, IOStateIn, IOStateOut) :-
io__write_string("Hello ", IOStateIn, TmpIOState0),
io__write_string(Mum, TmpIOState0, TmpIOState1),
io__write_string("!", TmpIOState1, TmpIOState2),
io__nl( TmpIOState2, IOStateOut)
(of course, you wouldn't write it like this, but you get the idea).
DCGs are a formalism for adding the pair of `state' thread arguments
to the end of the head and the goals within a clause. That's what the
--> is for, rather than :-
say_hi(Mum, IOStateIn, IOStateOut) -->
io__write_string("Hello "),
io__write_string(Mum),
io__write_string("!"),
io__nl.
However, you still have to declare the implicit extra pair of
arguments in pred and mode declarations so the compiler knows the
right type and mode for them.
Ralph
--
Ralph Becket | rwab1 at cam.sri.com | http://www.cam.sri.com/people/becket.html
More information about the users
mailing list