[mercury-users] Queries for clarifing...

Fergus Henderson fjh at cs.mu.OZ.AU
Sun Oct 13 21:56:54 AEST 2002


On 13-Oct-2002, Noel  Pinto <cool4life at rediffmail.com> wrote:
> 1) Are rules and facts declared by type variables or pred and 
> func?

I'm not quite sure what you mean.  If you mean what I think you mean,
then the simple answer is "No".  There might be a more *helpful* answer,
but that's the only answer that occurs to me right now.

> Here is some syntax for declaring for declaring rules and 
> predicates.  I want to verify whether I am right. Plz tell me if I 
> am :
> a) Predicate Rule
> :- type member(X, [_ | Xs]) :- member(x, Xs).

That is a syntax error.

> Predicate Rule
> :- type member(X, [_ | Xs]).

That is also a syntax error.

> b) Function Rule
> :- type member(X, [_ | Xs]) = string :- member(x, Xs).

That is also a syntax error.

> Function Rule
> :- type member(X, [_ | Xs]) = string.

That is also a syntax error.

> 2) Consider an example for example for a goal,
> member(X, As) => member(X, Bs).
> Can any variable like As and Bs having 's' as suffix be called as 
> a List??

No.  In general, the type of a variable does not need to have
any relationship to the variable's name.

In this particular example, if `member' is the predicate declared in
the standard library list module, then the variables As and Bs are lists.

> I have some more queries :-
> 1) In the manual, or-nodes and and-nodes are defined as, or-nodes 
> are constructors that can be used to construct terms of that 
> particular type and and-nodes are types of arguments of 
> constructors. Mode Information is attached to or-nodes of type 
> trees.
> a) What is a type tree?
> b) I am bit confused... There are certain examples from the manual 
> I mentioning below.
> :- type employee
>  	---> employee(
>  		name :: string,
>  		age :: int,
>  		department :: string
>  	).
> Is this an and-node???

The type tree corresponding to this type looks like this:

     employee
        OR
         |
     employee
        AND
       / | \
      /  |  \
 string int string

> c)
> :- type strange
>  	---> foo(int)
>  		; bar(string).
> Is this an or-node?

The type tree corresponding to this type looks like this:

       strange
         OR
        /  \
       /    \
     foo    bar
     AND    AND
      |      |
     int  string

> d) What about this...
> :- type fruit
>  	---> apple
>  		; orange
>  		; banana
>  		; pear.

The type tree corresponding to this type looks like this:

                  OR
                  /|\
                 // \\
                / | | \
               / /  |  \
              /  |  |   \
             /   |  |    \
            /   /   |     \
           /   /    |      \
          /   /     |       \
      apple orange banana   pear
       AND    AND    AND     AND

> e) Could I have type like this...
> :- type fruit
>  	---> apple
>  		; orange
>  		; banana(weight :: int,
>  			quality :: string)
>  		; pear(int).

Yes.

> What node would this be called?

The type tree corresponding to this type would be

                 fruit
                  OR
                  /|\
                 // \\
                / | | \
               / /  |  \
              /  |  |   \
             /   |  |    \
            /   /   |     \
           /   /    |      \
          /   /     |       \
      apple orange banana   pear
       AND    AND    AND     AND
                     / \
                    /   \
                  int  string

> 2) In the manual, an example shows mode names are defined and then 
> used. The example is below.
> :- mode out_listskel ==
>  	free >> listskel.
> :- mode in_listskel ==
>  	listskel >> listskel.
> 
> :- func length(list(T)) = int.
> :- mode length(in_listskel) = out.
> :- mode length(out_listskel) = int.
> 
> a)First out_listskel and in_listskel are defined. Why isn't pred 
> or func being defined first?

I'm not quite sure what you mean by this question.

Within a module, the order of declarations (other than pragmas)
is not important.

"pred" are "func" are, among other things, built-in modes and insts,
using for denoting higher-order modes or insts.  As such, they are
automatically predefined by the compiler, and do not need to be
defined by the user.

"pred" and "func" are also built-in operators used in predicate
and function type declarations.  Again, this use of "pred" and "func"
is built in to the compiler and so "pred" and "func" don't need
to be defined first.

> b) Can modes be used inside other modes,

Yes.

> c) Should listskel be defined before it is used, if yes then as 
> what?

The inst `listskel' should be defined, but the definition does not need
to occur before the first use.  It just needs to be defined somewhere.

It could be defined by

	:- inst listskel == bound([] ; [free | listskel]).

Cheers,
	Fergus.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list