[clean-list] variable sharing between expressions, gui app prints 42 at the end, rem/mod inconsistency

Arjen van Weelden A.vanWeelden at cs.ru.nl
Tue Jun 20 18:31:00 MEST 2006


Hi Peter,

Peter Hercek wrote:
> Hi all,
> 
> A few questions from a beginner :)
> 
> Clean language report says that "when a variable occurs more than once
> in a function body, the semantics prescribe that the actual argument is
> shared". So I expected the fib sequence below to have linear (and not
> exponential) complexity.
> 
> (>>>) infix 0 :: a !b -> a |toString b
> (>>>) val msg = trace_n msg val
> 
> take_ul :: !Int [#.a] -> [#.a] | UList a
> take_ul 1 [# h : t ] = [# h ]
> take_ul n [# h : t ] = [# h : take_ul (n-1) t ]
> 
> fib = [# 1, 1 : [# a+b >>> a+b \\ a <|- fib & b <|- tail_ul fib ] ]
> where
>  tail_ul [# _ : t ] = t
> 
> Start = take_ul 4 fib
> 
> But it does not seem that the fib sequence is being shared between right
> hand side and left hand side since the computation of the third element
> is being done 2 times as the trace output indicates. Can somebody help
> me out what I'm missing?

You have defined fib as a function, whose result is recomputed each time
it is evaluated. This is unlike Haskell, where any top-level function
becomes a CAF. Define fib as a Constant Applicative Form in Clean and it
should work (haven't tested it though).

fib =: [# 1, 1 : [# a+b >>> a+b \\ a <|- fib & b <|- tail_ul fib ] ]
where
 tail_ul [# _ : t ] = t

> Second problem is that a very simple dialog application is printing 42
> at the end (after I confirm the dialog) and waits for keyboard input
> before exiting. How to get rid of this 42 and keyboard input?
> 
> % cat hello.icl
> module hello
> import StdEnv, StdIO
> 
> Start world =
>  startIO NDI Void (open_dialog) [ProcessClose closeProcess] world
>  where
>    open_dialog pSt
>      # (okId,pSt) = openId pSt
>      = snd (openDialog undef (dialog okId) pSt)
>    dialog okId = Dialog "Hello Dialog"
>      ( TextControl "Hello World, I am a Clean user!" [] :+:
>        ButtonControl "OK" [ ControlId okId,
>                             ControlFunction quit ]
>      ) [ WindowOk okId ]
>    quit (ls,pSt) = (ls,closeProcess pSt)
> % ./hello.exe
> 42
> %
> I'm trying Windows Clean 2.1.1.

This 42 is the `state of the World', which in console programs shows up
as 65535. Select Project->Project Options... in the IDE and select
`Basic Values Only', `No Return Type', or `No Console' to make it go away.

> And the last one. Is Sparkle and Std environment supposed to be the
> same? I though it should but Sparkle defines both rem and mod operators
> but Std defines only rem. Also what is the difference between mod and rem?

They differ because not all StdEnv function behave in a mathematically
pleasant way. See for example drop and take in StdList; take n xs ++
drop n xs == xs is true in the Sparkle-StdEnv but not in the StdEnv. I
don't think there was ever any difference between mod and rem, although
some people expect mod to yield results with the same sign as its second
operand. I think they removed it because it was confusing and
superfluous. Apparently, it is still part of the Sparkle-StdEnv.

> Peter.
> _______________________________________________
> clean-list mailing list
> clean-list at science.ru.nl
> http://mailman.science.ru.nl/mailman/listinfo/clean-list

kind regards,
	Arjen


More information about the clean-list mailing list