# [Haskell-beginners] Re: Hudak state emulation discussion - can you give me some idea?

Will Ness will_n48 at yahoo.com
Wed Mar 18 10:45:27 EDT 2009

```Benjamin L. Russell <DekuDekuplex <at> Yahoo.com> writes:

>
> On Wed, 18 Mar 2009 13:02:34 +0530, Girish Bhat
> <girishbhat6620 <at> gmail.com> wrote:
>
> >>[...]
> >>
> >Thanks! It does. I think what threw me was that while there is enough
> >redundancy in what he states for someone more clever than me, he would
> >have explicitly stated before hand  that he was defining the operators
> >[:=], [+'], ['if] etc.:)
>
> But he did; specifically, on pages 405 (the previous page) to 406 of
>
> So he did in fact explicitly state beforehand that he was defining
> those operators.
>

What he didn't do, is specify the precedences associativities of these
operators.

It seems that for the definitions to make sense, together with the code that
follows them in the article, the fixity declarations ought to be as e.g.

infixl 8 +'
infix  8 <'
infix  7 :=
infixl 6 ;              -- infixr is good too

(p; q) s = q (p s)      -- (;)  = CB  -- flip (.)
(x:=f) s = x s (f s)    -- (:=) = S
goto f s = f s          -- id   = I
(f+'g) s = f s + g s
(f<'g) s = f s < g s
if' p c s = if (p s) then (c s) else s
x' (a,b) = a
i' (a,b) = b

so that ( x:=f ; i:=g ; x:=h )
would parse as ( ( (x:=f) ; (i:=g) ) ; (x:=h) ) , so that

( x:=f ; i:=g ; x:=h ) s = (x:=h) . (i:=g) . (x:=f) \$ s

The types involved would be as follows. 's' denotes state; (_:=_) denote state
transformers of type (s -> s) (for them to be composable). 'x' and 'f' in
(x:=f) are

f :: s -> v         -- value producer (including x' and i')
x :: s v -> s       -- state updater (with the new value)

so that the combination (x:=f) s = x s (f s) has 'f' produce a new value
and 'x' update its supplied state with it:

(x:=f) s = s' where v = f s; s' = x s v

The whole combination as presented in the article actually defines a new
function to perform all these activities, and would be defined as

prog = x := const 1 ; i := const 0 ; loop
where loop = x := f ; i := i' +' const 1 ;
if' (i' <' const 10) (goto loop)

and used as

prog initState where initState = (0,0)

or something like that. Which is all very reminiscent of monad, a "programmable
semicolon".

Cheers,

```