perga/app/Check.hs

38 lines
932 B
Haskell
Raw Normal View History

2024-11-11 13:52:50 -08:00
-- {-# LANGUAGE BangPatterns #-}
2024-10-05 13:31:09 -07:00
module Check where
import Control.Monad (guard)
2024-11-11 13:37:44 -08:00
import Data.List ((!?))
2024-11-11 13:52:50 -08:00
-- import Debug.Trace (trace)
2024-11-11 13:37:44 -08:00
import Expr
2024-10-05 13:31:09 -07:00
type Context = [Expr]
findType :: Context -> Expr -> Maybe Expr
findType g (Var k) = do
2024-11-11 13:37:44 -08:00
t <- g !? fromInteger k
kind <- findType g t
guard $ isSort kind
pure t
2024-10-05 13:31:09 -07:00
findType _ Star = Just Square
findType _ Square = Nothing
findType g (App m n) = do
2024-11-11 13:37:44 -08:00
Pi a b <- findType g m
a' <- findType g n
guard $ betaEquiv a a'
pure $ subst n b
2024-10-05 13:31:09 -07:00
findType g (Abs t v) = do
2024-11-11 13:37:44 -08:00
argType <- findType g t
guard $ isSort argType
bodyType <- findType (incIndices t : map incIndices g) v
resType <- findType g (Pi t bodyType)
guard $ isSort resType
pure $ Pi t bodyType
2024-10-05 13:31:09 -07:00
findType g (Pi t v) = do
2024-11-11 13:37:44 -08:00
argType <- findType g t
guard $ isSort argType
bodyType <- findType (incIndices t : map incIndices g) v
guard $ isSort bodyType
pure bodyType