[Fwd: Mozart 1.2.3]

Denys Duchier Denys.Duchier at ps.uni-sb.de
Sun Jun 16 15:17:20 CEST 2002


brunklaus at ps.uni-sb.de (Thorsten Brunklaus) writes:

> Mozart Compiler 1.2.3 (20011204) playing Oz 3
>
> fun {Map Xs F}
>    case Xs
>    of nil then nil
>    [] X|Xr then {F X}|{Map Xr F}
>    end 
> end 
> {Browse {Map [1 2 3 4] fun {$ X} X*X end}}
>
> %********************** static analysis error *******************
> %**
> %** unification error in needed statement
> %**
> %** First value:  <P/3 Map>
> %** Second value: <P/3>
> %** in file "Oz", line 1, column 0
> %** ------------------ rejected (1 error)

The error message tells you that the system attempts to unify the
library procedure Map (i.e. List.map, here denoted by <P/3 Map>) with
another procedure (here simply denoted by <P/3>.  Since these are
distinct procedure (and procedures have token equality), the
unification fails.

Perhaps contrary to your expectation:

    fun {Foo ...} ... end

is not a function declaration: it is a statement which (1) creates a
procedure value and (2) unifies it with Foo.  Foo needs to be
explicitly introduced.  If the fun statement is executed in a scope
where Foo hasn't been introduced, then an error is signaled that Foo
is unknown.  If it is executed in a scope where Foo has been
introduced, then it attempts to unify Foo with the new procedure value
which it creates (at run time).

Thus you could write:

    declare Foo in fun {Foo ...} ... end

to first introduce a (new) variable Foo and then bind it to the
procedure created by the fun statement.

For convenience, Oz also permits to write:

    declare fun {Foo ...} ... end

to the same end.  Here is how this works.  Consider `declare' and
`local' constructs; they have the following syntax:

    declare DECLARATIONS in STATEMENTS
    local   DECLARATIONS in STATEMENTS

In principle, DECLARATIONS should consist just of variables,
i.e. precisely these variables which are being introduced in the
corresponding scope.

However writing

    declare X1 X2 X3 in [X1 X2 X3]={GetThreeValues}
or  local   X1 X2 X3 in [X1 X2 X3]={GetThreeValues} ... end

is not very convenient, so Oz allows you to write:

    declare [X1 X2 X3]={GetThreeValues}
    local   [X1 X2 X3]={GetThreeValues} ... end

[X1 X2 X3]={GetThreeValues} is a statement, but when it occurs within
DECLARATIONS, the variables occurring on the LEFT-side are
automatically considered to be part of the DECLARATIONS (i.e. are
introduced in the scope).

More generally, it is permited to intersperse arbitrary statements
with the DECLARATIONS, in which case some of these statements are
given special interpretation to conveniently introduce the variables
which they bind.

1. E1=E2

   if E1 is a variable, a record, a list, or in general any syntactic
   expression that denotes plain data rather than procedure
   applications, then all variables occurring in E1 are considered to
   be introduced.

2. fun  {F ...} ... end
   proc {P ...} ... end
   class C ... end
   functor F ... end

   again, if these occur directly in DECLARATIONS, then the variable
   which the statement binds (resp. F, P, C and F) is considered to be
   introduced in the DECLARATIONS.

What is missing in your example, is simply a precedeing `declare'.

> local 
>    fun {AndThen BP1 BP2}
>       if {BP1} then {BP2}
>       else false end 
>    end 
>    fun {BinaryTree T}
>       case T
>       of nil then true 
>       [] tree(K V T1 T2) then 
>          {AndThen
>           fun {$} {BinaryTree T1} end 
>           fun {$} {BinaryTree T2} end}
>       else false end 
>    end 
> end
>
> %*************************** parse error ************************
> %**
> %** expecting `in'
> %**
> %** in file "Oz", line 24, column 0
> %** ------------------ rejected (1 error) ----------

The error message tells you precisely what the problem is: you forgot
the keyword `in'.  The syntax of the `local' construct is:

    local DECLARATIONS in STATEMENTS end

Cheers,

-- 
Dr. Denys Duchier			Denys.Duchier at ps.uni-sb.de
Forschungsbereich Programmiersysteme	(Programming Systems Lab)
Universitaet des Saarlandes, Geb. 45	http://www.ps.uni-sb.de/~duchier
Postfach 15 11 50			Phone: +49 681 302 5618
66041 Saarbruecken, Germany		Fax:   +49 681 302 5615
-
Please send submissions to users at mozart-oz.org
and administriva mail to users-request at mozart-oz.org.
The Mozart Oz web site is at http://www.mozart-oz.org/.





More information about the mozart-users mailing list