Skip to content

Instantly share code, notes, and snippets.

@TerrorJack
Last active April 8, 2016 12:02
Show Gist options
  • Select an option

  • Save TerrorJack/beeafac91cc5ebd1f30c3c7e6bb226f6 to your computer and use it in GitHub Desktop.

Select an option

Save TerrorJack/beeafac91cc5ebd1f30c3c7e6bb226f6 to your computer and use it in GitHub Desktop.
Typed open sum in Haskell, the naive approach.
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeOperators #-}
module Data.OpenSum where
import Data.Dynamic
newtype Sum (l :: [*]) = Sum { unSum :: Dynamic }
deriving Typeable
class Member (l :: [*]) t
instance Member (t ': l) t
instance {-# OVERLAPPABLE #-} Member l t => Member (nt ': l) t
inj :: (Typeable t, Member l t) => t -> Sum l
inj = Sum . toDyn
prj :: (Typeable t, Member l t) => Sum l -> Maybe t
prj = fromDynamic . unSum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment