|
1 | 1 | \import Data.Fin
|
2 | 2 | \import Function
|
3 | 3 | \import Function.Meta
|
| 4 | +\import Logic |
| 5 | +\import Logic.Meta |
4 | 6 | \import Meta
|
5 | 7 | \import Paths
|
6 | 8 | \import Paths.Meta
|
| 9 | +\import decidable.FormulaCode |
| 10 | +\import decidable.TermCode |
7 | 11 | \import syntax.Context
|
| 12 | +\import syntax.Free |
8 | 13 | \import syntax.Lift
|
9 | 14 | \import syntax.Syntax
|
| 15 | +\import util.ArrayUtil |
10 | 16 | \open Context
|
11 | 17 | \open Nat (+)
|
12 | 18 |
|
|
52 | 58 | | apply f args => pmap (apply f) $ ext $ ext (\lam j => compose {_} {c} {c'} {c''} {args j})
|
53 | 59 | }
|
54 | 60 |
|
| 61 | + \func renameTermFree {_ : Language} {c c' : Context} (t : Term c) (remap : RenameFreeTerm c c' t) : Term c' |
| 62 | + \elim t |
| 63 | + | var v => var $ remap v idp |
| 64 | + | apply f args => apply f (\lam i => renameTermFree (args i) (\lam v free => remap v (inP (i, free)))) |
| 65 | + |
55 | 66 | \func rename {_ : Language} {c c' : Context} (f : Formula c) (r : Rename c c') : Formula c'
|
56 | 67 | \elim f
|
57 | 68 | | equal a b => equal (renameTerm a r) (renameTerm b r)
|
|
61 | 72 | | cAnd a b => cAnd (rename a r) (rename b r)
|
62 | 73 | | forAllH f => forAllH $ rename f (Extend.extend r)
|
63 | 74 | | cExists f => cExists $ rename f (Extend.extend r)
|
| 75 | + |
| 76 | + -- same as above but we additionally guarantee r is only applied on the free vars |
| 77 | + \func renameFree {_ : Language} {c c' : Context} (f : Formula c) (r : RenameFree c c' f) : Formula c' |
| 78 | + \elim f |
| 79 | + | equal a b => equal (renameTermFree a (\lam v free => r v (byLeft free))) (renameTermFree b (\lam v free => r v (byRight free))) |
| 80 | + | atomic rel terms => atomic rel $ \lam i => renameTermFree (terms i) {?} |
| 81 | + | notH f => notH $ renameFree f r |
| 82 | + | impH antecedent consequent => impH (renameFree antecedent r) (renameFree consequent r) |
| 83 | + | cAnd a b => cAnd (renameFree a r) (renameFree b r) |
| 84 | + | forAllH f => forAllH $ renameFree f (Extend.extend r) |
| 85 | + | cExists f => cExists $ renameFree f (Extend.extend r) |
| 86 | + |
| 87 | + \func unrenameTerm {_ : Language} {c c' : Context} {t t' : Term c} {r : Rename c c'} |
| 88 | + (rInj : \Pi {v v' : variable c} (r v = r v') -> v = v') |
| 89 | + (p : renameTerm t r = renameTerm t' r) : t = t' |
| 90 | + \elim t, t', p |
| 91 | + | var v, var v1, p => \let p' => TermCodeDec.termCode-encode p \in pmap var $ rInj p' |
| 92 | + | apply f args, apply f1 args1, p => |
| 93 | + \let |
| 94 | + | (p1, p2) => TermCodeDec.termCode-encode p |
| 95 | + \in \case \elim f1, \elim args1, p1, p2 \with { |
| 96 | + | _, args1, idp, p2 => pmap |
| 97 | + (apply f) |
| 98 | + (ext $ ext (\lam j => \let p2' => convertArrayEquality p2 | p2Result => pmap (\lam (z : Array _ _) => z j) p2' \in unrenameTerm rInj p2Result)) |
| 99 | + } |
| 100 | + |
| 101 | + \func unrename {_ : Language} {c c' : Context} {f f' : Formula c} {r : Rename c c'} |
| 102 | + (rInj : \Pi {v v' : variable c} (r v = r v') -> v = v') |
| 103 | + (p : rename f r = rename f' r) : f = f' |
| 104 | + \elim f, f', p |
| 105 | + | equal a b, equal a1 b1, p => \let (p1, p2) => FormulaCodeDec.formulaCode-encode p \in |
| 106 | + pmap2 equal (unrenameTerm rInj p1) (unrenameTerm rInj p2) |
| 107 | + | atomic r1 terms, atomic r2 terms1, p => |
| 108 | + \let |
| 109 | + | (p1, p2) => FormulaCodeDec.formulaCode-encode p |
| 110 | + \in \case \elim r2, \elim terms1, p1, p2 \with { |
| 111 | + | _, _, idp, p2 => pmap |
| 112 | + (atomic r1) |
| 113 | + (ext $ ext (\lam j => \let p2' => convertArrayEquality p2 | p2Result => pmap (\lam (z : Array _ _) => z j) p2' \in unrenameTerm rInj p2Result)) |
| 114 | + } |
| 115 | + | notH f, notH f', p => |
| 116 | + \let p' => FormulaCodeDec.formulaCode-decode $ FormulaCodeDec.formulaCode-encode p |
| 117 | + \in pmap notH $ unrename rInj p' |
| 118 | + | impH a f, impH a1 f', p => |
| 119 | + \let |
| 120 | + | (p1, p2) => FormulaCodeDec.formulaCode-encode p |
| 121 | + | p1' => FormulaCodeDec.formulaCode-decode p1 |
| 122 | + | p2' => FormulaCodeDec.formulaCode-decode p2 |
| 123 | + \in pmap2 impH (unrename rInj p1') (unrename rInj p2') |
| 124 | + | cAnd a f, cAnd a1 f', p => |
| 125 | + \let |
| 126 | + | (p1, p2) => FormulaCodeDec.formulaCode-encode p |
| 127 | + | p1' => FormulaCodeDec.formulaCode-decode p1 |
| 128 | + | p2' => FormulaCodeDec.formulaCode-decode p2 |
| 129 | + \in pmap2 cAnd (unrename rInj p1') (unrename rInj p2') |
| 130 | + | forAllH f, forAllH f', p => |
| 131 | + \let |
| 132 | + | p' => FormulaCodeDec.formulaCode-decode $ FormulaCodeDec.formulaCode-encode p |
| 133 | + \in pmap forAllH (unrename (\lam {v : variable (suc c)} {v' : variable (suc c)} p => \case \elim v, \elim v', \elim p \with { |
| 134 | + | 0, 0, p => idp |
| 135 | + | 0, suc f1, p => \let p : 0 = suc _ => p \in usingOnly p contradiction |
| 136 | + | suc f1, 0, p => \let p : suc _ = 0 => p \in usingOnly p contradiction |
| 137 | + | suc f1, suc f2, p => \let p' => unfsuc p \in pmap fsuc (rInj p') |
| 138 | + }) p') |
| 139 | + | cExists f, cExists f', p => |
| 140 | + \let |
| 141 | + | p' => FormulaCodeDec.formulaCode-decode $ FormulaCodeDec.formulaCode-encode p |
| 142 | + \in pmap cExists (unrename (\lam {v : variable (suc c)} {v' : variable (suc c)} p => \case \elim v, \elim v', \elim p \with { |
| 143 | + | 0, 0, p => idp |
| 144 | + | 0, suc f1, p => \let p : 0 = suc _ => p \in usingOnly p contradiction |
| 145 | + | suc f1, 0, p => \let p : suc _ = 0 => p \in usingOnly p contradiction |
| 146 | + | suc f1, suc f2, p => \let p' => unfsuc p \in pmap fsuc (rInj p') |
| 147 | + }) p') |
64 | 148 | }
|
65 | 149 |
|
66 | 150 | \func Substitution {_ : Language} (c c' : Context) => variable c -> Term c'
|
|
84 | 168 | }
|
85 | 169 |
|
86 | 170 | -- TODO: this proof seems very complicated. What is the general principle?
|
| 171 | + |
87 | 172 | \func distributeExtends {_ : Language} {c c' c'' : Context} {s : Substitution c c'} {s' : Substitution c' c''} :
|
88 | 173 | (\lam v => substituteTerm (Extend.extends s v) (Extend.extends s')) = {Substitution (suc c) (suc c'')}
|
89 | 174 | Extend.extends (\lam v => substituteTerm (s v) s') => ext (helper s s')
|
|
101 | 186 |
|
102 | 187 | \private \func helper2 {_ : Language} {c' c'' : Context} (t : Term c') (s' : Substitution c' c'') :
|
103 | 188 | substituteTerm (Rename.renameTerm t fsuc) (Extend.extends s') = Rename.renameTerm (substituteTerm t s') fsuc
|
104 |
| - \elim t |
105 |
| - | var v => idp |
106 |
| - | apply f args => pmap (apply f) (ext $ ext (\lam j => helper2 (args j) s')) |
| 189 | + \elim t |
| 190 | + | var v => idp |
| 191 | + | apply f args => pmap (apply f) (ext $ ext (\lam j => helper2 (args j) s')) |
107 | 192 | }
|
108 | 193 |
|
109 | 194 | \func substitute {_ : Language} {c c' : Context} (f : Formula c) (s : Substitution c c') : Formula c'
|
|
0 commit comments