Last active
November 14, 2017 13:32
-
-
Save pjan/0f255bb7df67318428df3c092ec26c75 to your computer and use it in GitHub Desktop.
Type to value reflection
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 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