Skip to content

Instantly share code, notes, and snippets.

@maoe
Created March 29, 2011 17:14
Show Gist options
  • Save maoe/892787 to your computer and use it in GitHub Desktop.
Save maoe/892787 to your computer and use it in GitHub Desktop.
phantom typeの型推論
data Term t = Zero
| Succ (Term Int)
| Pred (Term Int)
| IsZero (Term Int)
| If (Term Bool) (Term Int) (Term Int)
-- eval :: Term t -> T
eval Zero = I 0
eval (Succ e) = I $ succ $ int $ eval e
eval (Pred e) = I $ pred $ int $ eval e
eval (IsZero e) = B $ int (eval e) == 0
eval (If b t e) = if bool (eval b) then eval t else eval e
data T = I { int :: Int }
| B { bool :: Bool }
deriving Show
{- eval (If b t e)の節で型エラー。evalの型を明示すれば型チェックは通る。
Couldn't match expected type `Int' with actual type `Bool'
Expected type: Term Int
Actual type: Term Bool
In the first argument of `eval', namely `b'
In the first argument of `bool', namely `(eval b)'
Failed, modules loaded: none.
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment