[mercury-users] Difference of Predicate Declaration from Implementation

Stormie Stormie stormie at hotmail.it
Tue Sep 4 19:58:26 AEST 2007



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) }.

_________________________________________________________________
Doretta รจ In linea: vieni a conoscerla su Messenger!
http://www.doretta82.it/banner/index.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20070904/506019ab/attachment.html>


More information about the users mailing list