[mercury-users] syntax across languages

Ralph Becket rafe at cs.mu.OZ.AU
Tue Jun 4 11:35:23 AEST 2002


Pixel, Monday,  3 June 2002:
> Ralph Becket <rafe at cs.mu.OZ.AU> writes:
> > 
> > It seems a small thing to worry about.  The Mercury term for a
> > disjunction where each arm is headed by a unification of a variable with
> > a different data constructor is `switch', precisely because of its
> > similarity to the C construct of the same name.
> 
> what i mean:
> 
> switch (a_long_expression(with_lots_of_parameters)) {
>   case foo: ... 
>   case bar: ...
> }
> 
> vs
> 
> ( a_long_expression(with_lots_of_parameters) = foo, ...
> ; a_long_expression(with_lots_of_parameters) = bar, ...
> )
> 
> i'd prefer something along:
> 
>    X = a_long_expression(with_lots_of_parameters),
>    (X = foo, ... ; X = bar, ...))
> 
> is that ok?

Perfectly fine (and it's at least as concise as the C version, to boot.)

> > A Mercury switch is not the same as an if-then-else chain.  For a start,
> > behaviour depends on the mode.  Also (unless the right compiler flags are
> > given) the compiler is free to reorder the disjuncts.
> 
> ok sorry. 
> - I thought mercury had a "red cut"

No, Mercury is 100% declarative (the only way to write impure code is to
use a foreign function).  Mercury does have committed-choice
non-determinism, but you have to say so explicitly if you want it and it
does have a sound declarative semantics (unlike cut, which has no
declarative semantics - or at least none that you'd care to use...)

> - and i didn't understand what the "switch" term was about. I've read
> and understood that "switch" in mercury is a special kind of
> disjunctions.

That's correct.  Mercury can spot switch disjunctions and emit very
efficient code for them.

> > > > 		(1) multiple clauses:
> > > > 
> > > > 		foo(positive) = ...
> > > > 		foo(negative) = ...
> > > > 		foo(zero) = ...
> > > 
> > > i'd prefer an anonymous solution. is there one?
> > 
> > This is it!  The variable being switched on isn't even mentioned, so in
> > that sense it's even more `anonymous' than the C version.
> 
> ???
> 
> C has
> 
> switch (a_long_expression(with_lots_of_parameters)) {
>   case foo: ... 
>   case bar: ...
> }
> 
> the *value* switched on isn't mentioned.

Yes it is, in the switch(_) part.

Mercury clausal form is shorthand for

	foo(X) = Y :-
		(	X = positive,	Y = ...
		;	X = negative,	Y = ...
		;	X = zero,	Y = ...
		).

except that you don't have to mention X explicitly.

> in any case, here i meant that in "foo(positive)", "foo" is not
> anonymous! If there is no anonymous construct, is there a way to have
> "foo" localized a la haskell:
>   foo (a_long_expression with_lots_of_parameters)
>     where foo Positive = ...
>           foo Negative = ...

You can define lambdas, although the syntax for them isn't as nice as
Haskell's (we don't yet support clausal form lambdas):

	Foo = ( func(X) = Y :-
			( X = positive, Y = ...
			; X = negative, Y = ...
			; X = zero,	Y = ...
			) ),
	Z = Foo(a_long_expression with_lots_of_parameters)

I want to add support for fully general local definitions to Mercury,
but it's a fairly low priority at the moment.

Cheers,

Ralph
--------------------------------------------------------------------------
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