pi/src/Core/Term.idr

128 lines
4.9 KiB
Idris
Raw Normal View History

2022-07-23 03:38:15 +02:00
module Core.Term
2022-04-23 15:18:06 +02:00
2022-07-21 00:05:45 +02:00
import Data.Fin
2022-04-23 15:18:06 +02:00
2022-07-23 03:38:15 +02:00
import Core.Misc
2022-04-23 15:18:06 +02:00
%default total
{-
The type of terms is indexed by the size of the environment in which
they are valid, that is, it is impossible to construct an ill-scoped term.
2022-07-26 23:07:13 +02:00
Defs are used for performance reasons and are not implemented in a type safe manner.
if a def is not in scope the checker will scream at you.
2022-04-23 15:18:06 +02:00
-}
public export
data Term : (_ : Index) -> Type where
2022-07-21 00:05:45 +02:00
TType : Term n -- Type of types
TTop : Term n -- Unit type
TStar : Term n -- Unit term
TTopInd : Term n -> Term n -> Term n -- : (x : ) → C x
TBot : Term n -- Empty type
TBotInd : Term n -> Term n -- : (x : ⊥) → C x
TNat : Term n --
TZero : Term n -- 0
TSuc : Term n -> Term n -- successor
TNatInd : Term n -> Term n -> Term n -> Term n -- : (x : ) → C x
2022-07-21 04:18:50 +02:00
TSigma : Term n -> Term n -> Term n -- Sum type (Σ _ : A, B _)
TPair : Term n -> Term n -> Term n -- Sum constructor _,_
TSigInd : Term n -> Term n -> Term n -> Term n -> Term n -- A B C f : (x : Σ _ : A , B _) → C x
TId : Term n -> Term n -> Term n -> Term n -- Id Type (Id A x y)
TRefl : Term n -> Term n -> Term n -- Refl A x
TJ : Term n -> Term n -> Term n -> Term n -> Term n -> Term n -- A a b C d : (p : Id A a b) → C p
2022-07-21 19:51:55 +02:00
TLet : Term n -> Term n -> Term (S n) -> Term n -- let _ : #0 := #1 in #2
2022-07-21 00:05:45 +02:00
TLam : Term (S n) -> Term n -- Lambda abstraction (λ _ . Scope)
TPi : Term n -> Term (S n) -> Term n -- Pi type (∏ _ : A . B _ )
TApp : Term n -> Term n -> Term n -- Appliction
2022-07-26 23:07:13 +02:00
2022-07-21 04:18:50 +02:00
TVar : Fin n -> Term n -- Variable
2022-07-21 00:05:45 +02:00
2022-07-26 23:07:13 +02:00
TDef : Int -> Term n -- Def Variable
2022-07-21 04:18:50 +02:00
infixl 3 `TApp`
2022-05-13 19:46:05 +02:00
public export
Show (Term n) where
2022-07-21 00:05:45 +02:00
show TType = "TType"
show TTop = ""
show TStar = ""
show (TTopInd c st) = "-ind (" ++ show c ++ ") (" ++ show st ++ ")"
show TBot = ""
show (TBotInd c) = "⊥-ind (" ++ show c ++ ")"
show TNat = ""
show TZero = "0"
show (TSuc n) = "suc (" ++ show n ++ ")"
show (TNatInd c z s) = "-ind (" ++ show c ++ ") (" ++ show z ++ ") (" ++ show s ++ ")"
2022-07-21 19:51:55 +02:00
show (TSigma a b) = "Σ (" ++ show a ++ ") (" ++ show b ++ ")"
show (TPair a b) = "Pair (" ++ show a ++ ") (" ++ show b ++ ")"
2022-07-21 04:18:50 +02:00
show (TSigInd a b c f) = "Σ-ind (" ++ show a ++ ") (" ++ show b ++ ") (" ++ show c ++ ") (" ++ show f ++ ")"
show (TId ty x y) = "Id (" ++ show ty ++ ") (" ++ show x ++ ") (" ++ show y ++ ")"
show (TRefl ty tr) = "Refl (" ++ show ty ++ ") (" ++ show tr ++ ")"
show (TJ ty a b c d) = "J (" ++ show ty ++ ") (" ++ show a ++ ") (" ++ show b ++ ") ("
++ show c ++ ") (" ++ show d ++ ")"
2022-07-21 19:51:55 +02:00
show (TLet ty tr itr) = "let : (" ++ show ty ++ ") := (" ++ show tr ++ ") in (" ++ show itr ++ ")"
2022-07-21 04:18:50 +02:00
2022-07-21 19:51:55 +02:00
show (TLam sc) = "λ (" ++ show sc ++ ")"
show (TPi ty sc) = "Π (" ++ show ty ++ ") (" ++ show sc ++ ")"
2022-07-21 00:05:45 +02:00
2022-07-21 19:51:55 +02:00
show (TApp f x) = "(" ++ show f ++ ") TApp (" ++ show x ++ ")"
show (TVar i) = "Var " ++ show i
2022-07-26 23:07:13 +02:00
show (TDef i) = "Def " ++ show i
2022-07-21 00:05:45 +02:00
public export
weakTr : Term n -> Term (S n)
weakTr = go 0
where
go : {0 n : Nat} -> Fin (S n) -> Term n -> Term (S n)
2022-07-21 04:18:50 +02:00
go n TType = TType
go n TTop = TTop
go n TStar = TTop
go n (TTopInd c st) = TTopInd (go n c) (go n st)
go n TBot = TBot
go n (TBotInd c) = TBotInd (go n c)
go n TNat = TNat
go n TZero = TZero
go n (TSuc m) = TSuc (go n m)
go n (TNatInd c z s) = TNatInd (go n c) (go n z) (go n s)
go n (TSigma a b) = TSigma (go n a) (go n b)
go n (TPair a b) = TPair (go n a) (go n b)
go n (TSigInd a b c f) = TSigInd (go n a) (go n b) (go n c) (go n f)
go n (TId ty a b) = TId (go n ty) (go n a) (go n b)
go n (TRefl ty tr) = TRefl (go n ty) (go n tr)
go n (TJ ty a b c d) = TJ (go n ty) (go n a) (go n b) (go n c) (go n d)
2022-07-21 19:51:55 +02:00
go n (TLet ty tr itr) = TLet (go n ty) (go n tr) (go (FS n) itr)
2022-07-21 04:18:50 +02:00
go n (TLam sc) = TLam (go (FS n) sc)
go n (TPi ty sc) = TPi (go n ty) (go (FS n) sc)
go n (TApp f x) = TApp (go n f) (go n x)
go n (TVar i) = if weaken i < n
then TVar (weaken i)
else TVar (FS i)
2022-07-26 23:07:13 +02:00
go n (TDef i) = TDef i
2022-07-21 00:05:45 +02:00
public export
weakTr2 : Term n -> Term (2+n)
weakTr2 = weakTr . weakTr
public export
weakTr3 : Term n -> Term (3+n)
weakTr3 = weakTr . weakTr2
public export
weakTr4 : Term n -> Term (4+n)
weakTr4 = weakTr2 . weakTr2