[Haskell-beginners] Trying to understand type families.

Brandon Allbery allbery.b at gmail.com
Sat Aug 20 00:43:27 CEST 2011


On Fri, Aug 19, 2011 at 18:27, Michael Litchard <michael at schmong.org> wrote:

> instance GMapKey () where
>  data GMap () v           = GMapUnit (Maybe v)
>  empty                    = GMapUnit Nothing
>  lookup () (GMapUnit v)   = v
>  insert () v (GMapUnit _) = GMapUnit $ Just v
>
> Could someone explain what the () is doing?
>

Nothing?

It's a dummy example using Haskell's "unit type" () (a type with only one
non-bottom value, namely () itself; you can think of it as a nullary tuple
type).  In this case, it's being declared as an instance of GMapKey, so it
is used where a GMapKey is needed; that means the key type parameter of the
GMap type and the key value parameter of the lookup and insert functions.
 Since there's only one value, the corresponding map type GMapUnit is a
Maybe instead of a list or tree of some kind, since the single possible key
value is either present or absent.  This lets the example focus on the
instance machinery without complicating it with nontrivial lookup and insert
operations.

-- 
brandon s allbery                                      allbery.b at gmail.com
wandering unix systems administrator (available)     (412) 475-9364 vm/sms
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110819/8c409a2c/attachment.htm>


More information about the Beginners mailing list