pi/src/Ctx.idr

41 lines
1.0 KiB
Idris

module Ctx
import Misc
import Data.Nat
%default total
infixr 7 ::
public export
data Ctx : (Index -> Type) -> Index -> Type where
Nil : Ctx ty 0
(::) : {n : _} -> ty n -> Ctx ty n -> Ctx ty (S n)
-- indexed by amount of free variables
public export
data Closure : (Index -> Type) -> Index -> Type where
MkClosure : {n : _} -> Ctx ty n -> ty (m + n) -> Closure ty m
public export
enclose : {n : _} -> ty n -> Closure ty n
enclose {n = n} ty = MkClosure Nil (rewrite plusZeroRightNeutral n in ty)
public export
lookup : {m : _} -> (n : Nat) -> Ctx ty m -> LT n m -> ty (minus m (S n))
lookup {m = S m} Z (x :: _) _ = rewrite minusZeroRight m in x
lookup (S n) (_ :: ctx) (LTESucc p) = lookup n ctx p
public export
strengthen : Ctx ty (S n) -> Ctx ty n
strengthen (_ :: ctx) = ctx
public export
strengthenTo : {n,m : _} -> Ctx ty m -> LTE n m -> Ctx ty n
strengthenTo {n = Z} _ _ = Nil
strengthenTo (x :: ctx) (LTESucc p)
= case lteSplit p of
Left Refl => x :: ctx
Right p2 => strengthenTo ctx p2