[<<][rtl][>>][..]
Sun May 27 08:38:41 EDT 2018

Free Monad

The above smells like the Free Monad.

Free Term Node

See http://hackage.haskell.org/package/free-5.0.2/docs/Control-Monad-Free.html


So inlining is some form of fmap

Here's what worked.  I had to really do it step by step to figure out
the types of holes.

inline :: (Int -> Term Node) -> Int -> Exp Node
inline ref = inl where
  inl :: Int -> Exp Node
  inl n = f $ ref n where
    f (Delay _) = Pure n
    f term = Free $ fmap inl term

Parameterized node type

-- Inlining, terminated on Delay to avoid cycles.
inline :: (t -> Term t) -> t -> Exp t
inline ref = inl where
  inl n = f $ ref n where
    f (Delay _) = Pure n
    f term = Free $ fmap inl term


Generalized to predicate

-- Inlining, terminated on Delay to avoid cycles.
inline' :: (Term t -> Bool) -> (t -> Term t) -> t -> Exp t
inline' p ref = inl where
  inl n = f $ ref n where
    f term = case p term of
      False -> Pure n
      True  -> Free $ fmap inl term

inline = inline' p where
  p (Delay _) = False
  p _ = True


Now I want to extend this to keep track of whether a node was inlined
or not.  But first, can this be generalized further?

Is there another case where a Bool is used to pick two alternatives?
Yes that's just if.

inline' :: (Term t -> Bool) -> (t -> Term t) -> t -> Exp t
inline' p ref = inl where
  inl n = f $ ref n where
    f term = if' (p term) (Free $ fmap inl term) (Pure n)

EDIT: Continued a bit.  Straightforward in the end.




[Reply][About]
[<<][rtl][>>][..]