|
2 | 2 | #import "./template.typ": * |
3 | 3 | #import "./theme.typ": * |
4 | 4 | #import "@preview/algo:0.3.4": algo, i, d, comment, code |
5 | | -= Simple Algorithm for Rewriting <PaperAlgo> |
| 5 | += Generalised Rewriting in Literature <PaperAlgo> |
6 | 6 |
|
7 | 7 | We briefly mentioned that the literature algorithm about generalised rewriting consists of two parts. To understand the proof and constraint generation section of that algorithm we will start by introducing Lean expressions, explain Coq's morphism framework, and finally provide the algorithm specification. |
8 | 8 |
|
9 | | -== Lean Terms |
| 9 | +== Terms in Lean |
10 | 10 |
|
11 | 11 | In this section we will clarify the terms goal, subgoal, Lean term, and constraint by explaining the nature of Lean expressions. |
12 | 12 |
|
13 | | -A term (or expression) in Lean 4 can be represented as any of the following variants `const`, `sort`, `fvar`, `mvar`, `app`, `lam`, `bvar`, `forallE`, `letE`, `lit`, `mdata`, or `proj`. They each represent a unique constructor of Lean's expression (`Expr`) type: |
| 13 | +A term (or expression) in Lean 4 can be represented as any of the following variants `const`, `sort`, `fvar`, `mvar`, `app`, `lam`, `bvar`, `forallE`, `letE`, `lit`, `mdata`, or `proj`. They esentially corresponds to an extension of the simply typed λ-calculus @lambdacalc. Those variants each represent a unique constructor of Lean's expression (`Expr`) type: |
14 | 14 |
|
15 | 15 | ```lean |
16 | 16 | inductive Expr : Type where |
@@ -43,15 +43,15 @@ Note that some constructors of the `Expr` type contain references to Expr itself |
43 | 43 | - Expr.mdata is the expression for annotations. |
44 | 44 | - Expr.proj is a more efficient notation for product types in Lean. |
45 | 45 |
|
46 | | -Expressions of type `mvar` are the kind of Lean expressions that also represent the goals and subgoals that we want to prove. In Lean's tactic mode we always have one active main goal that we need to solve first. Metavariables are called existential variables in Coq and are a crucial part of the algorithm for generalised rewriting. The constraints we mentioned earlier are synonymous for metavariables. However, during the rewriting process, we usually inspect them prior to becoming goals for the user to solve. |
| 46 | +Expressions of type `mvar` are the kind of Lean expressions that also represent the goals and subgoals that we want to prove. In Lean's tactic mode we always have one active main goal that we need to solve first. Metavariables are called existential variables in Coq and are a crucial part of the algorithm for generalised rewriting. The constraints we mentioned earlier are synonymous for metavariables. However, during the rewriting process, we usually inspect them prior to becoming goals for the user to solve @metaprogrammingbook. |
47 | 47 |
|
48 | | -Metavariables of a certain type $Gamma tack tau$ can be assigned with any value $Gamma tack e : tau$ in the context $Gamma$ (hypotheses, theorems, etc.). Both $Gamma$ and $tau$ are part of that metavariable. When we assign a metavariable that represents a goal, we also close the goal. Another way to leverage metavariables is to use them as placeholders in any given Lean term when the value is unknown at the time of creation. It is also possible to share a metavariable $?_x$ across multiple terms. Assigning such a metavariable with a value $v$ also assigns every occurrence of $?_x$ with $v$. Whenever we choose variable names starting with a question mark in this thesis, we refer to metavariables. |
| 48 | +Metavariables of a certain type $Gamma tack tau$ can be assigned with any value $Gamma tack e : tau$ in the context $Gamma$ (hypotheses, theorems, etc.). Both $Gamma$ and $tau$ are part of that metavariable. When we assign a metavariable that represents a goal, we also close the goal. Another way to leverage metavariables is to use them as placeholders in any given Lean term when the value is unknown at the time of creation. It is also possible to share a metavariable $?_x$ across multiple terms. Assigning such a metavariable with a value $v$ also assigns every occurrence of $?_x$ with $v$. Whenever we choose variable names starting with a question mark in this thesis, we refer to metavariables @metaprogrammingbook. |
49 | 49 |
|
50 | | -Another concept frequently used in the rewriting algorithm we propose is unification. In Lean 4 unification refers to the assignments of metavariables so that two terms $t$ and $u$ containing them, become definitionally equal @carneiro2019type. We will refer to two different functions for unification. Given a term $rho : r space a space a'$ with $r : alpha -> alpha -> mono("Prop")$ and $a space a' : alpha$, the function $mono("unify")_rho (t)$ tries to unify t with the left-hand side of $rho$'s applied relation. The function also introduces local hypotheses for bound variables of possible `forallE` or `lam` expressions in $t$, in which case unification is evaluated on the according body. The $mono("unify")$ function returns tuple $(Psi, r, u, b)$. The first element is a set $Psi$ of hypotheses corresponding to the newly introduced binder variables that were not reassigned during unification. The relation $r$ represents the relation over the successful unification and is typically the relation used in $rho$. The term $u$ is the term that was made equal through unification, typically the right-hand side of $rho$. The last argument $b$ is a boolean value that is `true` when unification succeeded and `false` otherwise. The modified version $mono("unify"^"*")_rho (t)$ has the same behaviour but tries unification on all subterms and succeeds if at least one unification does @sozeau:inria-00628904. |
| 50 | +Another concept frequently used in the rewriting algorithm we propose is unification. In Lean 4 unification refers to the assignments of metavariables so that two terms $t$ and $u$ containing them, become definitionally equal @ullrich @carneiro2019type. We will refer to two different functions for unification. Given a term $rho : r space a space a'$ with $r : alpha -> alpha -> mono("Prop")$ and $a space a' : alpha$, the function $mono("unify")_rho (t)$ tries to unify t with the left-hand side of $rho$'s applied relation. The function also introduces local hypotheses for bound variables of possible `forallE` or `lam` expressions in $t$, in which case unification is evaluated on the according body. The $mono("unify")$ function returns tuple $(Psi, r, u, b)$. The first element is a set $Psi$ of hypotheses corresponding to the newly introduced binder variables that were not reassigned during unification. The relation $r$ represents the relation over the successful unification and is typically the relation used in $rho$. The term $u$ is the term that was made equal through unification, typically the right-hand side of $rho$. The last argument $b$ is a boolean value that is `true` when unification succeeded and `false` otherwise. The modified version $mono("unify"^"*")_rho (t)$ has the same behaviour but tries unification on all subterms and succeeds if at least one unification does @sozeau:inria-00628904. |
51 | 51 |
|
52 | 52 | #pagebreak() |
53 | 53 |
|
54 | | -== Proof Strategy for Rewriting |
| 54 | +== Proof Strategies for Rewriting |
55 | 55 |
|
56 | 56 | We mentioned that every rewrite must be justified by a proof and the rewrite tactic must provide that proof. When we want to prove a goal $P space t : mono("Prop")$ and have a hypothesis $h$ of type $r space t space u$ given $r$ is a relation $alpha arrow alpha arrow mono("Prop")$, we can prove a rewrite from $t$ to $u$ given we have the additional information that $P$ is preserved by $r$. In order to generate a rewrite proof for that, $r$ must be an equivalence relation or imply the implication relation ($<-$). Essentially, this means ($<-$) is a direct subrelation of $r$ or a transitive subrelation. When those hypotheses are in place, the proof is straight forward for this minimal example. By Lean's definition of subrelations, it suffices to show $r space (P space t) space ?_t$ and $?_t$. We can assign $?_t$ with $P space u$ can eventually proceed to obtaining a proof for the rewrite $P space t <- P space u$. |
57 | 57 |
|
@@ -194,7 +194,7 @@ algo( |
194 | 194 | return ?s t u p |
195 | 195 | ], caption: [Algorithm for relation inference.]) <infersub> |
196 | 196 |
|
197 | | -== Example <examplesection> |
| 197 | +#example("Simple Rewrite") <examplesection> |
198 | 198 |
|
199 | 199 | To demonstrate an example where we can see multiple different subterms but still keep an overview over the generated constraints, we choose a rather untypical rewrite. We want to rewrite a proposition in a conjunction using bi-implication. The algorithm rewrites $p and q$ to $q and q$ for a given relation $r$ that is a subrelation of $(<-)$ and a given proof $h : r space p space q$. The algorithm first tries to unify the entire term $p and q$ with the left-hand side of our proof ($p$). Conjunctions in Lean are defined by the `And` structure. Thus, our term $t$ is interpreted as $mono("And") p space q$ which must be read as $(mono("And") space p) space q$. This falls into the `app` case such that we first interpret $(mono("And") p)$ followed by $q$. Again the term $(mono("And") p)$ doesn't unify with $p$, and the algorithm follows another `app` iteration for the terms `And` and $p$. `And` itself does not unify and does not match any other category. Therefore, the algorithm treats it as an atom (`const`) and generates a metavariable $?_(mono("And")_r) : mono("relation") (mono("relation Prop"))$ and passes ($?_(mono("And")_m) : mono("Proper") (mono("relation") mono("Prop")) space ?_(mono("And")_r) mono("And")$) for the proof of identity. The next term we consider, $p$, does indeed unify with $p$ and is therefore replaced with $q$. For now, the return value for the proof will be just $h$. After the two recursive `Rew`-invocations terminate, we combine the proofs and carrier relations for a proof of $r space (mono("And") space p) space (mono("And") space q)$. We start with another hole $?_(mono("And")_p) : "Subrelation" ?_(mono("And")_r) (r ==> (?_T : "relation (Prop" -> "Prop)"))$. Recall that `Subrelation` is a typeclass with only constructor `subrelation`. Thus, any metavariable of type `Subrelation` must be of that constructor and eventually unfolds to $forall r_1 space r_2, ?_"rel" space r_1 space r_2 -> forall x space y, r space x space y -> ?_T space x space y -> ?_T (r_1 space x) space (r_2 space y)$. This allows us to construct the desired proof by carefully applying the arguments $?_(mono("And")_p) mono("And") mono("And") space ?_(mono("And")_m) space p space q space h$. By instantiating $r_1$ and $r_2$ with the `And` relation and $x$ and $y$ with $p$ and $q$, we receive our desired rewrite proof for this part of the term $?_(mono("And")_T) $. |
200 | 200 |
|
|
0 commit comments