Skip to content

Instantly share code, notes, and snippets.

@clayrat
Last active March 4, 2021 00:54
Show Gist options
  • Save clayrat/780764244d801606d2d102e4d9b4343d to your computer and use it in GitHub Desktop.
Save clayrat/780764244d801606d2d102e4d9b4343d to your computer and use it in GitHub Desktop.
well-typed DeBrujin level
module DBLev
import Data.List.Elem
import Decidable.Equality
%default total
-- from https://github.com/agda/agda/blob/master/test/Succeed/Issue985.agda
data Ty = A
data LupLev : Nat -> (g : List Ty) -> (a : Ty) -> Elem a g -> Type where
LupZ : LupLev (length g) (a::g) a Here
LupS : LupLev x g a e -> LupLev x (b::g) a (There e)
Uninhabited (LupLev x [] t e) where
uninhabited LupZ impossible
record Lev (x : Nat) (g : List Ty) where
constructor MkLev
{ty : Ty}
{el : Elem ty g}
lev : LupLev x g ty el
Uninhabited (Lev x []) where
uninhabited (MkLev lev) = uninhabited lev
weakLev : Lev x g -> Lev x (a::g)
weakLev (MkLev lev) = MkLev $ LupS lev
lookupLev : (x : Nat) -> (g : List Ty) -> Dec (Lev x g)
lookupLev x [] = No uninhabited
lookupLev x (a::g) = case decEq x (length g) of
Yes Refl => Yes $ MkLev LupZ
No ctra => case lookupLev x g of
Yes l => Yes $ weakLev l
No ctra2 => No $ \(MkLev l) => case l of
LupZ => ctra Refl
LupS ll => ctra2 $ MkLev ll
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment