open import 1Lab.Reflection hiding (reverse)
open import 1Lab.Type
open import Data.Dec.Base
open import Data.Fin.Base
module 1Lab.Reflection.Variables where
data Env {β} (A : Type β) : Nat β Type β where
[] : Env A zero
_β·_ : β {n} β Env A n β A β Env A (suc n)
record Variables {a} (A : Type a) : Type a where
constructor mk-variables
field
{nvars} : Nat
bound : Env A nvars
variables : Term β Maybe Term
open Variables
private variable
a b : Level
A : Type a
n : Nat
empty-vars : Variables A
empty-vars = mk-variables [] (Ξ» _ β nothing)
private
bind : Term β Term β (Term β Maybe Term) β Term β Maybe Term
bind tm tm-var lookup tm' with lookup tm' | tm β‘? tm'
... | just tm'-var | _ = just tm'-var
... | nothing | yes _ = just tm-var
... | nothing | no _ = nothing
fin-term : Nat β Term
fin-term zero = con (quote fzero) (unknown hβ· [])
fin-term (suc n) = con (quote fsuc) (unknown hβ· fin-term n vβ· [])
env-rec : (Mot : Nat β Type b) β
(β {n} β Mot n β A β Mot (suc n)) β
Mot zero β
Env A n β Mot n
env-rec Mot _β·*_ []* [] = []*
env-rec Mot _β·*_ []* (xs β· x) = env-rec (Mot β suc) _β·*_ ([]* β·* x) xs
reverse : Env A n β Vec A n
reverse {A = A} env = env-rec (Vec A) (Ξ» xs x β x β· xs) [] env
bind-var : Variables A β Term β TC (Term Γ Variables A)
bind-var vs tm with variables vs tm
... | just v = do
pure (v , vs)
... | nothing = do
a β unquoteTC tm
let v = fin-term (nvars vs)
let vs' = mk-variables (bound vs β· a)
(bind tm v (variables vs))
pure (v , vs')
environment : Variables A β TC (Term Γ Term)
environment vs = do
env β quoteTC (reverse (bound vs))
size β quoteTC (nvars vs)
pure (size , env)