Last active
April 8, 2016 12:02
-
-
Save TerrorJack/beeafac91cc5ebd1f30c3c7e6bb226f6 to your computer and use it in GitHub Desktop.
Typed open sum in Haskell, the naive approach.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# 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