Skip to content

Instantly share code, notes, and snippets.

@YuMingLiao
Created May 14, 2019 02:58
Show Gist options
  • Save YuMingLiao/d199732449b6f60472396f115c601f84 to your computer and use it in GitHub Desktop.
Save YuMingLiao/d199732449b6f60472396f115c601f84 to your computer and use it in GitHub Desktop.
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE UndecidableInstances #-}
data Nat = Z | S Nat
data Expr = Num Nat
| Add Expr Expr
type family Eval (expr :: Expr) :: Nat where
Eval ('Num n) = n
Eval ('Add e e) = Add (Eval e) (Eval e)
type family Add (x :: Nat) (y :: Nat) :: Nat where
Add 'Z y = y
Add ('S x) y = 'S (Add x y)
-- *Main> :kind! Eval ('Add ('Num ('S 'Z)) ('Num ('Z)))
-- Eval ('Add ('Num ('S 'Z)) ('Num ('Z))) :: Nat
-- = Eval ('Add ('Num ('S 'Z)) ('Num 'Z))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment