2021-07-20 16:05:19 -04:00
|
|
|
module Quox.Syntax.Var
|
|
|
|
|
|
|
|
import Quox.Name
|
2022-02-26 20:17:42 -05:00
|
|
|
import Quox.Pretty
|
|
|
|
import Quox.OPE
|
2021-07-20 16:05:19 -04:00
|
|
|
|
|
|
|
import Data.Nat
|
|
|
|
import Data.List
|
2022-02-26 20:17:42 -05:00
|
|
|
import Decidable.Equality
|
|
|
|
import Data.Bool.Decidable
|
2021-07-20 16:05:19 -04:00
|
|
|
|
|
|
|
%default total
|
|
|
|
|
|
|
|
|
|
|
|
public export
|
|
|
|
data Var : Nat -> Type where
|
|
|
|
VZ : Var (S n)
|
|
|
|
VS : Var n -> Var (S n)
|
|
|
|
%name Var i, j
|
|
|
|
%builtin Natural Var
|
|
|
|
|
|
|
|
public export
|
|
|
|
(.nat) : Var n -> Nat
|
|
|
|
(VZ).nat = 0
|
|
|
|
(VS i).nat = S i.nat
|
|
|
|
%transform "Var.(.nat)" Var.(.nat) i = believe_me i
|
|
|
|
|
2022-02-26 19:47:55 -05:00
|
|
|
public export %inline Cast (Var n) Nat where cast = (.nat)
|
|
|
|
public export %inline Cast (Var n) Integer where cast = cast . cast {to = Nat}
|
2021-07-20 16:05:19 -04:00
|
|
|
|
2022-02-26 19:47:55 -05:00
|
|
|
export %inline Eq (Var n) where i == j = i.nat == j.nat
|
|
|
|
export %inline Ord (Var n) where compare i j = compare i.nat j.nat
|
|
|
|
export %inline Show (Var n) where showPrec d i = showCon d "V" $ showArg i.nat
|
2021-07-20 16:05:19 -04:00
|
|
|
|
2022-02-26 20:17:42 -05:00
|
|
|
public export %inline Injective VS where injective Refl = Refl
|
|
|
|
|
2021-07-20 16:05:19 -04:00
|
|
|
|
2021-09-03 09:00:16 -04:00
|
|
|
parameters {auto _ : Pretty.HasEnv m}
|
|
|
|
private
|
|
|
|
prettyIndex : Nat -> m (Doc a)
|
|
|
|
prettyIndex i =
|
|
|
|
ifUnicode (pretty $ pack $ map sup $ unpack $ show i) (":" <+> pretty i)
|
|
|
|
where
|
|
|
|
sup : Char -> Char
|
|
|
|
sup c = case c of
|
|
|
|
'0' => '⁰'; '1' => '¹'; '2' => '²'; '3' => '³'; '4' => '⁴'
|
|
|
|
'5' => '⁵'; '6' => '⁶'; '7' => '⁷'; '8' => '⁸'; '9' => '⁹'; _ => c
|
|
|
|
|
|
|
|
||| `prettyVar hlok hlerr names i` pretty prints the de Bruijn index `i`.
|
|
|
|
|||
|
|
|
|
||| If it is within the bounds of `names`, then it uses the name at that index,
|
|
|
|
||| highlighted as `hlok`. Otherwise it is just printed as a number highlighted
|
|
|
|
||| as `hlerr`.
|
|
|
|
export
|
|
|
|
prettyVar' : HL -> HL -> List Name -> Nat -> m (Doc HL)
|
|
|
|
prettyVar' hlok hlerr names i =
|
|
|
|
case inBounds i names of
|
|
|
|
Yes _ => hlF' hlok [|prettyM (index i names) <+> prettyIndex i|]
|
|
|
|
No _ => pure $ hl hlerr $ pretty i
|
|
|
|
|
|
|
|
export %inline
|
|
|
|
prettyVar : HL -> HL -> List Name -> Var n -> m (Doc HL)
|
|
|
|
prettyVar hlok hlerr names i = prettyVar' hlok hlerr names i.nat
|
2021-07-20 16:05:19 -04:00
|
|
|
|
|
|
|
|
|
|
|
public export
|
|
|
|
fromNatWith : (i : Nat) -> (0 p : i `LT` n) -> Var n
|
|
|
|
fromNatWith Z (LTESucc _) = VZ
|
|
|
|
fromNatWith (S i) (LTESucc p) = VS $ fromNatWith i p
|
|
|
|
%transform "Var.fromNatWith" fromNatWith i p = believe_me i
|
|
|
|
|
|
|
|
public export %inline
|
|
|
|
V : (i : Nat) -> {auto 0 p : i `LT` n} -> Var n
|
|
|
|
V i {p} = fromNatWith i p
|
|
|
|
|
2021-09-03 11:10:20 -04:00
|
|
|
export %inline
|
2021-07-20 16:05:19 -04:00
|
|
|
tryFromNat : Alternative f => (n : Nat) -> Nat -> f (Var n)
|
|
|
|
tryFromNat n i =
|
|
|
|
case i `isLT` n of
|
|
|
|
Yes p => pure $ fromNatWith i p
|
|
|
|
No _ => empty
|
|
|
|
|
|
|
|
export
|
|
|
|
0 toNatLT : (i : Var n) -> i.nat `LT` n
|
|
|
|
toNatLT VZ = LTESucc LTEZero
|
|
|
|
toNatLT (VS i) = LTESucc $ toNatLT i
|
|
|
|
|
2022-02-26 20:17:42 -05:00
|
|
|
public export
|
|
|
|
toNatInj : {i, j : Var n} -> i.nat = j.nat -> i = j
|
2021-07-20 16:05:19 -04:00
|
|
|
toNatInj {i = VZ} {j = VZ} Refl = Refl
|
|
|
|
toNatInj {i = VZ} {j = (VS i)} Refl impossible
|
|
|
|
toNatInj {i = (VS i)} {j = VZ} Refl impossible
|
2022-02-26 20:17:42 -05:00
|
|
|
toNatInj {i = (VS i)} {j = (VS j)} prf = cong VS $ toNatInj $ injective prf
|
|
|
|
|
|
|
|
public export %inline Injective (.nat) where injective = toNatInj
|
2021-07-20 16:05:19 -04:00
|
|
|
|
|
|
|
export
|
|
|
|
0 fromToNat : (i : Var n) -> (p : i.nat `LT` n) -> fromNatWith i.nat p = i
|
|
|
|
fromToNat VZ (LTESucc p) = Refl
|
|
|
|
fromToNat (VS i) (LTESucc p) = rewrite fromToNat i p in Refl
|
|
|
|
|
|
|
|
export
|
|
|
|
0 toFromNat : (i : Nat) -> (p : i `LT` n) -> (fromNatWith i p).nat = i
|
|
|
|
toFromNat 0 (LTESucc x) = Refl
|
|
|
|
toFromNat (S k) (LTESucc x) = cong S $ toFromNat k x
|
|
|
|
|
|
|
|
|
|
|
|
-- not using %transform like other things because weakSpec requires the proof
|
|
|
|
-- to be relevant. but since only `LTESucc` is ever possible that seems
|
2022-02-26 19:48:37 -05:00
|
|
|
-- to be an instance of <https://github.com/idris-lang/Idris2/issues/1259>?
|
2021-07-20 16:05:19 -04:00
|
|
|
export
|
|
|
|
weak : (0 p : m `LTE` n) -> Var m -> Var n
|
2022-02-26 20:06:52 -05:00
|
|
|
weak p i = fromNatWith i.nat $ transitive (toNatLT i) p
|
2021-07-20 16:05:19 -04:00
|
|
|
|
|
|
|
public export
|
|
|
|
0 weakSpec : m `LTE` n -> Var m -> Var n
|
|
|
|
weakSpec LTEZero _ impossible
|
|
|
|
weakSpec (LTESucc p) VZ = VZ
|
|
|
|
weakSpec (LTESucc p) (VS i) = VS $ weakSpec p i
|
|
|
|
|
|
|
|
export
|
|
|
|
0 weakSpecCorrect : (p : m `LTE` n) -> (i : Var m) -> (weakSpec p i).nat = i.nat
|
|
|
|
weakSpecCorrect LTEZero _ impossible
|
|
|
|
weakSpecCorrect (LTESucc x) VZ = Refl
|
|
|
|
weakSpecCorrect (LTESucc x) (VS i) = cong S $ weakSpecCorrect x i
|
|
|
|
|
|
|
|
export
|
|
|
|
0 weakCorrect : (p : m `LTE` n) -> (i : Var m) -> (weak p i).nat = i.nat
|
2022-02-26 19:28:19 -05:00
|
|
|
weakCorrect LTEZero _ impossible
|
2021-07-20 16:05:19 -04:00
|
|
|
weakCorrect (LTESucc p) VZ = Refl
|
|
|
|
weakCorrect (LTESucc p) (VS i) = cong S $ weakCorrect p i
|
|
|
|
|
|
|
|
export
|
|
|
|
0 weakIsSpec : (p : m `LTE` n) -> (i : Var m) -> weak p i = weakSpec p i
|
|
|
|
weakIsSpec p i = toNatInj $ trans (weakCorrect p i) (sym $ weakSpecCorrect p i)
|
|
|
|
|
|
|
|
|
|
|
|
public export
|
|
|
|
interface FromVar f where %inline fromVar : Var n -> f n
|
|
|
|
|
|
|
|
public export FromVar Var where fromVar = id
|
2021-12-23 13:01:24 -05:00
|
|
|
|
|
|
|
|
|
|
|
public export
|
|
|
|
data LT : Var n -> Var n -> Type where
|
|
|
|
LTZ : VZ `LT` VS i
|
|
|
|
LTS : i `LT` j -> VS i `LT` VS j
|
|
|
|
%builtin Natural Var.LT
|
|
|
|
%name Var.LT lt
|
|
|
|
|
|
|
|
public export %inline
|
|
|
|
GT : Var n -> Var n -> Type
|
|
|
|
i `GT` j = j `LT` i
|
|
|
|
|
|
|
|
export
|
|
|
|
Transitive (Var n) LT where
|
|
|
|
transitive LTZ (LTS _) = LTZ
|
2022-02-26 20:06:52 -05:00
|
|
|
transitive (LTS p) (LTS q) = LTS $ transitive p q
|
2021-12-23 13:01:24 -05:00
|
|
|
|
|
|
|
export Uninhabited (i `Var.LT` i) where uninhabited (LTS p) = uninhabited p
|
|
|
|
export Uninhabited (VS i `LT` VZ) where uninhabited _ impossible
|
|
|
|
|
|
|
|
export
|
|
|
|
isLT : (i, j : Var n) -> Dec (i `LT` j)
|
|
|
|
isLT VZ VZ = No uninhabited
|
|
|
|
isLT VZ (VS j) = Yes LTZ
|
|
|
|
isLT (VS i) VZ = No uninhabited
|
|
|
|
isLT (VS i) (VS j) with (isLT i j)
|
|
|
|
_ | Yes prf = Yes (LTS prf)
|
|
|
|
_ | No contra = No (\case LTS p => contra p)
|
|
|
|
|
|
|
|
|
|
|
|
public export
|
|
|
|
data Compare : (i, j : Var n) -> Type where
|
|
|
|
IsLT : (lt : i `LT` j) -> Compare i j
|
|
|
|
IsEQ : Compare i i
|
|
|
|
IsGT : (gt : i `GT` j) -> Compare i j
|
|
|
|
%name Compare cmp
|
|
|
|
|
|
|
|
export
|
|
|
|
compareS : Compare i j -> Compare (VS i) (VS j)
|
|
|
|
compareS (IsLT lt) = IsLT (LTS lt)
|
|
|
|
compareS IsEQ = IsEQ
|
|
|
|
compareS (IsGT gt) = IsGT (LTS gt)
|
|
|
|
|
|
|
|
export
|
|
|
|
compareP : (i, j : Var n) -> Compare i j
|
|
|
|
compareP VZ VZ = IsEQ
|
|
|
|
compareP VZ (VS j) = IsLT LTZ
|
|
|
|
compareP (VS i) VZ = IsGT LTZ
|
|
|
|
compareP (VS i) (VS j) = compareS $ compareP i j
|
|
|
|
|
2022-02-26 19:50:01 -05:00
|
|
|
export
|
|
|
|
0 compareSelf : (c : Compare i i) -> c = IsEQ
|
|
|
|
compareSelf (IsLT lt) = absurd lt
|
|
|
|
compareSelf IsEQ = Refl
|
|
|
|
compareSelf (IsGT gt) = absurd gt
|
|
|
|
|
|
|
|
export
|
|
|
|
0 comparePSelf : (i : Var n) -> compareP i i = IsEQ
|
|
|
|
comparePSelf i = compareSelf {}
|
|
|
|
|
2021-12-23 13:01:24 -05:00
|
|
|
|
|
|
|
public export
|
|
|
|
data LTE : Var n -> Var n -> Type where
|
|
|
|
LTEZ : VZ `LTE` j
|
|
|
|
LTES : i `LTE` j -> VS i `LTE` VS j
|
|
|
|
|
|
|
|
export
|
|
|
|
Reflexive (Var n) LTE where
|
|
|
|
reflexive {x = VZ} = LTEZ
|
2022-02-26 20:06:52 -05:00
|
|
|
reflexive {x = VS i} = LTES reflexive
|
2021-12-23 13:01:24 -05:00
|
|
|
|
|
|
|
export
|
|
|
|
Transitive (Var n) LTE where
|
|
|
|
transitive LTEZ q = LTEZ
|
2022-02-26 20:06:52 -05:00
|
|
|
transitive (LTES p) (LTES q) = LTES $ transitive p q
|
2021-12-23 13:01:24 -05:00
|
|
|
|
|
|
|
export
|
|
|
|
Antisymmetric (Var n) LTE where
|
|
|
|
antisymmetric LTEZ LTEZ = Refl
|
2022-02-26 20:06:52 -05:00
|
|
|
antisymmetric (LTES p) (LTES q) = cong VS $ antisymmetric p q
|
2021-12-23 13:01:24 -05:00
|
|
|
|
|
|
|
export
|
2022-02-26 19:49:42 -05:00
|
|
|
splitLTE : {j : Var n} -> i `LTE` j -> Either (i = j) (i `LT` j)
|
2021-12-23 13:01:24 -05:00
|
|
|
splitLTE {j = VZ} LTEZ = Left Refl
|
|
|
|
splitLTE {j = VS _} LTEZ = Right LTZ
|
|
|
|
splitLTE (LTES p) with (splitLTE p)
|
|
|
|
_ | (Left eq) = Left $ cong VS eq
|
|
|
|
_ | (Right lt) = Right $ LTS lt
|
2022-02-26 20:17:42 -05:00
|
|
|
|
|
|
|
|
|
|
|
export Uninhabited (VZ = VS i) where uninhabited _ impossible
|
|
|
|
export Uninhabited (VS i = VZ) where uninhabited _ impossible
|
|
|
|
|
|
|
|
|
|
|
|
public export
|
|
|
|
eqReflect : (i, j : Var n) -> (i = j) `Reflects` (i == j)
|
|
|
|
eqReflect VZ VZ = RTrue Refl
|
|
|
|
eqReflect VZ (VS i) = RFalse absurd
|
|
|
|
eqReflect (VS i) VZ = RFalse absurd
|
|
|
|
eqReflect (VS i) (VS j) with (eqReflect i j)
|
|
|
|
eqReflect (VS i) (VS j) | r with (i == j)
|
|
|
|
eqReflect (VS i) (VS j) | RTrue yes | True = RTrue $ cong VS yes
|
|
|
|
eqReflect (VS i) (VS j) | RFalse no | False = RFalse $ no . injective
|
|
|
|
|
|
|
|
public export
|
|
|
|
reflectToDec : p `Reflects` b -> Dec p
|
|
|
|
reflectToDec (RTrue y) = Yes y
|
|
|
|
reflectToDec (RFalse n) = No n
|
|
|
|
|
|
|
|
public export %inline
|
|
|
|
varDecEq : (i, j : Var n) -> Dec (i = j)
|
|
|
|
varDecEq i j = reflectToDec $ eqReflect i j
|
|
|
|
|
|
|
|
-- justified by eqReflect [citation needed]
|
|
|
|
private %inline
|
|
|
|
decEqFromBool : (i, j : Var n) -> Dec (i = j)
|
|
|
|
decEqFromBool i j =
|
|
|
|
if i == j then Yes $ believe_me $ Refl {x = 0}
|
|
|
|
else No $ id . believe_me
|
|
|
|
|
|
|
|
%transform "Var.decEq" varDecEq = decEqFromBool
|
|
|
|
|
|
|
|
public export %inline DecEq (Var n) where decEq = varDecEq
|
2022-02-26 20:17:51 -05:00
|
|
|
|
2022-04-11 17:36:01 -04:00
|
|
|
export
|
|
|
|
Tighten Var where
|
2022-02-26 20:17:51 -05:00
|
|
|
tighten Id i = pure i
|
|
|
|
tighten (Drop q) VZ = empty
|
|
|
|
tighten (Drop q) (VS i) = tighten q i
|
|
|
|
tighten (Keep q) VZ = pure VZ
|
|
|
|
tighten (Keep q) (VS i) = VS <$> tighten q i
|