diff -r cdc96d79bbba -r 6ba52f3a1542 Pearl-jv/Paper.thy --- a/Pearl-jv/Paper.thy Sat Apr 09 00:29:40 2011 +0100 +++ b/Pearl-jv/Paper.thy Sat Apr 09 13:44:49 2011 +0800 @@ -67,7 +67,11 @@ At its core Nominal Isabelle is based on the nominal logic work by Pitts at al \cite{GabbayPitts02,Pitts03}, whose most basic notion is a sort-respecting permutation operation defined over a countably - infinite collection of sorted atoms. The aim of this paper is to + infinite collection of sorted atoms. + + + + The aim of this paper is to describe how we adapted this work so that it can be implemented in a theorem prover based on Higher-Order Logic (HOL). For this we present the definition we made in the implementation and also review @@ -314,11 +318,11 @@ \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%% \begin{tabular}{@ {}l} - @{thm add_assoc[where a="\\<^isub>1" and b="\\<^isub>2" and c="\\<^isub>3"]}\\ - @{thm monoid_add_class.add_0_left[where a="\::perm"]} \hspace{9mm} - @{thm monoid_add_class.add_0_right[where a="\::perm"]} \hspace{9mm} - @{thm group_add_class.left_minus[where a="\::perm"]} - \end{tabular} + i)~~@{thm add_assoc[where a="\\<^isub>1" and b="\\<^isub>2" and c="\\<^isub>3"]}\\ + ii)~~@{thm monoid_add_class.add_0_left[where a="\::perm"]} \hspace{9mm} + iii)~~@{thm monoid_add_class.add_0_right[where a="\::perm"]} \hspace{9mm} + iv)~~@{thm group_add_class.left_minus[where a="\::perm"]} + \end{tabular}\hfill\numbered{grouplaws} \end{isabelle} \noindent @@ -327,23 +331,28 @@ come in handy when we have to do calculations with permutations. Note that Isabelle/HOL defies standard conventions of mathematical notation by using additive syntax even for non-commutative groups. Obviously, - composition of permutations is not commutative in general, because @{text - "\\<^sub>1 + \\<^sub>2 \ \\<^sub>2 + \\<^sub>1"}. But since the point of this paper is to implement the + composition of permutations is not commutative in general; for example + + \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%% + @{text "(a b) + (b c) \ (b c) + (a b)"} + \end{isabelle} + + \noindent + But since the point of this paper is to implement the nominal theory as smoothly as possible in Isabelle/HOL, we tolerate the non-standard notation in order to reuse the existing libraries. - A \emph{permutation operation}, written @{text "\ \ x"}, applies a permutation - @{text "\"} to an object @{text "x"} of type @{text \}, say. - This operation has the type + A \emph{permutation operation}, written infix as @{text "\ \ x"}, + applies a permutation @{text "\"} to an object @{text "x"} of type + @{text \}, say. This operation has the type \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%% @{text "_ \ _ :: perm \ \ \ \"} \end{isabelle} \noindent - Isabelle/HOL will allow us to give a definition of this operation for - `base' types, such as atoms, permutations, booleans and natural numbers, - and for type-constructors, such as functions, sets, lists and products. + Isabelle/HOL allows us to give a definition of this operation for + `base' types, such as atoms, permutations, booleans and natural numbers: \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%% \begin{tabular}{@ {}l@ {\hspace{4mm}}l@ {}} @@ -351,12 +360,20 @@ permutations: & @{thm permute_perm_def[where p="\" and q="\'", THEN eq_reflection]}\\ booleans: & @{thm permute_bool_def[where p="\", no_vars, THEN eq_reflection]}\\ nats: & @{thm permute_nat_def[where p="\", no_vars, THEN eq_reflection]}\\ + \end{tabular}\hfill\numbered{permdefsbase} + \end{isabelle} + + \noindent + and for type-constructors, such as functions, sets, lists and products: + + \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%% + \begin{tabular}{@ {}l@ {\hspace{4mm}}l@ {}} functions: & @{text "\ \ f \ \x. \ \ (f ((-\) \ x))"}\\ sets: & @{thm permute_set_eq[where p="\", no_vars, THEN eq_reflection]}\\ lists: & @{thm permute_list.simps(1)[where p="\", no_vars, THEN eq_reflection]}\\ & @{thm permute_list.simps(2)[where p="\", no_vars, THEN eq_reflection]}\\ products: & @{thm permute_prod.simps[where p="\", no_vars, THEN eq_reflection]}\\ - \end{tabular}\hfill\numbered{permdefs} + \end{tabular}\hfill\numbered{permdefsconstrs} \end{isabelle} In order to reason abstractly about this operation, @@ -371,8 +388,9 @@ \end{isabelle} \noindent - From these properties and the laws about groups follows that a - permutation and its inverse cancel each other. That is + From these properties and the laws about groups + (\ref{grouplaws}.{\it iv }) follows that a permutation and its inverse + cancel each other. That is \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%% \begin{tabular}{@ {}l} @@ -447,16 +465,16 @@ and establish: \begin{theorem} - If @{text \}, @{text "\\<^isub>1"} and @{text "\\<^isub>2"} are permutation types, - then so are @{text "atom"}, @{text "\\<^isub>1 \ \\<^isub>2"}, - @{text perm}, @{term "\ set"}, @{term "\ list"}, @{term "\\<^isub>1 \ \\<^isub>2"}, - @{text bool} and @{text "nat"}. + The types @{type atom}, @{type perm}, @{type bool} and @{type nat} + are permutation types, and if @{text \}, @{text "\\<^isub>1"} and @{text + "\\<^isub>2"} are permutation types, then so are \mbox{@{text "\\<^isub>1 \ \\<^isub>2"}}, + @{text "\ set"}, @{text "\ list"} and @{text "\\<^isub>1 \ \\<^isub>2"}. \end{theorem} \begin{proof} All statements are by unfolding the definitions of the permutation operations and simple calculations involving addition and - minus. With permutations for example we have + minus. In case of permutations for example we have \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%% \begin{tabular}[b]{@ {}rcl} @@ -487,6 +505,8 @@ @{text x} for variables. We assume HOL-terms are fully typed, but for the sake of greater legibility we leave the typing information implicit. We also assume the usual notions for free and bound variables of a HOL-term. + It is custom in HOL to regard terms equal modulo alpha-, beta- and + eta-equivalence. An equivariant HOL-term is one that is invariant under the permutation operation. This notion can be defined in Isabelle/HOL @@ -498,6 +518,10 @@ \end{definition} \noindent + We will be mainly interested in the case where @{text t} is a constant, but + of course there is no way to restrict this definition in Isabelle/HOL to just + this case. + There are a number of equivalent formulations for the equivariance property. For example, assuming @{text t} is of permutation type @{text "\ \ \"}, then equivariance can also be stated as @@ -530,12 +554,11 @@ \noindent using the permutation operation on booleans and property - \eqref{permuteequ}. - Lemma~\ref{permutecompose} establishes that the + \eqref{permuteequ}. Lemma~\ref{permutecompose} establishes that the permutation operation is equivariant. The permutation operation for - lists and products, shown in \eqref{permdefs}, state that the + lists and products, shown in \eqref{permdefsconstrs}, state that the constructors for products, @{text "Nil"} and @{text Cons} are - equivariant. Furthermore a simple calculation will show that our + equivariant. Furthermore a simple calculation will show that our swapping functions are equivariant, that is \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%% @@ -552,21 +575,102 @@ "\"} and @{text "\"} are all equivariant (see ??? intro) In contrast, the advantage of Definition \ref{equivariance} is that - it leads to a simple rewrite system. Consider the following oriented - versions + it leads to a simple rewrite system that allows us to `push' a + permutation towards the leaves in a HOL-term (i.e.~constants and free + variables). The permutation disappears in cases where the + constants are equivariant. Given a database of equivariant constants, + we can implement a decision procedure that helps to find out when + a compound term is equivariant. A permutation @{text \} can be pushed over + applications and abstractions in a HOL-term as follows + + \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%% + \begin{tabular}{@ {}lrcl} + i) & @{text "\ \ (t\<^isub>1 t\<^isub>2)"} & @{text "\"} & @{term "(\ \ t\<^isub>1) (\ \ t\<^isub>2)"}\\ + ii) & @{text "\ \ (\x. t)"} & @{text "\"} & @{text "\x. \ \ (t[x := (-\) \ x])"}\\ + \end{tabular}\hfill\numbered{rewriteapplam} + \end{isabelle} + + \noindent + The first rewrite rule we established as equation in \eqref{permutefunapp}; + the second follows from the definition of permutations acting on functions + and the fact that HOL-terms are considered equal modulo beta-reduction. + The inverse permutations that are introduced by the second rewrite rule + can be removed using the additional rewrite rule + + \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%% + \begin{tabular}{@ {}rcl} + @{term "\ \ (- \) \ x"} & @{text "\"} & @{term "x"} + \end{tabular}\hfill\numbered{rewriteunpermute} + \end{isabelle} + + \noindent + and permutations in front of equivariant constants can be removed by the + rule \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%% \begin{tabular}{@ {}rcl} - @{text "\ \ (t\<^isub>1 t\<^isub>2)"} & @{text "\"} & @{term "(\ \ t\<^isub>1) (\ \ t\<^isub>2)"}\\ - @{text "\ \ (\x. t)"} & @{text "\"} & @{text "\x. \ \ (t[x := (-\) \ x])"}\\ - @{term "\ \ (- \) \ x"} & @{text "\"} & @{term "x"}\\ - \end{tabular}\hfill\numbered{swapeqvt} + @{term "\ \ c"} & @{text "\"} & @{term "c"} + \end{tabular}\hfill\numbered{rewriteconsts} \end{isabelle} - - of the equations -together with the permutation + \noindent + By a meta-argument, that means one we cannot formalise inside + Isabelle/HOL, we can convince ourselves that the rewriting process + of first pushing a permutation inside a term using + \eqref{rewriteapplam}, then removing permutation in front of bound + variables using \eqref{rewriteunpermute} and finally removing all + permutations in front of equivariant constants must terminate: the + size of the term gets smaller in \eqref{rewriteapplam} if we do not + count the inverse permutations introduced by the second rewrite rule + and there can only be finitely many bound variables in a term + (similarly equivariant constants), therefore only finitely many + applications of \eqref{rewriteunpermute} and + \eqref{rewriteconsts}. The only problem is that instances where + rules (\ref{rewriteapplam}.{\it i}) or rules + \eqref{rewriteunpermute} apply `overlap' and potentially our measure + increases. Consider for example the term @{term "\ \ (\x. x)"}, + whose final reduct should just be \mbox{@{term "\x. x"}}. Using + (\ref{rewriteapplam}.{\it ii}), the term can reduce to @{term + "\x. \ \ (- \) \ x"}, to which we can apply \eqref{rewriteunpermute} + in order to obtain the desired result. However, the subterm term @{text "(- + \) \ x"} is also an application (in fact three nested ones). Therefore + @{term "\x. \ \ (- \) \ x"} can also rewrite + to @{text "\x. (- (\ \ \)) \ (\ \ x)"} to which we cannot apply + (\ref{rewriteapplam}.{\it i}) directly and even worse our measure + increased. In order to distinguish situations where \eqref{rewriteconsts} + should apply instead of (\ref{rewriteapplam}.{\it i}) we use a standard + trick in HOL. We introduce a separate definition for terms of the form + @{text "(- \) \ x"}, namely as + + \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%% + @{term "unpermute \ x \ (- \) \ x"} + \end{isabelle} + + \noindent + With this trick, we can reformulate our rewrite rules pushing a permutation @{text \} + inside a term as follows + + \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%% + \begin{tabular}{@ {}lrcl} + i) & @{text "\ \ (t\<^isub>1 t\<^isub>2)"} & @{text "\"} & @{term "(\ \ t\<^isub>1) (\ \ t\<^isub>2)"}\hspace{45mm}\mbox{}\\ + \multicolumn{4}{r}{provided @{text "t\<^isub>1 t\<^isub>2"} is not of the form @{text "unpermute \ x"}}\smallskip\\ + ii) & @{text "\ \ (\x. t)"} & @{text "\"} & @{text "\x. \ \ (t[x := unpermute \ x])"}\\ + iii) & @{text "\ \ (unpermute \ x)"} & @{text "\"} & @{term x}\\ + iv) & @{term "\ \ c"} & @{text "\"} & @{term "c"}\hspace{6mm}provided @{text c} is equivariant\\ + \end{tabular} + \end{isabelle} + + \noindent + In this rewrite systems no rule overlaps. Taking the measure + consisting of lexicographically ordered pairs whose first component + is the size of the term (without counting @{text unpermutes}) and + the second is the number of occurences of @{text "unpermute \ x"} + and @{text "\ \ c"}, then each rule is strictly decreasing. + Consequently the process of applying these rules must terminate. + + + together with the permutation operation for functions and the properties in \eqref{permutefunapp} and \eqref{cancel} leads to a simple rewrite system that pushes permutations inside a term until they reach either function constants or variables. The permutations in front of @@ -599,7 +703,7 @@ \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%% \begin{tabular}[b]{@ {}c@ {\hspace{2mm}}l@ {\hspace{8mm}}l} & @{text "\ \ (\x. t)"}\\ - @{text "\"} & @{text "\y. \ \ ((\x. t) ((-\) \ y))"} & by \eqref{permdefs}\\ + @{text "\"} & @{text "\y. \ \ ((\x. t) ((-\) \ y))"} & by \eqref{permdefsconstrs}\\ \end{tabular}\hfill\qed \end{isabelle}