# [mercury-users] RE: Difference of Predicate Declaration from Implementation

stormie at hotmail.it stormie at hotmail.it
Tue Sep 4 22:43:15 AEST 2007

```Hi All,

Looks like I have resolved my queries after reading on DCGs (http://www.amzi.com/manuals/amzi7/pro/ref_dcg.htm).

Regards,

________________________________
From: stormie at hotmail.it
To: mercury-users at cs.mu.oz.au
Subject: Difference of Predicate Declaration from Implementation
Date: Tue, 4 Sep 2007 11:58:26 +0200

I have question from calculator.m example in mercury

1) Arguments in the predicate declaration (e.g. fullexpr) are different  from its implementation.
e.g. arguments for fullexpr are three, but it just takes one (i.e. X) in its implementation.
Why is that so?

2) What is the purpose of ['\n']

-------------------------------------------------------------

% Simple recursive-descent parser.

:- pred fullexpr(expr::out, list(char)::in, list(char)::out) is semidet.

fullexpr(X) -->
expr(X),
['\n'].

:- pred expr(expr::out, list(char)::in, list(char)::out) is semidet.

expr(Expr) -->
factor(Factor),
expr2(Factor, Expr).

:- pred expr2(expr::in, expr::out, list(char)::in, list(char)::out) is semidet.

expr2(Factor, Expr) -->
( ['+'] -> factor(Factor2), expr2(plus( Factor, Factor2), Expr)
; ['-'] -> factor(Factor2), expr2(minus(Factor, Factor2), Expr)
; { Expr = Factor }
).

:- pred factor(expr::out, list(char)::in, list(char)::out) is semidet.

factor(Factor) -->
term(Term),
factor2(Term, Factor).

:- pred factor2(expr::in, expr::out, list(char)::in, list(char)::out)
is semidet.

factor2(Term, Factor) -->
( ['*'] -> term(Term2), factor2(times(Term, Term2), Factor)
; ['/'] -> term(Term2), factor2(div(  Term, Term2), Factor)
; { Factor = Term }
).

:- pred term(expr::out, list(char)::in, list(char)::out) is semidet.

term(Term) -->
( const(Const) ->
{ string.from_char_list(Const, ConstString) },
{ string.to_int(ConstString, Num) },
{ Term = number(Num) }
;
['('], expr(Term), [')']
).

:- pred const(list(char)::out, list(char)::in, list(char)::out) is semidet.

const([Digit|Rest]) -->
digit(Digit),
( const(Const) ->
{ Rest = Const }
;
{ Rest = [] }
).

:- pred digit(char::out, list(char)::in, list(char)::out) is semidet.

digit(Char) -->
[Char],
{ char.is_digit(Char) }.

________________________________
Con Windows Live Messenger chatti, parli, giochi, crei il tuo blog, controlli la tua posta, scambi file, foto e molto altro con un solo programma GRATUITO. Scarica Messenger 8.1

_________________________________________________________________
Invite your mail contacts to join your friends list with Windows Live Spaces. It's easy!
http://spaces.live.com/spacesapi.aspx?wx_action=create&wx_url=/friends.aspx&mkt=en-us
--------------------------------------------------------------------------
mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au