[<<][meta][>>][..]
Sat Oct 15 12:18:10 EDT 2011

Bug: computations run twice

test3 :: Term Tint -> Term Tfloat -> Term Tfloat -> MTerm (Term Tfloat, Term Tfloat)
test3 _ a b = do
  s <- fadd a b
  p <- fmul a b
  return (a, b)

*ExprM> compile test3
in:  [i.in0,f.in1,f.in2]
out: [f.in1,f.in2]
env:
f.%0 <- f.add f.in1 f.in2
f.%1 <- f.mul f.in1 f.in2
f.%2 <- f.add f.in1 f.in2
f.%3 <- f.mul f.in1 f.in2


Looks like this is becasue of the double use of m_t_ts in:

instance (Monad m, TermApp m (m os))
         => TermApp m (m (Term t, os)) where
  termApp _ m_t_ts = ([], mss) where
    mss = do
      (Term s) <- liftM fst m_t_ts
      ss <- snd $ termApp [] (liftM snd m_t_ts)
      return (s : ss)

Indeed.  The following solved it:

instance (Monad m, TermApp m (m os))
         => TermApp m (m (Term t, os)) where
  termApp _ m_t_ts = ([], mss) where
    mss = do
      ((Term s), ts) <- m_t_ts
      ss <- snd $ termApp [] (return ts :: m os)
      return (s : ss)


I'm starting to get this "monad is a computation" idea.  In the bug
above it was quite obvious that m_t_ts got "executed" twice.



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