Path: utzoo!utgpu!water!watmath!clyde!bellcore!rutgers!cmcl2!nrl-cmf!ames!pasteur!agate!labrea!decwrl!sun!pitstop!sundc!seismo!uunet!mcvax!ukc!eagle!rej
From: rej@eagle.ukc.ac.uk (R.E.Jones)
Newsgroups: comp.lang.misc
Subject: Re: a simple miranda problem
Message-ID: <5447@eagle.ukc.ac.uk>
Date: 10 Aug 88 10:03:15 GMT
References: 
Reply-To: rej@ukc.ac.uk (R.E.Jones)
Distribution: comp
Organization: Computing Lab, University of Kent at Canterbury, UK.
Lines: 52

In article  feldmark@hanako.stars.flab.Fujitsu.JUNET writes:
>
>I have been reading about miranda and am trying to understand a simple
>program given in the article "An Overview of Miranda" from SIGPLAN
>Notices, December 1986.  
>
>answer = twice twice twice suc 0
>twice f x = f (f x)
>suc x = x + 1
>
>I'm trying to apply partial paramaterization at each step and get an
>exact picture of what's going on as far as the execution mechanism is
>concerned, but keep coming up with weird results.  I think the
>parenthesis are confusing me. I am assuming that functions inside
>parenthesis should be applied to arguments and then parenthesis
>removed.  

I don't think your model of evaluation helps. You must remember that Miranda
uses lazy evaluation - arguments will not be evaluated unless they are needed.
Thus if
	const a b = a
then
	const 3 (reverse [1..]) evaluates to 3.
Note: [1..] is the list of all natural numbers. This expression terminates
precisely because the second argument to const is not needed, and so
(reverse [1..]) is not evaluated.

I suspect that your problem does indeed lie with the brackets. You need to
remember that Miranda functions are curried: they take just ONE argument. Thus
      			f a b = (f a) b
Notice the difference between this and the definition of twice.

Anyway, the evaluation proceeds as follows. We'll use with simpler examples.
I'll try not to play too fast and loose with the brackets :-). [ => means
'evaluates to', == means 'is the same as'.]

twice suc 0
=> suc (suc 0)
=> (suc 0) + 1
=> (0 + 1) + 1
=> 2

twice twice suc 0 == (twice twice suc) 0
=> twice (twice suc) 0
=> (twice suc) (twice suc 0) == twice suc (twice suc 0)
=> suc (suc (twice suc 0))
=> (suc (twice suc 0)) + 1
=> ((twice suc 0) + 1) + 1
=> ...
=> 4

...and similarly, answer => ... => 16 (eventually!).