[<<][meta][>>][..]
Wed Jan 19 20:09:27 EST 2011

Onward: sharing + type classes

Leaving the SC monad as it is:

data SC s r c = SC {runSC :: (s -> (s -> c -> r) -> r)}
instance Monad (SC s r) where
    return x = SC (\s k -> k s x)
    (SC a) >>= f = SC (\s k -> a s (\s' x -> (runSC (f x)) s' k))

and defining explicit sharing ops:

returnN = SC . varShare
bindN a f = a >>= (\x -> ((returnN x) >>= f))
varAlloc (VState n) = 
    (n, VState (n+1))
varShare x s k =
    Let n x x' where
            x' = k s' (Var n)
            (n, s') = varAlloc s

we have essentially all that's necessary:

test y = do
  x <- returnN y
  return (x * x)

showSC $ test $ Lit 1
=> Let 0 (Lit 1) (Mul (Var 0) (Var 0))

Composition also works fine:

showSC $ (test (Lit 1)) >>= test
=> Let 0 (Lit 1) (Let 1 (Mul (Var 0) (Var 0)) (Mul (Var 1) (Var 1)))

The next step is to make the returnN (return Named) operation more
general so that the SC monad can also work on other values, especially
Num classes.

Maybe read the ``Finally, Tagless, ...'' paper again[1].


[1] http://www.cs.rutgers.edu/~ccshan/tagless/jfp.pdf



[Reply][About]
[<<][meta][>>][..]