|
| 1 | +--- |
| 2 | +layout: doc-page |
| 3 | +title: "The Meta-theory of Symmetric Meta-programming" |
| 4 | +--- |
| 5 | + |
| 6 | +# The Meta-theory of Symmetric Meta-programming |
| 7 | + |
| 8 | +23.12.2017 |
| 9 | + |
| 10 | +This note presents a simplified variant of |
| 11 | +[symmetric meta-programming](./symmetric-meta-programming.md) |
| 12 | +and sketches its soundness proof. The variant treats only dialogues |
| 13 | +between two stages. A program can have quotes which can contain |
| 14 | +splices (which can contain quotes, which can contain splices, and so |
| 15 | +on). Or the program could start with a splice with embedded |
| 16 | +quotes. The essential restriction is that (1) a term can contain top-level |
| 17 | +quotes or top-level splices, but not both, and (2) quotes cannot appear |
| 18 | +directly inside quotes and splices cannot appear directly inside |
| 19 | +splices. In other words, the universe is restricted to two phases |
| 20 | +only. |
| 21 | + |
| 22 | +Under this restriction we can simplify the typing rules so that there are |
| 23 | +always exactly two environments instead of having a stack of environments. |
| 24 | +The variant presented here differs from the full calculus also in that we |
| 25 | +replace evaluation contexts with contextual typing rules. While this |
| 26 | +is more verbose, it makes it easier to set up the meta theory. |
| 27 | + |
| 28 | +## Syntax |
| 29 | + |
| 30 | + Terms t ::= x variable |
| 31 | + (x: T) => t lambda |
| 32 | + t t application |
| 33 | + ’t quote |
| 34 | + ~t splice |
| 35 | + |
| 36 | + Simple terms u ::= x | (x: T) => u | u u |
| 37 | + |
| 38 | + Values v ::= (x: T) => t lambda |
| 39 | + ’u quoted value |
| 40 | + |
| 41 | + Types T ::= A base type |
| 42 | + T -> T function type |
| 43 | + ’T quoted type |
| 44 | + |
| 45 | +## Operational semantics |
| 46 | + |
| 47 | +### Evaluation |
| 48 | + |
| 49 | + ((x: T) => t) v --> [x := v]t |
| 50 | + |
| 51 | + t1 --> t2 |
| 52 | + --------------- |
| 53 | + t1 t --> t2 t |
| 54 | + |
| 55 | + t1 --> t2 |
| 56 | + --------------- |
| 57 | + v t1 --> v t2 |
| 58 | + |
| 59 | + t1 ==> t2 |
| 60 | + ------------- |
| 61 | + ’t1 --> ’t2 |
| 62 | + |
| 63 | + |
| 64 | +### Splicing |
| 65 | + |
| 66 | + ~’u ==> u |
| 67 | + |
| 68 | + t1 ==> t2 |
| 69 | + ------------------------------- |
| 70 | + (x: T) => t1 ==> (x: T) => t2 |
| 71 | + |
| 72 | + t1 ==> t2 |
| 73 | + --------------- |
| 74 | + t1 t ==> t2 t |
| 75 | + |
| 76 | + t1 ==> t2 |
| 77 | + --------------- |
| 78 | + u t1 ==> u t2 |
| 79 | + |
| 80 | + t1 --> t2 |
| 81 | + ------------- |
| 82 | + ~t1 ==> ~t2 |
| 83 | + |
| 84 | + |
| 85 | +## Typing Rules |
| 86 | + |
| 87 | +Typing judgments are of the form `E1 * E2 |- t: T` where `E1, E2` are environments and |
| 88 | +`*` is one of `~` and `’`. |
| 89 | + |
| 90 | + x: T in E2 |
| 91 | + --------------- |
| 92 | + E1 * E2 |- x: T |
| 93 | + |
| 94 | + |
| 95 | + E1 * E2, x: T1 |- t: T2 |
| 96 | + -------------------------------- |
| 97 | + E1 * E2 |- (x: T1) => t: T -> T2 |
| 98 | + |
| 99 | + |
| 100 | + E1 * E2 |- t1: T2 -> T E1 * E2 |- t2: T2 |
| 101 | + ------------------------------------------- |
| 102 | + E1 * E2 |- t1 t2: T |
| 103 | + |
| 104 | + |
| 105 | + E2 ’ E1 |- t: T |
| 106 | + ----------------- |
| 107 | + E1 ~ E2 |- ’t: ’T |
| 108 | + |
| 109 | + |
| 110 | + E2 ~ E1 |- t: ’T |
| 111 | + ---------------- |
| 112 | + E1 ’ E2 |- ~t: T |
| 113 | + |
| 114 | + |
| 115 | +(Curiously, this looks a bit like a Christmas tree). |
| 116 | + |
| 117 | +## Soundness |
| 118 | + |
| 119 | +The meta-theory typically requires mutual inductions over two judgments. |
| 120 | + |
| 121 | +### Progress Theorem |
| 122 | + |
| 123 | + 1. If `E1 ~ |- t: T` then either `t = v` for some value `v` or `t --> t2` for some term `t2`. |
| 124 | + 2. If ` ’ E2 |- t: T` then either `t = u` for some simple term `u` or `t ==> t2` for some term `t2`. |
| 125 | + |
| 126 | +Proof by structural induction over terms. |
| 127 | + |
| 128 | +To prove (1): |
| 129 | + |
| 130 | + - the cases for variables, lambdas and applications are as in STL. |
| 131 | + - If `t = ’t2`, then by inversion we have ` ’ E1 |- t2: T2` for some type `T2`. |
| 132 | + By the second I.H., we have one of: |
| 133 | + - `t2 = u`, hence `’t2` is a value, |
| 134 | + - `t2 ==> t3`, hence `’t2 --> ’t3`. |
| 135 | + - The case `t = ~t2` is not typable. |
| 136 | + |
| 137 | +To prove (2): |
| 138 | + |
| 139 | + - If `t = x` then `t` is a simple term. |
| 140 | + - If `t = (x: T) => t2`, then either `t2` is a simple term, in which case `t` is as well. |
| 141 | + Or by the second I.H. `t2 ==> t3`, in which case `t ==> (x: T) => t3`. |
| 142 | + - If `t = t1 t2` then one of three cases applies: |
| 143 | + |
| 144 | + - `t1` and `t2` are a simple term, then `t` is as well a simple term. |
| 145 | + - `t1` is not a simple term. Then by the second IH, `t1 ==> t12`, hence `t ==> t12 t2`. |
| 146 | + - `t1` is a simple term but `t2` is not. Then by the second IH. `t2 ==> t22`, hence `t ==> t1 t22`. |
| 147 | + |
| 148 | + - The case `t = ’t2` is not typable. |
| 149 | + - If `t = ~t2` then by inversion we have `E2 ~ |- t2: ’T2`, for some some type `T2`. |
| 150 | + By the first I.H., we have one of |
| 151 | + |
| 152 | + - `t2 = v`. Since `t2: ’T2`, we must have `v = ’u`, for some simple term `u`, hence `t = ~’u`. |
| 153 | + By quote-splice reduction, `t ==> u`. |
| 154 | + - `t2 --> t3`. Then by the context rule for `’t`, `t ==> ’t3`. |
| 155 | + |
| 156 | + |
| 157 | +### Substitution Lemma |
| 158 | + |
| 159 | + 1. If `E1 ~ E2 |- s: S` and `E1 ~ E2, x: S |- t: T` then `E1 ~ E2 |- [x := s]t: T`. |
| 160 | + 2. If `E1 ~ E2 |- s: S` and `E2, x: S ’ E1 |- t: T` then `E2 ’ E1 |- [x := s]t: T`. |
| 161 | + |
| 162 | +The proofs are by induction on typing derivations for `t`, analogous |
| 163 | +to the proof for STL (with (2) a bit simpler than (1) since we do not |
| 164 | +need to swap lambda bindings with the bound variable `x`). The |
| 165 | +arguments that link the two hypotheses are as follows. |
| 166 | + |
| 167 | +To prove (1), let `t = ’t1`. Then `T = ’T1` for some type `T1` and the last typing rule is |
| 168 | + |
| 169 | + E2, x: S ’ E1 |- t1: T1 |
| 170 | + ------------------------- |
| 171 | + E1 ~ E2, x: S |- ’t1: ’T1 |
| 172 | + |
| 173 | +By the second I.H. `E2 ’ E1 |- [x := s]t1: T1`. By typing, `E1 ~ E2 |- ’[x := s]t1: ’T1`. |
| 174 | +Since `[x := s]t = [x := s](’t1) = ’[x := s]t1` we get `[x := s]t: ’T1`. |
| 175 | + |
| 176 | +To prove (2), let `t = ~t1`. Then the last typing rule is |
| 177 | + |
| 178 | + E1 ~ E2, x: S |- t1: ’T |
| 179 | + ----------------------- |
| 180 | + E2, x: S ’ E1 |- ~t1: T |
| 181 | + |
| 182 | +By the first I.H., `E1 ~ E2 |- [x := s]t1: ’T`. By typing, `E2 ’ E1 |- ~[x := s]t1: T`. |
| 183 | +Since `[x := s]t = [x := s](~t1) = ~[x := s]t1` we get `[x := s]t: T`. |
| 184 | + |
| 185 | + |
| 186 | +### Preservation Theorem |
| 187 | + |
| 188 | + 1. If `E1 ~ E2 |- t1: T` and `t1 --> t2` then `E1 ~ E2 |- t2: T`. |
| 189 | + 2. If `E1 ’ E2 |- t1: T` and `t1 ==> t2` then `E1 ’ E2 |- t2: T`. |
| 190 | + |
| 191 | +The proof is by structural induction on evaluation derivations. The proof of (1) is analogous |
| 192 | +to the proof for STL, using the substitution lemma for the beta reduction case, with the addition of reduction of quoted terms, which goes as follows: |
| 193 | + |
| 194 | + - Assume the last rule was |
| 195 | + |
| 196 | + t1 ==> t2 |
| 197 | + ------------- |
| 198 | + ’t1 --> ’t2 |
| 199 | + |
| 200 | + By inversion of typing rules, we must have `T = ’T1` for some type `T1` such that `t1: T1`. |
| 201 | + By the second I.H., `t2: T1`, hence `’t2: `T1`. |
| 202 | + |
| 203 | + |
| 204 | +To prove (2): |
| 205 | + |
| 206 | + - Assume the last rule was `~’u ==> u`. The typing proof of `~’u` must have the form |
| 207 | + |
| 208 | + |
| 209 | + E1 ’ E2 |- u: T |
| 210 | + ----------------- |
| 211 | + E1 ~ E2 |- ’u: ’T |
| 212 | + ----------------- |
| 213 | + E1 ’ E2 |- ~’u: T |
| 214 | + |
| 215 | + Hence, `E1 ’ E2 |- u: T`. |
| 216 | + |
| 217 | + - Assume the last rule was |
| 218 | + |
| 219 | + t1 ==> t2 |
| 220 | + ------------------------------- |
| 221 | + (x: S) => t1 ==> (x: T) => t2 |
| 222 | + |
| 223 | + By typing inversion, `E1 ' E2, x: S |- t1: T1` for some type `T1` such that `T = S -> T1`. |
| 224 | + By the I.H, `t2: T1`. By the typing rule for lambdas the result follows. |
| 225 | + |
| 226 | + - The context rules for applications are equally straightforward. |
| 227 | + |
| 228 | + - Assume the last rule was |
| 229 | + |
| 230 | + t1 ==> t2 |
| 231 | + ------------- |
| 232 | + ~t1 ==> ~t2 |
| 233 | + |
| 234 | + By inversion of typing rules, we must have `t1: ’T`. |
| 235 | + By the first I.H., `t2: ’T`, hence `~t2: T`. |
| 236 | + |
0 commit comments