Skip to content

Instantly share code, notes, and snippets.

@abailly
Created May 25, 2018 16:24
Show Gist options
  • Save abailly/abd10120b269c70afc9ad3907a7ce7e6 to your computer and use it in GitHub Desktop.
Save abailly/abd10120b269c70afc9ad3907a7ce7e6 to your computer and use it in GitHub Desktop.
Singletons-like type to value association
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
import Protolude
data Tag = Foo | Bar
data RTag (t :: Tag) where
RFoo :: RTag 'Foo
RBar :: RTag 'Bar
data Wrapped (t :: Tag) =
Wrapped { innerType :: Inner t }
type family Inner (t :: Tag) :: * where
Inner 'Foo = Int
Inner 'Bar = Double
data Wrapper where
Wrapper :: forall sr . Wrapped sr -> RTag sr -> Wrapper
foo :: Wrapper -> IO ()
foo (Wrapper (Wrapped i) RFoo) = print (i :: Int)
foo (Wrapper (Wrapped d) RBar) = print (d :: Double)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment