Sunday, July 01, 2007

Implementing a type for partial values

(Edit 2008-01-14: updated formatting.)

In my previous post, I gave an interface for a type of "partial values" and invited implementations. Here's mine, which surprised me in its simplicity. The whole module is online.

The trick is to represent Partial a as a -> a, i.e., a way to selectively replace parts of a value. Typically the replaced parts are bottom/undefined. I want Partial a to be a monoid, and Data.Monoid provides an instance:

-- | The monoid of endomorphisms under composition.
newtype Endo a = Endo { appEndo :: a -> a }

instance Monoid (Endo a) where
 mempty = Endo id
 Endo f `mappend` Endo g = Endo (f . g)

So my definition is simply a synonym:

type Partial = Endo

Note that mempty and mappend do exactly what I want. mempty adds no information at all, while u `mappend` v adds (overrides) information with u and then with v.

The implementation of functions on Partial is trivial.

-- | Convenience for partial-manipulating functions
inPartial :: ((a->a) -> (a'->a')) -> (Partial a -> Partial a')
inPartial f = Endo . f . appEndo

valp :: c -> Partial c
valp c = Endo (const c)

pval :: Partial c -> c
pval (Endo f) = f undefined
unFst :: Partial a -> Partial (a,b)
unFst = inPartial first

unSnd :: Partial b -> Partial (a,b)
unSnd = inPartial second

unElt :: Functor f => Partial a -> Partial (f a)
unElt = inPartial fmap

I'm not sure I'll end up using the Partial type in Eros, but I like having it around. If you think of variations, extensions and/or other uses, please let me know.

1 comment:

Michael Karcher said...

My first attempt on trying to implement partial values was

type Partial a = [a->a]

After reading the beginning of your solution post, it was obvious that putting the functions first into a list to fold it later is unneeded. Then I first wrote my own "Endo" instance, and got it to work too. When refactoring the code to the presented version, I noted, that with the "Endo" Monoid instance, mappend show exactly the opposite behaviour than the requested one: The left argument replaces info from the right one, and not vice versa.