[Haskell-cafe] Embedded Functions in Algebraic Data Types?

Luke Palmer lrpalmer at gmail.com
Sun Feb 10 08:54:51 EST 2008

Quite frequently.

Here are a few examples from my own code:

For "functional references" (representing a bidirectional function
from a data type to a part of itself (for example the first element of
a pair)).

> data Accessor s a
>     = Accessor { get :: s -> a
>                , set :: a -> s -> s
>                }

My quantum computation arrow (really in the realm of "concrete, useful
things", huh? :-)

> data Operator b c
>     = Op (forall d. QStateVec (b,d) -> IO (QStateVec (c,d)))
>     | ...

The ubiquitous FRP Behavior, comprising a current value and a function
which takes a timestep and returns the next value.

> data Behavior a
>    = Behavior a (Double -> Behavior a)

The "suspend" monad, representing a computation which can either
finish now with a value of type a, or suspends to request a value of
type v before continuing.

> data Suspend v a
>     = Return a
>     | Suspend (v -> Suspend v a)

It seems that most frequently, functions in ADTs are used when
implementing monads or arrows, but that doens't need to be the case.
A lot of times a function is the best way to represent a particular
part of a data structure :-)


On Feb 10, 2008 1:34 PM, Michael Feathers <mfeathers at mindspring.com> wrote:
> On a lark, I loaded this into Hugs this morning, and it didn't complain:
> data Thing = Thing (Integer -> Integer)
> But, I've never seen that sort of construct in an example.  Do people
> ever embed functions in ADTs?
> Michael
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

More information about the Haskell-Cafe mailing list