Debian Codegraph @

src/Reflection/AST/DeBruijn.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Weakening, strengthening and free variable check for reflected terms.
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Reflection.AST.DeBruijn where
10
11 open import Data.Bool.Base using (Bool; true; false; _∨_; if_then_else_)
12 open import Data.Nat.Base using (ℕ; zero; suc; _+_; _∸_; _<ᵇ_; _≡ᵇ_)
13 open import Data.List.Base using (List; []; _∷_; _++_)
14 open import Data.Maybe.Base using (Maybe; nothing; just)
15 import Data.Maybe.Effectful as Maybe using (applicative)
16 import Function.Identity.Effectful as Identity using (applicative)
17 open import Effect.Applicative using (RawApplicative; mkRawApplicative)
18 open import Reflection
19 open import Reflection.AST.Argument.Visibility using (Visibility)
20 import Reflection.AST.Traversal as Traverse
21
22 private
23 variable A : Set
24
25 ------------------------------------------------------------------------
26 -- Weakening
27
28 module _ where
29
30 open Traverse Identity.applicative
31
32 private
33
34 wkVar : ℕ → Cxt → ℕ → ℕ
35 wkVar by (from , _) i = if i <ᵇ from then i else i + by
36
37 actions : ℕ → Actions
38 actions k = record defaultActions { onVar = wkVar k }
39
40 weakenFrom′ : (Actions → Cxt → A → A) → (from by : ℕ) → A → A
41 weakenFrom′ trav from by = trav (actions by) (from , []) -- not using the context part
42
43 weakenFrom : (from by : ℕ) → Term → Term
44 weakenFrom = weakenFrom′ traverseTerm
45
46 weaken : (by : ℕ) → Term → Term
47 weaken = weakenFrom 0
48
49 weakenArgs : (by : ℕ) → Args Term → Args Term
50 weakenArgs = weakenFrom′ traverseArgs 0
51
52 weakenClauses : (by : ℕ) → Clauses → Clauses
53 weakenClauses = weakenFrom′ traverseClauses 0
54
55
56 ------------------------------------------------------------------------
57 -- η-expansion
58
59 private
60 η : Visibility → (Args Term → Term) → Args Term → Term
61 η h f args =
62 lam h (abs "x" (f (weakenArgs 1 args ++
63 arg (arg-info h defaultModality) (var 0 []) ∷
64 [])))
65
66 η-expand : Visibility → Term → Term
67 η-expand h (var x args) = η h (var (suc x)) args
68 η-expand h (con c args) = η h (con c) args
69 η-expand h (def f args) = η h (def f) args
70 η-expand h (pat-lam cs args) = η h (pat-lam (weakenClauses 1 cs)) args
71 η-expand h (meta x args) = η h (meta x) args
72 η-expand h t@(lam _ _) = t
73 η-expand h t@(pi _ _) = t
74 η-expand h t@(agda-sort _) = t
75 η-expand h t@(lit _) = t
76 η-expand h t@unknown = t
77
78
79 ------------------------------------------------------------------------
80 -- Strengthening
81
82 module _ where
83
84 open Traverse Maybe.applicative
85
86 private
87 strVar : ℕ → Cxt → ℕ → Maybe ℕ
88 strVar by (from , Γ) i with i <ᵇ from | i <ᵇ from + by
89 ... | true | _ = just i
90 ... | _ | true = nothing
91 ... | _ | _ = just (i ∸ by)
92
93 actions : ℕ → Actions
94 actions by = record defaultActions { onVar = strVar by }
95
96 strengthenFromBy′ : (Actions → Cxt → A → Maybe A) → (from by : ℕ) → A → Maybe A
97 strengthenFromBy′ trav from by = trav (actions by) (from , []) -- not using the context part
98
99 strengthenFromBy : (from by : ℕ) → Term → Maybe Term
100 strengthenFromBy = strengthenFromBy′ traverseTerm
101
102 strengthenBy : (by : ℕ) → Term → Maybe Term
103 strengthenBy = strengthenFromBy 0
104
105 strengthenFrom : (from : ℕ) → Term → Maybe Term
106 strengthenFrom from = strengthenFromBy from 1
107
108 strengthen : Term → Maybe Term
109 strengthen = strengthenFromBy 0 1
110
111
112 ------------------------------------------------------------------------
113 -- Free variable check
114
115 module _ where
116
117 private
118 anyApplicative : ∀ {ℓ} → RawApplicative {ℓ} (λ _ → Bool)
119 anyApplicative = mkRawApplicative _ (λ _ → false) _∨_
120
121 open Traverse anyApplicative
122
123 private
124 fvVar : ℕ → Cxt → ℕ → Bool
125 fvVar i (n , _) x = i + n ≡ᵇ x
126
127 actions : ℕ → Actions
128 actions i = record defaultActions { onVar = fvVar i }
129
130 infix 4 _∈FV_
131
132 _∈FV_ : ℕ → Term → Bool
133 i ∈FV t = traverseTerm (actions i) (0 , []) t