[<<][meta][>>][..]
Sat Oct 29 09:25:38 EDT 2011

Inserting Lambda binding

I'm pretty sure that this is the way to insert an outer binding:

insertContext :: (Term -> Term) -> Code t -> MCode (Code t)
insertContext outer inner = SC c where
  c s k = outer (k s inner)

but I'm confused as how to use it.  Esentially (insertContext context)
has the same type as return, so it should be possible to use it in
place of return.

I get complaints about return type: 
    Expected type: MCode (Code (as -> MCode t))
      Actual type: MCode (Code t)

-          in do
-           (Code bodyTerm) <- f argTerms
-           return $ Code $ Lambda argVars bodyTerm
+          binder = Lambda argVars
+          body = f argTerms
+         in do
+          body' <- body
+          insertContext binder body'
+
+

Maybe need to unpack/pack to change the type?

Now this has still the same problem because of the order of the bind
operations:

  _lambda = lambda where
    lambda :: forall as ras t. Args Term Code as ras => 
              (ras -> MCode (Code t)) -> MCode (Code (as -> MCode t))
    lambda f = term where
      n = treeSize (undefined :: ras)
      term :: MCode (Code (as -> MCode t))
      term = do
        varNames <- varList n
        let 
          (argTerms, []) = treeTerms varNames (undefined :: ras)
          argVars  = map (\(Ref v) -> v) (treeVars argTerms)
          binder = Lambda argVars
          body = f argTerms
         in do
          body' <- body
          -- Need to unpack, repack the Code wrapping to change the
          -- phantom type from (Code t) to (Code (as -> MCode t)).
          (Code inner) <- insertContext binder body'
          return $ Code $ inner

Maybe insert context needs to be just a side effect?
That seems to do it:

-- insertContext :: (Term -> Term) -> MCode ()
insertContext outer = SC c where
  c s k = outer (k s ())


instance Loop Term MCode' Code where
  _lambda = lambda where
    lambda :: forall as ras t. Args Term Code as ras => 
              (ras -> MCode (Code t)) -> MCode (Code (as -> MCode t))
    lambda f = term where
      n = treeSize (undefined :: ras)
      term :: MCode (Code (as -> MCode t))
      term = do
        varNames <- varList n
        let 
          (argTerms, []) = treeTerms varNames (undefined :: ras)
          argVars  = map (\(Ref v) -> v) (treeVars argTerms)
          binder = Lambda argVars
          body = f argTerms
         in do
          insertContext binder 
          -- Need to unpack, repack the Code wrapping to change the
          -- phantom type from (Code t) to (Code (as -> MCode t)).
          (Code body') <- body
          return $ Code $ body'


f6 = _lambda $ \a -> do
  square <- mul a a
  _ret square


t10 = termCompile (f6 :: C1)

> t10
Lambda [Var i "t0" 0] 
  (Let (Var i "t1" 0) 
      (Op i "mul" [Ref (Var i "t0" 0),
                   Ref (Var i "t0" 0)])
   (Ret [Var i "t1" 0]))



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