Hi,

I've been working on this problem for some time and I was hoping for some
feedback. I'm trying to implement an embedded language that can thread a
global structure through the evaluation of a term.

A mini version of this language only includes three functions, lam, app and
ext (lambda, apply and extension respectively). The only difference between
this and other embedded languages is that terms embedded in an extension
have access to a global variable that is invisible to the terms themselves.

The writer of a term in this language can only access the global variable
by applying an extension. The writer extensions insures that the effect of
evaluation on the global variable is commutative and insures that the
effect of the global variable on terms is constant regardless of the order
of evaluation.

For example the global variable could be a map with some sort of lazy
processing. An extension that queries the map for an element might cause a
change in the state of the map and return some information about an
element, but as long as the change in state are commutative and the same
information is returned about elements regardless of the order of
evaluation then the concept is safe.

My motivation for experimenting with such a language comes from a belief
that certain complicated programs would be easier to express in such a
language and that there might be very useful code transformations that
could eventually be applied to such a language that would be possible
because of the constraints on the behavior of extensions.

However I have yet to properly implement a language in Haskell and so I
decided to post what I have and look for comments.

Basically the language takes a common form of embedded languages; a lambda
expression such as

\x y->x y

would have the form:

lam (\x -> lam (\y -> app x y))

however in order to evaluate an expression the need to also apply it to a
global term:

lam (\x -> lam (\y -> app x y)) global

Ideally the result of evaluating this would be a (global', \x y-> x y), a
modified version of global paired with the term. So the type of the term:

lam (\x -> lam (\y -> app x y)) is something like g -> (g, (a -> b) -> a ->
b) where g is the type of global.

>From that came the first idea of the types of the evaluator functions:

lam :: ( (g->(g,a)) -> (g->(g,b)) )-> (g->(g, a->b))
app :: (g->(g,a->b)) -> (g->(g, a)) -> (g->(g,b))

with some additional parenthesis for sake of clarity. Each sub-term has the
type g->(g,t)

any function can be included as an extension as long as the function has
the type:
(g->(g,a)) -> (g->(g,b))

This seems to work well except that it seems to be impossible (as far as
I've tried) to construct a definition for the lam function that both fits
this type and properly passes the global through the entire evaluation. For
example it's easy to define app like this:

app :: (g->(g,a->b)) -> (g->(g,a)) -> (g->(g,b))
app = (\ eA eB -> \ g0 -> let     (gB, fB) = eB g0
                                     in let (gA, fA) = eA gB
                                     in     (gA, fA fB) )

but so far the definition of lam has eluded me. This is the closest I've
come:

lam :: ((g->(g,a)) -> (g->(g,b))) -> (g->(g,a->b))
lam = (\ f -> \ g0 -> (g0, \x -> snd $ (f (\ gv->(gv,x))) g0 ))

This fits the type but I fear this definition does not return the properly
modified version of global.

I tried some other iterations of this idea. In one effort the first
argument of a function is extracted into the type of a term. So a term of
type a->b has the type (g->a->(g,b)). And a term of type a has the type
(g->((g, a)) such that:

lam2 :: ((g->(g,a)) -> (g->(g,b))) -> (g->a->(g,b))
lam2 = (\ f -> \ g x -> (f (\ gv -> (gv, x))) g)

app2 :: (g->a->(g,b)) -> (g->(g,a)) -> (g->(g,b))
app2 = (\ eA eB -> \ g0 -> let (gB, fB) = eB g0
                                       in  eA gB fB )

This seems like a step in the right direction unfortunately because the
return type of lam is now (g->a->(g,b)) and not (g->(g->c)) we cannot type
the term lam (\x -> lam (\y -> app x y)). In other words this language can
only express unary and nullary functions.
Just to help clarify this for myself I created to addition functions
lam2Helper and app2Helper:

lam2Helper :: (g->a->(g,b)) -> (g->(g,a->b))
lam2Helper = (\f -> \g -> (g, \a -> snd $ f g a))
app2Helper :: (g->(g,a->b)) -> (g->a->(g,b))
app2Helper = (\f -> \g a-> let (g1, f1) = f g
                           in (g1, f1 a))

these allow me to create the term:
lam2 (\x -> lam2Helper $ lam2 (\y -> app2 (app2Helper x) y))

but just like the original definition of lam from my first try, there is no
way to construct lam2Helper without improperly passing the global as I've
done in my attempt.

Finally on my third try I attempted to make every term have type
(g->a->(g,b) by allowing embedded nullary functions to have the type
(g->()->(g,t))

lam3 :: ((g->()->(g,a)) -> (g->()->(g,b))) -> (g->a->(g,b))
lam3 = (\ f -> \ g x -> (f (\ gv () -> (gv, x))) g () )
app3 :: (g->a->(g,b)) -> (g->()->(g,a)) -> (g->()->(g,b))
app3 = (\ eA eB -> \ gz () -> let (gB, fB) = eB gz ()
                              in  eA gB fB )

This allows me to construct the term:
lam (\x -> lam (\y -> app x y))
but only by throwing out important type information...

A compilable example of this is available here:
http://hpaste.org/86273

And with source candy here:
http://hpaste.org/86274

My goal at this point is just to understand the problem better. I feel I'm
at the edge of my understanding of type systems and arity and I'm unsure if
what I want to do is possible in Haskell. My goal would eventually be to
construct a working prototype of such a language and to construct a proof
that it maintains the types of terms and that the global variable is
evaluated once by every subterm.

I appreciate any insight, interest or help.

Thanks,
Ian Bloom
ianmbl...@gmail.com
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to