Some questions on Clean

Jan Krynicky JKRY3025@barbora.mff.cuni.cz
Tue, 3 Dec 1996 11:32:29 +0200


> Hello alltogether,
> I have some questions on Clean, 'hope you can add light:
> 
> 2. How do I observe an unique State? The following piece of code
>    shows the problem:
> 
> :: *S :== (Int, Int);
> 
> getA :: *S -> Int;
> getA (a,_) = a;
> 
getA :: *S -> (Int, *S)
getA (a,b) = (a,(a,b))

>
> // this wont compile:
> 
> doubleA s = s` where
> {
>   a = getA s;
>   s` = setA (a+a) s
> };

doubleA s = s`` 
 where
  (a,s`) = getA s
  s` = setA (a+a) s`
> 
> When I try to observe an unique State with a function like getA, the
> compiler moans about "conflicting uniqueness information".  The
> uncommented variant of "doubleA" works. What am i doing wrong?
> 
You are changing the object by looking at it. You must make a new
"observed" object.

Think about this:
s = (5,5)
foo s = (a,s`)
 where
  a = getA s
  s` = setA 3 s

What should be in 'a', 5 or 3 ? Keep in mind that after 's`=setA ...'
the old 's' doesn't exist (you can and probably will reuse the space) 
but if 'a' is still not evaluated it points into the nonexistent 
structure. So if you evaluate 'a' and then's`', everything is OK.
If you do it in reverse order - result is undefined. 

----

Maybe it would be better if you define
exchangeA :: Int *S -> (Int,*S)
exchangeA na (a,b) = (a,(na,b))

everything else can be made using that:

doubleA s = s`
 where
  (a,s`) = exchangeA (a+a) s
// it looks a bit unusual but it should work.

getA s = (a,s`)
 where
  (a,s`) = exchangeA a s
  
setA a s = s`
 where
  (_,s`) = exchangeA a s

> Cheerio,
> Andreas 
> 

Hi, Jenda-------------------Jenda--------------------
Jan Krynicky    JKRY3025@BARBORA.MFF.CUNI.CZ
http://www.ms.mff.cuni.cz/~jkry3025