Original work at http://okmij.org/ftp/Haskell/extensible/OpenUnion1.hs. Open unions (type-indexed co-products) for extensible effects. This implementation relies on _closed_ overlapping instances (or closed type function overlapping soon to be added to GHC).
- data Union r v = forall t . (Functor t, Typeable1 t) => Union (t v)
- class Member t r => SetMember set t r | r set -> t
- class Member t r
- data a :> b
- inj :: (Functor t, Typeable1 t, Member t r) => t v -> Union r v
- prj :: (Typeable1 t, Member t r) => Union r v -> Maybe (t v)
- prjForce :: (Typeable1 t, Member t r) => Union r v -> (t v -> a) -> a
- decomp :: Typeable1 t => Union (t :> r) v -> Either (Union r v) (t v)
- unsafeReUnion :: Union r w -> Union t w
SetMember is similar to
Member, but it allows types to belong to a
"set". For every set, only one member can be in
r at any given time.
This allows us to specify exclusivity and uniqueness among arbitrary effects:
-- Terminal effects (effects which must be run last) data Terminal -- Make Lifts part of the Terminal effects set. -- The fundep assures that there can only be one Terminal effect for any r. instance Member (Lift m) r => SetMember Terminal (Lift m) r -- Only allow a single unique Lift effect, by making a "Lift" set. instance Member (Lift m) r => SetMember Lift (Lift m) r
A sum data type, for composing effects
Try extracting the contents of a Union as a given type.
Extract the contents of a Union as a given type. If the Union isn't of that type, a runtime error occurs.
Try extracting the contents of a Union as a given type. If we can't, return a reduced Union that excludes the type we just checked.