Skip to content

Instantly share code, notes, and snippets.

@pjan
Last active November 14, 2017 13:32
Show Gist options
  • Save pjan/0f255bb7df67318428df3c092ec26c75 to your computer and use it in GitHub Desktop.
Save pjan/0f255bb7df67318428df3c092ec26c75 to your computer and use it in GitHub Desktop.
Type to value reflection
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Data.Proxy
import Data.Type.Bool
-- Types
data One
data Add a b
-- Type Classes
class Eval x where
eval :: Proxy x -> Int
instance Eval One where
eval _ = 1
instance forall a b. (Eval a, Eval b) =>
Eval (Add a b) where
eval _ = eval (Proxy :: Proxy a) + eval (Proxy :: Proxy b)
-- Example
type Two = Add One One
two :: Int
two = eval (Proxy :: Proxy Two)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment