Implementing a type for partial values

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 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.

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.

2 Comments

  1. Michael Karcher:

    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.

  2. Conal Elliott » Blog Archive » Merging partial values:

    […] partial information about values, and wrote about it in two posts, A type for partial values and Implementing a type for partial values. Of particular interest is the ability to combine two partial values into one, combining the […]

Leave a comment