Guest User

Untitled

a guest
May 26th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. {-# LANGUAGE LambdaCase, DeriveFunctor #-}
  2. module LambdaPi where
  3. import Bound
  4. import Control.Applicative
  5. import Control.Monad
  6. import Control.Monad.Gen
  7. import Control.Monad.Reader
  8. import qualified Data.Map as M
  9. import Data.Maybe
  10. import Prelude.Extras
  11.  
  12. data Expr a = Var a
  13. | App (Expr a) (Expr a)
  14. | Annot (Expr a) (Expr a)
  15. | ETrue
  16. | EFalse
  17. | Bool
  18. | Star
  19. | Pi (Expr a) (Scope () Expr a)
  20. | Lam (Scope () Expr a)
  21. deriving(Functor, Eq)
  22.  
  23. instance Eq1 Expr where (==#) = (==)
  24. instance Applicative Expr where
  25. pure = return
  26. (<*>) = ap
  27. instance Monad Expr where
  28. return = Var
  29. Var a >>= f = f a
  30. (App l r) >>= f = App (l >>= f) (r >>= f)
  31. ETrue >>= _ = ETrue
  32. EFalse >>= _ = EFalse
  33. Bool >>= _ = Bool
  34. Star >>= _ = Star
  35. Annot l r >>= f = Annot (l >>= f) (r >>= f)
  36. Pi l s >>= f = Pi (l >>= f) (s >>>= f)
  37. Lam e >>= f = Lam (e >>>= f)
  38.  
  39. type Val = Expr -- Represents normalized expressions
  40.  
  41. nf :: Expr a -> Val a
  42. nf = \case
  43. (Annot e t) -> Annot (nf e) (nf t)
  44. (Lam e) -> Lam (toScope . nf . fromScope $ e)
  45. (Pi l r) -> Pi (nf l) (toScope . nf . fromScope $ r)
  46. (App l r) ->
  47. case l of
  48. Lam f -> nf (instantiate1 r f)
  49. l' -> App l' (nf r)
  50. e -> e
  51.  
  52. type Env = M.Map Int (Val Int)
  53. type TyM = ReaderT Env (GenT Int Maybe)
  54.  
  55. unbind :: (MonadGen a m, Functor m, Monad f) => Scope () f a -> m (a, f a)
  56. unbind scope = ((,) <*> flip instantiate1 scope . return) <$> gen
  57.  
  58. unbindWith :: Monad f => a -> Scope () f a -> f a
  59. unbindWith = instantiate1 . return
  60.  
  61. inferType :: Expr Int -> TyM (Val Int)
  62. inferType (Var i) = asks (M.lookup i) >>= maybe mzero return
  63. inferType ETrue = return Bool
  64. inferType EFalse = return Bool
  65. inferType Bool = return Star
  66. inferType Star = return Star
  67. inferType (Lam _) = mzero -- We can only check lambdas
  68. inferType (Annot e ty) = do
  69. checkType ty Star
  70. let v = nf ty
  71. v <$ checkType e v
  72. inferType (App f a) = do
  73. ty <- inferType f
  74. case ty of
  75. Pi aTy body -> nf (App (Lam body) a) <$ checkType a aTy
  76. _ -> mzero
  77. inferType (Pi t s) = do
  78. checkType t Star
  79. (newVar, s') <- unbind s
  80. local (M.insert newVar $ nf t) $
  81. Star <$ checkType s' Star
  82.  
  83. checkType :: Expr Int -> Val Int -> TyM ()
  84. checkType (Lam s) (Pi t ts) = do
  85. (newVar, s') <- unbind s
  86. local (M.insert newVar (nf t)) $
  87. checkType s' (nf $ unbindWith newVar ts)
  88. checkType e t = inferType e >>= guard . (== t)
  89.  
  90. lam :: Eq a => a -> Expr a -> Expr a
  91. lam a = Lam . abstract1 a
  92.  
  93. pit :: Eq a => a -> Expr a -> Expr a -> Expr a
  94. pit v t = Pi t . abstract1 v
  95.  
  96. typecheck :: Expr Int -> Expr Int -> Bool
  97. typecheck e = isJust
  98. . runGenT
  99. . flip runReaderT M.empty
  100. . checkType e
Add Comment
Please, Sign In to add comment