module AST where
import Data.List.Safe ((!!))
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Text (Text)
import Prelude hiding ((!!))
type Name = Text
type Index = Int
data TermInf
= Global Name
| Local Index
| Star
| Ann TermChk TermChk
| App TermChk TermInf
| Pi TermChk TermChk
deriving (Eq, Show)
data TermChk
= Inf TermInf
| Lam TermChk
deriving (Eq, Show)
data Value
= VStar
| VLam TermChk
| VPi TermChk TermChk
deriving (Eq, Show)
type Env = Map Name Value
empty :: Env
empty = Map.empty
type Ctx = [Value]
type Eval t = Env -> Ctx -> t -> Maybe Value
evalInf :: Eval TermInf
evalInf env ctx (Global n) = Map.lookup n env
evalInf env ctx (Local idx) = ctx !! idx
evalInf env ctx Star = Just VStar
evalInf env ctx (Ann t _) = evalChk env ctx t
evalInf env ctx (App f a) = do
a <- evalInf env ctx a
case evalChk env ctx f of
Just (VLam b) -> evalChk env (a : ctx) b
Just (VPi _ b) -> evalChk env (a : ctx) b
_ -> Nothing
evalInf env ctx (Pi dom t) = Just (VPi dom t)
evalChk :: Eval TermChk
evalChk env ctx (Inf term) = evalInf env ctx term
evalChk env ctx (Lam body) = Just (VLam body)