Quotient-Paper/Paper.thy
changeset 2224 f5b6f9d8a882
parent 2223 c474186439bd
child 2225 cbffed7d81bd
equal deleted inserted replaced
2223:c474186439bd 2224:f5b6f9d8a882
     9 
     9 
    10 notation (latex output)
    10 notation (latex output)
    11   rel_conj ("_ OOO _" [53, 53] 52) and
    11   rel_conj ("_ OOO _" [53, 53] 52) and
    12   "op -->" (infix "\<rightarrow>" 100) and
    12   "op -->" (infix "\<rightarrow>" 100) and
    13   "==>" (infix "\<Rightarrow>" 100) and
    13   "==>" (infix "\<Rightarrow>" 100) and
    14   fun_map (infix "\<longrightarrow>" 51) and
    14   fun_map ("_ \<^raw:\mbox{\tt{---\textgreater}}> _" 51) and
    15   fun_rel (infix "\<Longrightarrow>" 51) and
    15   fun_rel ("_ \<^raw:\mbox{\tt{===\textgreater}}> _" 51) and
    16   list_eq (infix "\<approx>" 50) and (* Not sure if we want this notation...? *)
    16   list_eq (infix "\<approx>" 50) and (* Not sure if we want this notation...? *)
    17   fempty ("\<emptyset>") and
    17   fempty ("\<emptyset>") and
    18   funion ("_ \<union> _") and
    18   funion ("_ \<union> _") and
    19   finsert ("{_} \<union> _") and 
    19   finsert ("{_} \<union> _") and 
    20   Cons ("_::_") and
    20   Cons ("_::_") and
    69   This constructions yields the new type @{typ int} and definitions for @{text
    69   This constructions yields the new type @{typ int} and definitions for @{text
    70   "0"} and @{text "1"} of type @{typ int} can be given in terms of pairs of
    70   "0"} and @{text "1"} of type @{typ int} can be given in terms of pairs of
    71   natural numbers (namely @{text "(0, 0)"} and @{text "(1, 0)"}). Operations
    71   natural numbers (namely @{text "(0, 0)"} and @{text "(1, 0)"}). Operations
    72   such as @{text "add"} with type @{typ "int \<Rightarrow> int \<Rightarrow> int"} can be defined in
    72   such as @{text "add"} with type @{typ "int \<Rightarrow> int \<Rightarrow> int"} can be defined in
    73   terms of operations on pairs of natural numbers (namely @{text
    73   terms of operations on pairs of natural numbers (namely @{text
    74   "add\<^bsub>nat\<times>nat\<^esub> (n\<^isub>1, m\<^isub>1) (n\<^isub>2,
    74   "add_pair (n\<^isub>1, m\<^isub>1) (n\<^isub>2,
    75   m\<^isub>2) \<equiv> (n\<^isub>1 + n\<^isub>2, m\<^isub>1 + m\<^isub>2)"}).
    75   m\<^isub>2) \<equiv> (n\<^isub>1 + n\<^isub>2, m\<^isub>1 + m\<^isub>2)"}).
    76   Similarly one can construct the type of finite sets, written @{term "\<alpha> fset"}, 
    76   Similarly one can construct the type of finite sets, written @{term "\<alpha> fset"}, 
    77   by quotienting the type @{text "\<alpha> list"} according to the equivalence relation
    77   by quotienting the type @{text "\<alpha> list"} according to the equivalence relation
    78 
    78 
    79   @{text [display, indent=10] "xs \<approx> ys \<equiv> (\<forall>x. x \<in> xs \<longleftrightarrow> x \<in> ys)"}
    79   @{text [display, indent=10] "xs \<approx> ys \<equiv> (\<forall>x. memb x xs \<longleftrightarrow> memb x ys)"}
    80 
    80 
    81   \noindent
    81   \noindent
    82   which states that two lists are equivalent if every element in one list is also
    82   which states that two lists are equivalent if every element in one list is
    83   member in the other (@{text "\<in>"} stands here for membership in lists). The
    83   also member in the other. The empty finite set, written @{term "{||}"}, can
    84   empty finite set, written @{term "{||}"}, can then be defined as the 
    84   then be defined as the empty list and the union of two finite sets, written
    85   empty list and the union of two finite sets, written @{text "\<union>"}, as list append. 
    85   @{text "\<union>"}, as list append.
    86 
    86 
    87   An area where quotients are ubiquitous is reasoning about programming language
    87   An area where quotients are ubiquitous is reasoning about programming language
    88   calculi. A simple example is the lambda-calculus, whose ``raw'' terms are defined as
    88   calculi. A simple example is the lambda-calculus, whose ``raw'' terms are defined as
    89 
    89 
    90   @{text [display, indent=10] "t ::= x | t t | \<lambda>x.t"}
    90   @{text [display, indent=10] "t ::= x | t t | \<lambda>x.t"}
   129   \draw (-2.0,-0.195)  -- (0.7,-0.195);
   129   \draw (-2.0,-0.195)  -- (0.7,-0.195);
   130 
   130 
   131   \draw ( 0.7, 0.23) node {\begin{tabular}{@ {}c@ {}}equiv-\\[-1mm]clas.\end{tabular}};
   131   \draw ( 0.7, 0.23) node {\begin{tabular}{@ {}c@ {}}equiv-\\[-1mm]clas.\end{tabular}};
   132   \draw (-2.45, 0.35) node {\begin{tabular}{@ {}c@ {}}new\\[-1mm]type\end{tabular}};
   132   \draw (-2.45, 0.35) node {\begin{tabular}{@ {}c@ {}}new\\[-1mm]type\end{tabular}};
   133   \draw (1.8, 0.35) node[right=-0.1mm]
   133   \draw (1.8, 0.35) node[right=-0.1mm]
   134     {\begin{tabular}{@ {}l@ {}}existing\\[-1mm] type\\ (sets of raw terms)\end{tabular}};
   134     {\begin{tabular}{@ {}l@ {}}existing\\[-1mm] type\\ (sets of raw elements)\end{tabular}};
   135   \draw (0.9, -0.55) node {\begin{tabular}{@ {}l@ {}}non-empty\\[-1mm]subset\end{tabular}};
   135   \draw (0.9, -0.55) node {\begin{tabular}{@ {}l@ {}}non-empty\\[-1mm]subset\end{tabular}};
   136   
   136   
   137   \draw[->, very thick] (-1.8, 0.36) -- (-0.1,0.36);
   137   \draw[->, very thick] (-1.8, 0.36) -- (-0.1,0.36);
   138   \draw[<-, very thick] (-1.8, 0.16) -- (-0.1,0.16);
   138   \draw[<-, very thick] (-1.8, 0.16) -- (-0.1,0.16);
   139   \draw (-0.95, 0.26) node[above=0.4mm] {@{text Rep}};
   139   \draw (-0.95, 0.26) node[above=0.4mm] {@{text Rep}};
   143   \end{center}
   143   \end{center}
   144 
   144 
   145   \noindent
   145   \noindent
   146   The starting point is an existing type over which an equivalence relation
   146   The starting point is an existing type over which an equivalence relation
   147   given by the user is defined. With this input the package introduces a new
   147   given by the user is defined. With this input the package introduces a new
   148   type, which comes with two associated abstraction and representation
   148   type that comes with associated \emph{abstraction} and \emph{representation}
   149   functions, written @{text Abs} and @{text Rep}. These functions relate
   149   functions, written @{text Abs} and @{text Rep}. These functions relate
   150   elements in the existing and new type, and can be uniquely identified by
   150   elements in the existing type to ones in the new type and vice versa; they
   151   their type (which however we often omit for better readability). They
   151   can be uniquely identified by their type. For example for the integer
   152   represent an isomorphism between the non-empty subset and elements in the
   152   quotient construction the types of @{text Abs} and @{text Rep} are
   153   new type. They are necessary for making definitions involving the new type. For
   153 
   154   example @{text "0"} and @{text "1"} of type @{typ int} can be defined as
   154   \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%%
       
   155   @{text "Abs::nat \<times> nat \<Rightarrow> int"}\hspace{10mm}@{text "Rep::int \<Rightarrow> nat \<times> nat"}
       
   156   \end{isabelle}
       
   157 
       
   158   \noindent
       
   159   However we often leave this typing information implicit for better
       
   160   readability. Every abstraction and representation function represents an
       
   161   isomorphism between the non-empty subset and elements in the new type. They
       
   162   are necessary for making definitions involving the new type. For example
       
   163   @{text "0"} and @{text "1"} of type @{typ int} can be defined as
       
   164 
   155 
   165 
   156   \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%%
   166   \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%%
   157   @{text "0 \<equiv> Abs (0, 0)"}\hspace{10mm}@{text "1 \<equiv> Abs (1, 0)"}
   167   @{text "0 \<equiv> Abs (0, 0)"}\hspace{10mm}@{text "1 \<equiv> Abs (1, 0)"}
   158   \end{isabelle}
   168   \end{isabelle}
   159 
   169 
   160   \noindent
   170   \noindent
   161   Slightly more complicated is the definition of @{text "add"} which has type 
   171   Slightly more complicated is the definition of @{text "add"} having type 
   162   @{typ "int \<Rightarrow> int \<Rightarrow> int"}. Its definition is as follows
   172   @{typ "int \<Rightarrow> int \<Rightarrow> int"}. Its definition is as follows
   163 
   173 
   164   @{text [display, indent=10] "add n m \<equiv> Abs (add\<^bsub>nat\<times>nat\<^esub> (Rep n) (Rep m))"}
   174   @{text [display, indent=10] "add n m \<equiv> Abs (add_pair (Rep n) (Rep m))"}
   165   
   175   
   166   \noindent
   176   \noindent
   167   where we have to take first the representation of @{text n} and @{text m},
   177   where we take the representation of the arguments @{text n} and @{text m},
   168   add them according to @{text "add\<^bsub>nat\<times>nat\<^esub>"} and then take the
   178   add them according to @{text "add_pair"} and then take the
   169   abstraction of the result.  This is all straightforward and the existing
   179   abstraction of the result.  This is all straightforward and the existing
   170   quotient packages can deal with such definitions. But what is surprising
   180   quotient packages can deal with such definitions. But what is surprising
   171   that none of them can deal with slightly more complicated definitions involving
   181   that none of them can deal with slightly more complicated definitions involving
   172   \emph{compositions} of quotients. Such compositions are needed for example
   182   \emph{compositions} of quotients. Such compositions are needed for example
   173   in case of finite sets.  For example over lists one can define an operator
   183   in case of quotienting lists and the operator that flattens lists of lists, defined 
   174   that flattens lists of lists as follows
   184   as follows
   175 
   185 
   176   @{thm [display, indent=10] concat.simps(1) concat.simps(2)[no_vars]}
   186   @{thm [display, indent=10] concat.simps(1) concat.simps(2)[no_vars]}
   177 
   187 
   178   \noindent
   188   \noindent
   179   We expect that the corresponding operator on finite sets, written @{term "fconcat"},
   189   We expect that the corresponding operator on finite sets, written @{term "fconcat"},
   181 
   191 
   182   @{thm [display, indent=10] fconcat_empty[no_vars] fconcat_insert[no_vars]}
   192   @{thm [display, indent=10] fconcat_empty[no_vars] fconcat_insert[no_vars]}
   183 
   193 
   184   \noindent
   194   \noindent
   185   The quotient package should provide us with a definition for @{text "\<Union>"} in
   195   The quotient package should provide us with a definition for @{text "\<Union>"} in
   186   terms of @{text flat}, @{text Rep} and @{text Abs}. The problem is that it
   196   terms of @{text flat}, @{text Rep} and @{text Abs} (the latter two having
   187   is not enough to just take the representation of the argument and then
   197   the type @{text "\<alpha> fset \<Rightarrow> \<alpha> list"} and @{text "\<alpha> list \<Rightarrow> \<alpha> fset"},
   188   take the abstraction of the result, because in that case we obtain an operator
   198   respectively). The problem is that the method is used in the existing quotient
   189   with type @{text "(\<alpha> list) fset \<Rightarrow> \<alpha> fset"}, not the expected 
   199   packages of just taking the representation of the arguments and then take
   190   @{text "(\<alpha> fset) fset \<Rightarrow> \<alpha> fset"}. It turns out that we need
   200   the abstraction of the result is \emph{not} enough. The reason is that case in case
   191   to build aggregate representation and abstraction functions, which in
   201   of @{text "\<Union>"} we obtain the incorrect definition
   192   case of @{term "fconcat"} produce the following definition
   202 
   193 
   203   @{text [display, indent=10] "\<Union> S \<equiv> Abs (flat (Rep S))"}
   194   @{text [display, indent=10] "\<Union> S \<equiv> Abs (concat ((map Rep \<circ> Rep) S))"}
   204 
       
   205   \noindent
       
   206   where the right-hand side is not even typable! This problem can be remedied in the
       
   207   existing quotient packages by introducing an intermediate step and reasoning
       
   208   about faltening of lists of finite sets. However, this remedy is rather
       
   209   cumbersome and inelegant in light of our work, which can deal with such
       
   210   definitions directly. The solution is that we need to build aggregate
       
   211   representation and abstraction functions, which in case of @{text "\<Union>"}
       
   212   generate the following definition
       
   213 
       
   214   @{text [display, indent=10] "\<Union> S \<equiv> Abs (flat ((map Rep \<circ> Rep) S))"}
   195 
   215 
   196   \noindent
   216   \noindent
   197   where @{term map} is the usual mapping function for lists. In this paper we
   217   where @{term map} is the usual mapping function for lists. In this paper we
   198   will present a formal definition for our aggregate abstraction and
   218   will present a formal definition of our aggregate abstraction and
   199   representation functions (this definition was omitted in \cite{Homeier05}). 
   219   representation functions (this definition was omitted in \cite{Homeier05}). 
   200   They generate definitions like the one above for @{text add} and @{text "\<Union>"} 
   220   They generate definitions, like the one above for @{text "\<Union>"}, 
   201   according to the type of the ``raw'' constant and the type
   221   according to the type of the ``raw'' constant and the type
   202   of the quotient constant.
   222   of the quotient constant. This means we also have to extend the notions
   203 
   223   of \emph{respectfulness} and \emph{preservation} from Homeier 
   204 
   224   \cite{Homeier05}.
   205 *}
   225 
   206 
   226   We will also address the criticism by Paulson \cite{Paulson06} cited at the
   207 subsection {* Contributions *}
   227   beginning of this section about having to collect theorems that are lifted from the raw
   208 
   228   level to the quotient level. Our quotient package is modular so that it
   209 text {*
   229   allows to lift single theorems separately. This has the advantage for the
   210   We present the detailed lifting procedure, which was not shown before.
   230   user to develop a formal theory interactively an a natural progression. A
   211 
   231   pleasing result of the modularity is also that we are able to clearly
   212   The quotient package presented in this paper has the following
   232   specify what needs to be done in the lifting process (this was only hinted at in
   213   advantages over existing packages:
   233   \cite{Homeier05} and implemented as a ``rough recipe'' in ML-code).
   214   \begin{itemize}
   234  
   215 
   235   The paper is organised as follows \ldots
   216   \item We define quotient composition, function map composition and
   236 *}
   217     relation map composition. This lets lifting polymorphic types with
   237 
   218     subtypes quotiented as well. We extend the notions of
   238 
   219     respectfulness and preservation to cope with quotient
   239 
   220     composition.
   240 section {* Preliminaries and General Quotient\label{sec:prelims} *}
   221 
       
   222   \item We allow lifting only some occurrences of quotiented
       
   223     types. Rsp/Prs extended. (used in nominal)
       
   224 
       
   225   \item The quotient package is very modular. Definitions can be added
       
   226     separately, rsp and prs can be proved separately, Quotients and maps
       
   227     can be defined separately and theorems can
       
   228     be lifted on a need basis. (useful with type-classes).
       
   229 
       
   230   \item Can be used both manually (attribute, separate tactics,
       
   231     rsp/prs databases) and programatically (automated definition of
       
   232     lifted constants, the rsp proof obligations and theorem statement
       
   233     translation according to given quotients).
       
   234 
       
   235   \end{itemize}
       
   236 *}
       
   237 
       
   238 section {* General Quotient *}
       
   239 
   241 
   240 
   242 
   241 
   243 
   242 text {*
   244 text {*
   243   In this section we present the definitions of a quotient that follow
   245   In this section we present the definitions of a quotient that follow
   287       @{text "|"} & @{text "\<lambda>x\<^isup>\<sigma>. t"} & \textrm{(abstraction)} \\ \nonumber
   289       @{text "|"} & @{text "\<lambda>x\<^isup>\<sigma>. t"} & \textrm{(abstraction)} \\ \nonumber
   288   \end{eqnarray}
   290   \end{eqnarray}
   289 
   291 
   290 *}
   292 *}
   291 
   293 
   292 section {* Constants *}
   294 section {* Lifting of Definitions *}
   293 
   295 
   294 (* Say more about containers? *)
   296 (* Say more about containers? *)
   295 
   297 
   296 text {*
   298 text {*
   297 
   299 
   719   \end{itemize}
   721   \end{itemize}
   720 *}
   722 *}
   721 
   723 
   722 section {* Conclusion *}
   724 section {* Conclusion *}
   723 
   725 
       
   726 text {*
       
   727   The package is part of the standard distribution of Isabelle.
       
   728 *}
       
   729 
       
   730 
       
   731 subsection {* Contributions *}
       
   732 
       
   733 text {*
       
   734   We present the detailed lifting procedure, which was not shown before.
       
   735 
       
   736   The quotient package presented in this paper has the following
       
   737   advantages over existing packages:
       
   738   \begin{itemize}
       
   739 
       
   740   \item We define quotient composition, function map composition and
       
   741     relation map composition. This lets lifting polymorphic types with
       
   742     subtypes quotiented as well. We extend the notions of
       
   743     respectfulness and preservation to cope with quotient
       
   744     composition.
       
   745 
       
   746   \item We allow lifting only some occurrences of quotiented
       
   747     types. Rsp/Prs extended. (used in nominal)
       
   748 
       
   749   \item The quotient package is very modular. Definitions can be added
       
   750     separately, rsp and prs can be proved separately, Quotients and maps
       
   751     can be defined separately and theorems can
       
   752     be lifted on a need basis. (useful with type-classes).
       
   753 
       
   754   \item Can be used both manually (attribute, separate tactics,
       
   755     rsp/prs databases) and programatically (automated definition of
       
   756     lifted constants, the rsp proof obligations and theorem statement
       
   757     translation according to given quotients).
       
   758 
       
   759   \end{itemize}
       
   760 *}
       
   761 
       
   762 
   724 (*<*)
   763 (*<*)
   725 end
   764 end
   726 (*>*)
   765 (*>*)