[Haskell-beginners] Unwrap an expression?

Kyle Murphy orclev at gmail.com
Sun Sep 4 09:51:17 CEST 2011

You might be able to do something using Template Haskell, although that
isn't precisely the question you asked initially. Template Haskell allows
you to write Haskell code, that modifies the AST (more or less) during the
compilation process. Unfortunately my knowledge of TH is fairly limited, but
I suspect there should be a way to pass an abritraty expression into a TH
function, and use some combination of pattern matching and guard expressions
to return just the outermost function. Understand of course though because
TH happens at "compile time" and not runtime, doing something like:

foo x = M (f (g... (n (x)) ...))

would in the compiled binary be translated into something like:

foo x = f

which really means aside from the novelty (and maybe learning experience) of
doing it, there's not much point as it's a lot simpler just to write:

foo x = f

in the first place.

-R. Kyle Murphy
Curiosity was framed, Ignorance killed the cat.

On Fri, Sep 2, 2011 at 20:19, Brent Yorgey <byorgey at seas.upenn.edu> wrote:

> On Fri, Sep 02, 2011 at 02:23:40PM -0800, Christopher Howard wrote:
> > Perhaps this is a dumb question, but I was pondering the whole "Lazy"
> > aspect of Haskell, and was curious: is there someway you could create
> > a function "M", which you could pass an expression into, so that...
> >
> > M (f (g... (n (x)) ...)) returns f
> >
> > and/or returns the inner value (g... (n (x)) ...)
> >
> > I don't know if that breaks the whole "referential transparency" idea
> > or not, but with all those lambda calculus tricks I've heard about,
> > and the lazy evaluation, I thought it would be worth asking.
> It's a good question.  You cannot do this, and you're right that it
> breaks referential transparency.  For example, suppose we have
>  f x = x + 3
> Now using our mythical M function, we should be able to do
>  M (f 6)
> and get f (or 6) as the result.  But f 6 = 9 so we should be able to
> replace it, like so:
>  M 9
> but what is M 9? An error? In general, anything that works by
> inspecting the *syntax* of expressions will break referential
> transparency.
> Even more simply, however, you might also want to consider: what would
> the type of M be?
> -Brent
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110904/b4f36fdd/attachment.htm>

More information about the Beginners mailing list