Quotient-Paper/Paper.thy
changeset 2274 99cf23602d36
parent 2273 d62c082cb56b
child 2275 69b80ad616c5
equal deleted inserted replaced
2273:d62c082cb56b 2274:99cf23602d36
    22  
    22  
    23   
    23   
    24 
    24 
    25 ML {*
    25 ML {*
    26 fun nth_conj n (_, r) = nth (HOLogic.dest_conj r) n;
    26 fun nth_conj n (_, r) = nth (HOLogic.dest_conj r) n;
       
    27 
    27 fun style_lhs_rhs proj = Scan.succeed (fn ctxt => fn t =>
    28 fun style_lhs_rhs proj = Scan.succeed (fn ctxt => fn t =>
    28   let
    29   let
    29     val concl =
    30     val concl =
    30       Object_Logic.drop_judgment (ProofContext.theory_of ctxt) (Logic.strip_imp_concl t)
    31       Object_Logic.drop_judgment (ProofContext.theory_of ctxt) (Logic.strip_imp_concl t)
    31   in
    32   in
    32     case concl of (_ $ l $ r) => proj (l, r)
    33     case concl of (_ $ l $ r) => proj (l, r)
    33     | _ => error ("Binary operator expected in term: " ^ Syntax.string_of_term ctxt concl)
    34     | _ => error ("Binary operator expected in term: " ^ Syntax.string_of_term ctxt concl)
    34   end);
    35   end);
    35 *}
    36 *}
       
    37 
    36 setup {*
    38 setup {*
    37   Term_Style.setup "rhs1" (style_lhs_rhs (nth_conj 0)) #>
    39   Term_Style.setup "rhs1" (style_lhs_rhs (nth_conj 0)) #>
    38   Term_Style.setup "rhs2" (style_lhs_rhs (nth_conj 1)) #>
    40   Term_Style.setup "rhs2" (style_lhs_rhs (nth_conj 1)) #>
    39   Term_Style.setup "rhs3" (style_lhs_rhs (nth_conj 2))
    41   Term_Style.setup "rhs3" (style_lhs_rhs (nth_conj 2))
    40 *}
    42 *}
       
    43 
    41 (*>*)
    44 (*>*)
    42 
    45 
    43 
    46 
    44 section {* Introduction *}
    47 section {* Introduction *}
    45 
    48 
   253 
   256 
   254   The paper is organised as follows: Section \ref{sec:prelims} presents briefly
   257   The paper is organised as follows: Section \ref{sec:prelims} presents briefly
   255   some necessary preliminaries; Section \ref{sec:type} describes the definitions 
   258   some necessary preliminaries; Section \ref{sec:type} describes the definitions 
   256   of quotient types and shows how definitions of constants can be made over 
   259   of quotient types and shows how definitions of constants can be made over 
   257   quotient types. Section \ref{sec:resp} introduces the notions of respectfullness
   260   quotient types. Section \ref{sec:resp} introduces the notions of respectfullness
   258   and preservation; Section \ref{sec:lift} describes the lifting of theorems, 
   261   and preservation; Section \ref{sec:lift} describes the lifting of theorems;
       
   262   Section \ref{sec:examples} presents some examples
   259   and Section \ref{sec:conc} concludes and compares our results to existing 
   263   and Section \ref{sec:conc} concludes and compares our results to existing 
   260   work.
   264   work.
   261 *}
   265 *}
   262 
   266 
   263 section {* Preliminaries and General Quotients\label{sec:prelims} *}
   267 section {* Preliminaries and General Quotients\label{sec:prelims} *}
   264 
   268 
   265 text {*
   269 text {*
   266   We describe in this section briefly the most basic notions of HOL we rely on, and 
   270   We give in this section a crude overview of HOL and describe the main
   267   the important definitions given by Homeier for quotients \cite{Homeier05}.
   271   definitions given by Homeier for quotients \cite{Homeier05}.
   268 
   272 
   269   At its core HOL is based on a simply-typed term language, where types are 
   273   At its core, HOL is based on a simply-typed term language, where types are 
   270   recorded in Church-style fashion (that means, we can always infer the type of 
   274   recorded in Church-style fashion (that means, we can always infer the type of 
   271   a term and its subterms without any additional information). The grammars
   275   a term and its subterms without any additional information). The grammars
   272   for types and terms are as follows
   276   for types and terms are as follows
   273 
   277 
   274   \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%%
   278   \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%%
   281 
   285 
   282   \noindent
   286   \noindent
   283   We often write just @{text \<kappa>} for @{text "() \<kappa>"}, and use @{text "\<alpha>s"} and
   287   We often write just @{text \<kappa>} for @{text "() \<kappa>"}, and use @{text "\<alpha>s"} and
   284   @{text "\<sigma>s"} to stand for collections of type variables and types,
   288   @{text "\<sigma>s"} to stand for collections of type variables and types,
   285   respectively.  The type of a term is often made explicit by writing @{text
   289   respectively.  The type of a term is often made explicit by writing @{text
   286   "t :: \<sigma>"}. HOL includes a type @{typ bool} for booleans and the function 
   290   "t :: \<sigma>"}. HOL includes a type @{typ bool} for booleans and the function
   287   type, written @{text "\<sigma> \<Rightarrow> \<tau>"}. HOL also contains
   291   type, written @{text "\<sigma> \<Rightarrow> \<tau>"}. HOL also contains many primitive and defined
   288   many primitive and defined constants; this includes equality, with type @{text "= :: \<sigma> \<Rightarrow>
   292   constants; a primitive constant is equality, with type @{text "= :: \<sigma> \<Rightarrow> \<sigma> \<Rightarrow>
   289   \<sigma> \<Rightarrow> bool"}, and the identity function, with type @{text "id :: \<sigma> => \<sigma>"} (the former
   293   bool"}, and the identity function with type @{text "id :: \<sigma> => \<sigma>"} is
   290   being primitive and the latter being defined as @{text
   294   defined as @{text "\<lambda>x\<^sup>\<sigma>. x\<^sup>\<sigma>"}).
   291   "\<lambda>x\<^sup>\<sigma>. x\<^sup>\<sigma>"}).
       
   292 
   295 
   293   An important point to note is that theorems in HOL can be seen as a subset
   296   An important point to note is that theorems in HOL can be seen as a subset
   294   of terms that are constructed specially (namely through axioms and prove
   297   of terms that are constructed specially (namely through axioms and prove
   295   rules). As a result we are able to define automatic proof
   298   rules). As a result we are able to define automatic proof
   296   procedures showing that one theorem implies another by decomposing the term
   299   procedures showing that one theorem implies another by decomposing the term
   297   underlying the first theorem.
   300   underlying the first theorem.
   298 
   301 
   299   Like Homeier, our work relies on map-functions defined for every type constructor,
   302   Like Homeier, our work relies on map-functions defined for every type
   300   like @{text map} for lists. Homeier describes in \cite{Homeier05} map-functions
   303   constructor taking some arguments, for example @{text map} for lists. Homeier
   301   for products, sums,
   304   describes in \cite{Homeier05} map-functions for products, sums, options and
   302   options and also the following map for function types
   305   also the following map for function types
   303 
   306 
   304   @{thm [display, indent=10] fun_map_def[no_vars, THEN eq_reflection]}
   307   @{thm [display, indent=10] fun_map_def[no_vars, THEN eq_reflection]}
   305 
   308 
   306   \noindent
   309   \noindent
   307   Using this map-function, we can give the following, equivalent, but more 
   310   Using this map-function, we can give the following, equivalent, but more 
   308   uniform, definition for @{text add} shown in \eqref{adddef}:
   311   uniform, definition for @{text add} shown in \eqref{adddef}:
   309 
   312 
   310   @{text [display, indent=10] "add \<equiv> (Rep_int \<singlearr> Rep_int \<singlearr> Abs_int) add_pair"}
   313   @{text [display, indent=10] "add \<equiv> (Rep_int \<singlearr> Rep_int \<singlearr> Abs_int) add_pair"}
   311 
   314 
   312   \noindent
   315   \noindent
   313   Using extensionality and unfolding the definition, we can get back to \eqref{adddef}. 
   316   Using extensionality and unfolding the definition of @{text "\<singlearr>"}, 
   314   In what follows we shall use the terminology @{text "map_\<kappa>"} for a map-function 
   317   we can get back to \eqref{adddef}. 
   315   defined for the type-constructor @{text \<kappa>}. In our implementation we have 
   318   In what follows we shall use the convention to write @{text "map_\<kappa>"} for a map-function 
   316   a database of map-functions that can be dynamically extended.
   319   of the type-constructor @{text \<kappa>}. In our implementation we maintain 
       
   320   a database of these map-functions that can be dynamically extended.
   317 
   321 
   318   It will also be necessary to have operators, referred to as @{text "rel_\<kappa>"},
   322   It will also be necessary to have operators, referred to as @{text "rel_\<kappa>"},
   319   which define equivalence relations in terms of constituent equivalence
   323   which define equivalence relations in terms of constituent equivalence
   320   relations. For example given two equivalence relations @{text "R\<^isub>1"}
   324   relations. For example given two equivalence relations @{text "R\<^isub>1"}
   321   and @{text "R\<^isub>2"}, we can define an equivalence relations over 
   325   and @{text "R\<^isub>2"}, we can define an equivalence relations over 
   325 
   329 
   326   \noindent
   330   \noindent
   327   Homeier gives also the following operator for defining equivalence 
   331   Homeier gives also the following operator for defining equivalence 
   328   relations over function types
   332   relations over function types
   329   %
   333   %
   330   @{thm [display, indent=10] fun_rel_def[of "R\<^isub>1" "R\<^isub>2", no_vars, THEN eq_reflection]}
   334   \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%%
       
   335   @{thm fun_rel_def[of "R\<^isub>1" "R\<^isub>2", no_vars, THEN eq_reflection]}
       
   336   \hfill\numbered{relfun}
       
   337   \end{isabelle}
       
   338   
       
   339   \noindent
       
   340   In the context of quotients, the following two notions from are \cite{Homeier05} 
       
   341   needed later on.
       
   342 
       
   343   \begin{definition}[Respects]\label{def:respects}
       
   344   An element @{text "x"} respects a relation @{text "R"} provided @{text "R x x"}.
       
   345   \end{definition}
       
   346 
       
   347   \begin{definition}[Bounded Quantification and Bounded Abstractions]\label{def:babs}
       
   348   @{text "\<forall>x \<in> S. P x"} holds if for all @{text x}, @{text "x \<in> S"} implies @{text "P x"};
       
   349   and @{text "(\<lambda>x \<in> S. f x) = f x"} provided @{text "x \<in> S"}.
       
   350   \end{definition}
   331 
   351 
   332   The central definition in Homeier's work \cite{Homeier05} relates equivalence 
   352   The central definition in Homeier's work \cite{Homeier05} relates equivalence 
   333   relations, abstraction and representation functions:
   353   relations, abstraction and representation functions:
   334 
   354 
   335   \begin{definition}[Quotient Types]
   355   \begin{definition}[Quotient Types]
   353   \begin{proposition}\label{funquot}
   373   \begin{proposition}\label{funquot}
   354   @{thm[mode=IfThen] fun_quotient[where ?R1.0="R\<^isub>1" and ?R2.0="R\<^isub>2" 
   374   @{thm[mode=IfThen] fun_quotient[where ?R1.0="R\<^isub>1" and ?R2.0="R\<^isub>2" 
   355       and ?abs1.0="Abs\<^isub>1" and ?abs2.0="Abs\<^isub>2" and ?rep1.0="Rep\<^isub>1" and ?rep2.0="Rep\<^isub>2"]}
   375       and ?abs1.0="Abs\<^isub>1" and ?abs2.0="Abs\<^isub>2" and ?rep1.0="Rep\<^isub>1" and ?rep2.0="Rep\<^isub>2"]}
   356   \end{proposition}
   376   \end{proposition}
   357 
   377 
   358   \begin{definition}[Respects]\label{def:respects}
   378   \noindent
   359   An element @{text "x"} respects a relation @{text "R"} if and only if @{text "R x x"}.
   379   As a result, Homeier is able to build an automatic prover that can nearly
   360   \end{definition}
       
   361 
       
   362   \noindent
       
   363   As a result, Homeier was able to build an automatic prover that can nearly
       
   364   always discharge a proof obligation involving @{text "Quotient"}. Our quotient
   380   always discharge a proof obligation involving @{text "Quotient"}. Our quotient
   365   package makes heavy 
   381   package makes heavy 
   366   use of this part of Homeier's work including an extension 
   382   use of this part of Homeier's work including an extension 
   367   to deal with compositions of equivalence relations defined as follows
   383   to deal with compositions of equivalence relations defined as follows:
   368 
   384 
   369   \begin{definition}[Composition of Relations]
   385   \begin{definition}[Composition of Relations]
   370   @{abbrev "rel_conj R\<^isub>1 R\<^isub>2"} where @{text "\<circ>\<circ>"} is the predicate
   386   @{abbrev "rel_conj R\<^isub>1 R\<^isub>2"} where @{text "\<circ>\<circ>"} is the predicate
   371   composition defined by the rule
   387   composition defined by @{thm (concl) pred_compI[of "R\<^isub>1" "x" "y" "R\<^isub>2" "z"]}
   372   %
   388   holds if and only if @{thm (prem 1) pred_compI[of "R\<^isub>1" "x" "y" "R\<^isub>2" "z"]} and
   373   @{thm [mode=Rule, display, indent=10] pred_compI[of "R\<^isub>1" "x" "y" "R\<^isub>2" "z"]}
   389   @{thm (prem 2) pred_compI[of "R\<^isub>1" "x" "y" "R\<^isub>2" "z"]}.
   374   \end{definition}
   390   \end{definition}
   375 
   391 
   376   \noindent
   392   \noindent
   377   Unfortunately a quotient type theorem, like Proposition \ref{funquot}, for
   393   Unfortunately, there are two predicaments with compositions of relations.
   378   the composition of any two quotients in not true (it is not even typable in
   394   First, a general quotient theorem, like the one given in Proposition \ref{funquot},
   379   the HOL type system). However, we can prove useful instances for compatible
   395   cannot be stated inside HOL, because of the restriction on types.
   380   containers. We will show such example in Section \ref{sec:resp}.
   396   Second, even if we were able to state such a quotient theorem, it
   381 
   397   would not be true in general. However, we can prove specific and useful 
       
   398   instances of the quotient theorem. We will 
       
   399   show an example in Section \ref{sec:resp}.
   382 
   400 
   383 *}
   401 *}
   384 
   402 
   385 section {* Quotient Types and Quotient Definitions\label{sec:type} *}
   403 section {* Quotient Types and Quotient Definitions\label{sec:type} *}
   386 
   404 
   410   \noindent
   428   \noindent
   411   which introduce the type of integers and of finite sets using the
   429   which introduce the type of integers and of finite sets using the
   412   equivalence relations @{text "\<approx>\<^bsub>nat \<times> nat\<^esub>"} and @{text
   430   equivalence relations @{text "\<approx>\<^bsub>nat \<times> nat\<^esub>"} and @{text
   413   "\<approx>\<^bsub>list\<^esub>"} defined in \eqref{natpairequiv} and
   431   "\<approx>\<^bsub>list\<^esub>"} defined in \eqref{natpairequiv} and
   414   \eqref{listequiv}, respectively (the proofs about being equivalence
   432   \eqref{listequiv}, respectively (the proofs about being equivalence
   415   relations is omitted).  Given this data, we declare for \eqref{typedecl} internally 
   433   relations is omitted).  Given this data, we define for declarations shown in
   416   the quotient types as
   434   \eqref{typedecl} the quotient types internally as
   417   
   435   
   418   \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%%
   436   \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%%
   419   \isacommand{typedef}~~@{text "\<alpha>s \<kappa>\<^isub>q = {c. \<exists>x. c = R x}"}
   437   \isacommand{typedef}~~@{text "\<alpha>s \<kappa>\<^isub>q = {c. \<exists>x. c = R x}"}
   420   \end{isabelle}
   438   \end{isabelle}
   421 
   439 
   422   \noindent
   440   \noindent
   423   where the right-hand side is the (non-empty) set of equivalence classes of
   441   where the right-hand side is the (non-empty) set of equivalence classes of
   424   @{text "R"}. The restriction in this declaration is that the type variables
   442   @{text "R"}. The constraint in this declaration is that the type variables
   425   in the raw type @{text "\<sigma>"} must be included in the type variables @{text
   443   in the raw type @{text "\<sigma>"} must be included in the type variables @{text
   426   "\<alpha>s"} declared for @{text "\<kappa>\<^isub>q"}. HOL will provide us with the following
   444   "\<alpha>s"} declared for @{text "\<kappa>\<^isub>q"}. HOL will then provide us with the following
   427   abstraction and representation functions 
   445   abstraction and representation functions 
   428 
   446 
   429   \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%%
   447   \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%%
   430   @{text "abs_\<kappa>\<^isub>q :: \<sigma> set \<Rightarrow> \<alpha>s \<kappa>\<^isub>q"}\hspace{10mm}@{text "rep_\<kappa>\<^isub>q :: \<alpha>s \<kappa>\<^isub>q \<Rightarrow> \<sigma> set"}
   448   @{text "abs_\<kappa>\<^isub>q :: \<sigma> set \<Rightarrow> \<alpha>s \<kappa>\<^isub>q"}\hspace{10mm}@{text "rep_\<kappa>\<^isub>q :: \<alpha>s \<kappa>\<^isub>q \<Rightarrow> \<sigma> set"}
   431   \end{isabelle}
   449   \end{isabelle}
   443   on the expense of having to use Hilbert's choice operator @{text "\<epsilon>"} in the
   461   on the expense of having to use Hilbert's choice operator @{text "\<epsilon>"} in the
   444   definition of @{text "Rep_\<kappa>\<^isub>q"}. These derived notions relate the
   462   definition of @{text "Rep_\<kappa>\<^isub>q"}. These derived notions relate the
   445   quotient type and the raw type directly, as can be seen from their type,
   463   quotient type and the raw type directly, as can be seen from their type,
   446   namely @{text "\<sigma> \<Rightarrow> \<alpha>s \<kappa>\<^isub>q"} and @{text "\<alpha>s \<kappa>\<^isub>q \<Rightarrow> \<sigma>"},
   464   namely @{text "\<sigma> \<Rightarrow> \<alpha>s \<kappa>\<^isub>q"} and @{text "\<alpha>s \<kappa>\<^isub>q \<Rightarrow> \<sigma>"},
   447   respectively.  Given that @{text "R"} is an equivalence relation, the
   465   respectively.  Given that @{text "R"} is an equivalence relation, the
   448   following property
   466   following property holds  for every quotient type 
       
   467   (for the proof see \cite{Homeier05}).
   449 
   468 
   450   \begin{proposition}
   469   \begin{proposition}
   451   @{text "Quotient R Abs_\<kappa>\<^isub>q Rep_\<kappa>\<^isub>q"}
   470   @{text "Quotient R Abs_\<kappa>\<^isub>q Rep_\<kappa>\<^isub>q"}
   452   \end{proposition}
   471   \end{proposition}
   453 
       
   454   \noindent
       
   455   holds  for every quotient type defined
       
   456   as above (for the proof see \cite{Homeier05}).
       
   457 
   472 
   458   The next step in a quotient construction is to introduce definitions of new constants
   473   The next step in a quotient construction is to introduce definitions of new constants
   459   involving the quotient type. These definitions need to be given in terms of concepts
   474   involving the quotient type. These definitions need to be given in terms of concepts
   460   of the raw type (remember this is the only way how to extend HOL
   475   of the raw type (remember this is the only way how to extend HOL
   461   with new definitions). For the user the visible part of such definitions is the declaration
   476   with new definitions). For the user the visible part of such definitions is the declaration
   478   \end{tabular}
   493   \end{tabular}
   479   \end{isabelle}
   494   \end{isabelle}
   480   
   495   
   481   \noindent
   496   \noindent
   482   The first one declares zero for integers and the second the operator for
   497   The first one declares zero for integers and the second the operator for
   483   building unions of finite sets. 
   498   building unions of finite sets (@{text "flat"} having the type 
       
   499   @{text "(\<alpha> list) list \<Rightarrow> \<alpha> list"}). 
   484 
   500 
   485   The problem for us is that from such declarations we need to derive proper
   501   The problem for us is that from such declarations we need to derive proper
   486   definitions using the @{text "Abs"} and @{text "Rep"} functions for the
   502   definitions using the @{text "Abs"} and @{text "Rep"} functions for the
   487   quotient types involved. The data we rely on is the given quotient type
   503   quotient types involved. The data we rely on is the given quotient type
   488   @{text "\<tau>"} and the raw type @{text "\<sigma>"}.  They allow us to define \emph{aggregate
   504   @{text "\<tau>"} and the raw type @{text "\<sigma>"}.  They allow us to define \emph{aggregate
   489   abstraction} and \emph{representation functions} using the functions @{text "ABS (\<sigma>,
   505   abstraction} and \emph{representation functions} using the functions @{text "ABS (\<sigma>,
   490   \<tau>)"} and @{text "REP (\<sigma>, \<tau>)"} whose clauses we give below. The idea behind
   506   \<tau>)"} and @{text "REP (\<sigma>, \<tau>)"} whose clauses we give below. The idea behind
   491   these two functions is to recursively descend into the raw types @{text \<sigma>} and 
   507   these two functions is to simultaneously descend into the raw types @{text \<sigma>} and 
   492   quotient types @{text \<tau>}, and generate the appropriate
   508   quotient types @{text \<tau>}, and generate the appropriate
   493   @{text "Abs"} and @{text "Rep"} in places where the types differ. Therefore
   509   @{text "Abs"} and @{text "Rep"} in places where the types differ. Therefore
   494   we generate just the identity whenever the types are equal. On the ``way'' down,
   510   we generate just the identity whenever the types are equal. On the ``way'' down,
   495   however we might have to use map-functions to let @{text Abs} and @{text Rep} act
   511   however we might have to use map-functions to let @{text Abs} and @{text Rep} act
   496   over the appropriate types. The clauses of @{text ABS} and @{text REP} 
   512   over the appropriate types. In what follows we use the short-hand notation 
   497   are as follows (where we use the short-hand notation @{text "ABS (\<sigma>s, \<tau>s)"} to mean 
   513   @{text "ABS (\<sigma>s, \<tau>s)"} to mean @{text "ABS (\<sigma>\<^isub>1, \<tau>\<^isub>1)\<dots>ABS (\<sigma>\<^isub>i, \<tau>\<^isub>i)"}; similarly 
   498   @{text "ABS (\<sigma>\<^isub>1, \<tau>\<^isub>1)\<dots>ABS (\<sigma>\<^isub>i, \<tau>\<^isub>i)"}; similarly for @{text REP}):
   514   for @{text REP}.
   499 
   515   %
   500   \begin{center}
   516   \begin{center}
   501   \hfill
   517   \hfill
   502   \begin{tabular}{rcl}
   518   \begin{tabular}{rcl}
   503   \multicolumn{3}{@ {\hspace{-4mm}}l}{equal types:}\\ 
   519   \multicolumn{3}{@ {\hspace{-4mm}}l}{equal types:}\\ 
   504   @{text "ABS (\<sigma>, \<sigma>)"} & $\dn$ & @{text "id :: \<sigma> \<Rightarrow> \<sigma>"}\\
   520   @{text "ABS (\<sigma>, \<sigma>)"} & $\dn$ & @{text "id :: \<sigma> \<Rightarrow> \<sigma>"}\\
   514   @{text "REP (\<sigma>s \<kappa>, \<tau>s \<kappa>\<^isub>q)"} & $\dn$ & @{text "(MAP(\<rho>s \<kappa>) (REP (\<sigma>s', \<tau>s))) \<circ> Rep_\<kappa>\<^isub>q"}
   530   @{text "REP (\<sigma>s \<kappa>, \<tau>s \<kappa>\<^isub>q)"} & $\dn$ & @{text "(MAP(\<rho>s \<kappa>) (REP (\<sigma>s', \<tau>s))) \<circ> Rep_\<kappa>\<^isub>q"}
   515   \end{tabular}\hfill\numbered{ABSREP}
   531   \end{tabular}\hfill\numbered{ABSREP}
   516   \end{center}
   532   \end{center}
   517   %
   533   %
   518   \noindent
   534   \noindent
   519   where in the last two clauses we have that the type @{text "\<alpha>s
   535   In the last two clauses we have that the type @{text "\<alpha>s
   520   \<kappa>\<^isub>q"} is the quotient of the raw type @{text "\<rho>s \<kappa>"} (for example
   536   \<kappa>\<^isub>q"} is the quotient of the raw type @{text "\<rho>s \<kappa>"} (for example
   521   @{text "int"} and @{text "nat \<times> nat"}, or @{text "\<alpha> fset"} and @{text "\<alpha>
   537   @{text "int"} and @{text "nat \<times> nat"}, or @{text "\<alpha> fset"} and @{text "\<alpha>
   522   list"}). The quotient construction ensures that the type variables in @{text
   538   list"}). The quotient construction ensures that the type variables in @{text
   523   "\<rho>s"} must be among the @{text "\<alpha>s"}. The @{text "\<sigma>s'"} are given by the
   539   "\<rho>s"} must be among the @{text "\<alpha>s"}. The @{text "\<sigma>s'"} are given by the
   524   matchers for the @{text "\<alpha>s"} when matching @{text "\<rho>s \<kappa>"} against
   540   matchers for the @{text "\<alpha>s"} when matching @{text "\<rho>s \<kappa>"} against
   536   \end{center}
   552   \end{center}
   537   %
   553   %
   538   \noindent
   554   \noindent
   539   In this definition we rely on the fact that we can interpret type-variables @{text \<alpha>} as 
   555   In this definition we rely on the fact that we can interpret type-variables @{text \<alpha>} as 
   540   term variables @{text a}. In the last clause we build an abstraction over all
   556   term variables @{text a}. In the last clause we build an abstraction over all
   541   term-variables inside map-function generated by the auxiliary function 
   557   term-variables of the map-function generated by the auxiliary function 
   542   @{text "MAP'"}.
   558   @{text "MAP'"}.
   543   The need of aggregate map-functions can be seen in cases where we build quotients, 
   559   The need for aggregate map-functions can be seen in cases where we build quotients, 
   544   say @{text "(\<alpha>, \<beta>) \<kappa>\<^isub>q"}, out of compound raw types, say @{text "(\<alpha> list) \<times> \<beta>"}. 
   560   say @{text "(\<alpha>, \<beta>) \<kappa>\<^isub>q"}, out of compound raw types, say @{text "(\<alpha> list) \<times> \<beta>"}. 
   545   In this case @{text MAP} generates  the 
   561   In this case @{text MAP} generates  the 
   546   aggregate map-function:
   562   aggregate map-function:
   547 
   563 
   548   @{text [display, indent=10] "\<lambda>a b. map_prod (map a) b"}
   564   @{text [display, indent=10] "\<lambda>a b. map_prod (map a) b"}
   549   
   565   
   550   \noindent
   566   \noindent
   551   which we need in order to define the aggregate abstraction and representation 
   567   which is essential in order to define the corresponding aggregate 
   552   functions.
   568   abstraction and representation functions.
   553   
   569   
   554   To see how these definitions pan out in practise, let us return to our
   570   To see how these definitions pan out in practise, let us return to our
   555   example about @{term "concat"} and @{term "fconcat"}, where we have the raw type
   571   example about @{term "concat"} and @{term "fconcat"}, where we have the raw type
   556   @{text "(\<alpha> list) list \<Rightarrow> \<alpha> list"} and the quotient type @{text "(\<alpha> fset) fset \<Rightarrow> \<alpha>
   572   @{text "(\<alpha> list) list \<Rightarrow> \<alpha> list"} and the quotient type @{text "(\<alpha> fset) fset \<Rightarrow> \<alpha>
   557   fset"}. Feeding them into @{text ABS} gives us (after some @{text "\<beta>"}-simplifications)
   573   fset"}. Feeding these types into @{text ABS} gives us (after some @{text "\<beta>"}-simplifications)
   558   the abstraction function
   574   the abstraction function
   559 
   575 
   560   @{text [display, indent=10] "(map (map id \<circ> Rep_fset) \<circ> Rep_fset) \<singlearr> Abs_fset \<circ> map id"}
   576   @{text [display, indent=10] "(map (map id \<circ> Rep_fset) \<circ> Rep_fset) \<singlearr> Abs_fset \<circ> map id"}
   561 
   577 
   562   \noindent
   578   \noindent
   563   In our implementation we further
   579   In our implementation we further
   564   simplify this function by rewriting with the usual laws about @{text
   580   simplify this function by rewriting with the usual laws about @{text
   565   "map"}s and @{text "id"}, namely @{term "map id = id"} and @{text "f \<circ> id =
   581   "map"}s and @{text "id"}, namely @{term "map id = id"} and @{text "f \<circ> id =
   566   id \<circ> f = f"}. This gives us the abstraction function
   582   id \<circ> f = f"}. This gives us the simpler abstraction function
   567 
   583 
   568   @{text [display, indent=10] "(map Rep_fset \<circ> Rep_fset) \<singlearr> Abs_fset"}
   584   @{text [display, indent=10] "(map Rep_fset \<circ> Rep_fset) \<singlearr> Abs_fset"}
   569 
   585 
   570   \noindent
   586   \noindent
   571   which we can use for defining @{term "fconcat"} as follows
   587   which we can use for defining @{term "fconcat"} as follows
   594   then @{text "Abs"} is of type @{text "\<sigma> \<Rightarrow> \<tau>"} and @{text "Rep"} of type
   610   then @{text "Abs"} is of type @{text "\<sigma> \<Rightarrow> \<tau>"} and @{text "Rep"} of type
   595   @{text "\<tau> \<Rightarrow> \<sigma>"}.
   611   @{text "\<tau> \<Rightarrow> \<sigma>"}.
   596   \end{lemma}
   612   \end{lemma}
   597 
   613 
   598   \begin{proof}
   614   \begin{proof}
   599   By mutual induction and analysing the definitions of @{text "ABS"}, @{text "REP"}
   615   By mutual induction and analysing the definitions of @{text "ABS"} and @{text "REP"}. 
   600   and @{text "MAP"}. The cases of equal types and function types are
   616   The cases of equal types and function types are
   601   straightforward (the latter follows from @{text "\<singlearr>"} having the
   617   straightforward (the latter follows from @{text "\<singlearr>"} having the
   602   type @{text "(\<alpha> \<Rightarrow> \<beta>) \<Rightarrow> (\<gamma> \<Rightarrow> \<delta>) \<Rightarrow> (\<beta> \<Rightarrow> \<gamma>) \<Rightarrow> (\<alpha> \<Rightarrow> \<delta>)"}). In case of equal type
   618   type @{text "(\<alpha> \<Rightarrow> \<beta>) \<Rightarrow> (\<gamma> \<Rightarrow> \<delta>) \<Rightarrow> (\<beta> \<Rightarrow> \<gamma>) \<Rightarrow> (\<alpha> \<Rightarrow> \<delta>)"}). In case of equal type
   603   constructors we can observe that a map-function after applying the functions
   619   constructors we can observe that a map-function after applying the functions
   604   @{text "ABS (\<sigma>s, \<tau>s)"} produces a term of type @{text "\<sigma>s \<kappa> \<Rightarrow> \<tau>s \<kappa>"}.  The
   620   @{text "ABS (\<sigma>s, \<tau>s)"} produces a term of type @{text "\<sigma>s \<kappa> \<Rightarrow> \<tau>s \<kappa>"}.  The
   605   interesting case is the one with unequal type constructors. Since we know
   621   interesting case is the one with unequal type constructors. Since we know
   614   @{text "\<rho>s[\<tau>s] \<kappa> \<Rightarrow> \<tau>s \<kappa>\<^isub>q"} according to the type of @{text "\<circ>"}.\qed
   630   @{text "\<rho>s[\<tau>s] \<kappa> \<Rightarrow> \<tau>s \<kappa>\<^isub>q"} according to the type of @{text "\<circ>"}.\qed
   615   \end{proof}
   631   \end{proof}
   616   
   632   
   617   \noindent
   633   \noindent
   618   The reader should note that this lemma fails for the abstraction and representation 
   634   The reader should note that this lemma fails for the abstraction and representation 
   619   functions used, for example, in Homeier's quotient package.
   635   functions used in Homeier's quotient package.
   620 *}
   636 *}
   621 
   637 
   622 section {* Respectfulness and Preservation \label{sec:resp} *}
   638 section {* Respectfulness and Preservation \label{sec:resp} *}
   623 
   639 
   624 text {*
   640 text {*
   625   The main point of the quotient package is to automatically ``lift'' theorems
   641   The main point of the quotient package is to automatically ``lift'' theorems
   626   involving constants over the raw type to theorems involving constants over
   642   involving constants over the raw type to theorems involving constants over
   627   the quotient type. Before we can describe this lift process, we need to impose 
   643   the quotient type. Before we can describe this lifting process, we need to impose 
   628   some restrictions. The reason is that even if definitions for all raw constants 
   644   some restrictions in the form of proof obligations that arise during the
   629   can be given, \emph{not} all theorems can be actually be lifted. Most notably is
   645   lifting. The reason is that even if definitions for all raw constants 
   630   the bound variable function, that is the constant @{text bn}, defined for 
   646   can be given, \emph{not} all theorems can be lifted to the quotient type. Most 
   631   raw lambda-terms as follows
   647   notably is the bound variable function, that is the constant @{text bn}, defined 
       
   648   for raw lambda-terms as follows
   632 
   649 
   633   \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%%
   650   \begin{isabelle}\ \ \ \ \ \ \ \ \ \ %%%
   634   @{text "bn (x) \<equiv> \<emptyset>"}\hspace{4mm}
   651   @{text "bn (x) \<equiv> \<emptyset>"}\hspace{4mm}
   635   @{text "bn (t\<^isub>1 t\<^isub>2) \<equiv> bn (t\<^isub>1) \<union> bn (t\<^isub>2)"}\hspace{4mm}
   652   @{text "bn (t\<^isub>1 t\<^isub>2) \<equiv> bn (t\<^isub>1) \<union> bn (t\<^isub>2)"}\hspace{4mm}
   636   @{text "bn (\<lambda>x. t) \<equiv> {x} \<union> bn (t)"}
   653   @{text "bn (\<lambda>x. t) \<equiv> {x} \<union> bn (t)"}
   637   \end{isabelle}
   654   \end{isabelle}
   638 
   655 
   639   \noindent
   656   \noindent
   640   This constant just does not respect @{text "\<alpha>"}-equivalence and as
   657   We can generate a definition for this constant using @{text ABS} and @{text REP}.
       
   658   But this constant does \emph{not} respect @{text "\<alpha>"}-equivalence and 
   641   consequently no theorem involving this constant can be lifted to @{text
   659   consequently no theorem involving this constant can be lifted to @{text
   642   "\<alpha>"}-equated lambda terms. Homeier formulates the restrictions in terms of
   660   "\<alpha>"}-equated lambda terms. Homeier formulates the restrictions in terms of
   643   the properties of \emph{respectfullness} and \emph{preservation}. We have
   661   the properties of \emph{respectfullness} and \emph{preservation}. We have
   644   to slightly extend Homeier's definitions in order to deal with quotient
   662   to slightly extend Homeier's definitions in order to deal with quotient
   645   compositions. 
   663   compositions. 
   646 
   664 
   647   To formally define what respectfulness is, we have to first define 
   665   To formally define what respectfulness is, we have to first define 
   648   the notion of \emph{aggregate equivalence relations}.
   666   the notion of \emph{aggregate equivalence relations} using @{text REL}:
   649 
   667 
   650   TBD
   668   \begin{center}
   651 
   669   \hfill
   652   \begin{itemize}
   670   \begin{tabular}{rcl}
   653   \item @{text "REL(\<alpha>\<^isub>1, \<alpha>\<^isub>2)"} = @{text "op ="}
   671   \multicolumn{3}{@ {\hspace{-4mm}}l}{equal types:}\\ 
   654   \item @{text "REL(\<sigma>, \<sigma>)"}  =  @{text "op ="}
   672   @{text "REL (\<sigma>, \<sigma>)"} & $\dn$ & @{text "= :: \<sigma> \<Rightarrow> \<sigma> \<Rightarrow> bool"}\smallskip\\
   655   \item @{text "REL((\<sigma>\<^isub>1,\<dots>,\<sigma>\<^isub>n))\<kappa>, (\<tau>\<^isub>1,\<dots>,\<tau>\<^isub>n))\<kappa>)"}  =  @{text "(rel \<kappa>) (REL(\<sigma>\<^isub>1,\<tau>\<^isub>1)) \<dots> (REL(\<sigma>\<^isub>n,\<tau>\<^isub>n))"}
   673    \multicolumn{3}{@ {\hspace{-4mm}}l}{equal type constructors:}\\ 
   656   \item @{text "REL((\<sigma>\<^isub>1,\<dots>,\<sigma>\<^isub>n))\<kappa>\<^isub>1, (\<tau>\<^isub>1,\<dots>,\<tau>\<^isub>m))\<kappa>\<^isub>2)"}  =  @{text "(rel \<kappa>\<^isub>1) (REL(\<rho>\<^isub>1,\<nu>\<^isub>1) \<dots> (REL(\<rho>\<^isub>p,\<nu>\<^isub>p) OOO Eqv_\<kappa>\<^isub>2"} provided @{text "\<eta> \<kappa>\<^isub>2 = (\<alpha>\<^isub>1\<dots>\<alpha>\<^isub>p)\<kappa>\<^isub>1 \<and> \<exists>s. s(\<sigma>s\<kappa>\<^isub>1)=\<rho>s\<kappa>\<^isub>1 \<and> s(\<tau>s\<kappa>\<^isub>2)=\<nu>s\<kappa>\<^isub>2"}
   674   @{text "REL (\<sigma>s \<kappa>, \<tau>s \<kappa>)"} & $\dn$ & @{text "rel_\<kappa> (REL (\<sigma>s, \<tau>s))"}\smallskip\\
   657   \end{itemize}
   675   \multicolumn{3}{@ {\hspace{-4mm}}l}{unequal type constructors:}\smallskip\\
       
   676   @{text "REL (\<sigma>s \<kappa>, \<tau>s \<kappa>\<^isub>q)"} & $\dn$ & @{text "rel_\<kappa>\<^isub>q (REL (\<sigma>s', \<tau>s))"}\\
       
   677   \end{tabular}\hfill\numbered{REL}
       
   678   \end{center}
       
   679 
       
   680   \noindent
       
   681   The @{text "\<sigma>s'"} in the last clause are calculated as in \eqref{ABSREP}:
       
   682   we know that type @{text "\<alpha>s \<kappa>\<^isub>q"} is the quotient of the raw type 
       
   683   @{text "\<rho>s \<kappa>"}. The @{text "\<sigma>s'"} are determined by matching 
       
   684   @{text "\<rho>s \<kappa>"} and @{text "\<sigma>s \<kappa>"}.
       
   685 
       
   686   Lets return to the lifting procedure of theorems. Assume we have a theorem
       
   687   that contains the raw constant @{text "c\<^isub>r :: \<sigma>"} and which we want to
       
   688   lift to a theorem where @{text "c\<^isub>r"} is replaced by the corresponding
       
   689   constant @{text "c\<^isub>q :: \<tau>"} defined over a quotient type. In this situation 
       
   690   we throw the following proof obligation
       
   691 
       
   692   @{text [display, indent=10] "REL (\<sigma>, \<tau>) c\<^isub>r c\<^isub>r"}
       
   693 
       
   694   \noindent
       
   695   if @{text \<sigma>} and @{text \<tau>} have no type variables. In case they have, then
       
   696   the proof obligation is of the form
       
   697 
       
   698   @{text [display, indent=10] "Quotient R\<^bsub>\<alpha>s\<^esub> Abs\<^bsub>\<alpha>s\<^esub> Rep\<^bsub>\<alpha>s\<^esub> implies REL (\<sigma>, \<tau>) c\<^isub>r c\<^isub>r"}
       
   699 
       
   700   \noindent
       
   701   where @{text "\<alpha>s"} are the type variables in @{text \<sigma>} and @{text \<tau>}.
       
   702   Homeier calls these proof obligations \emph{respectfullness
       
   703   theorems}. Before lifting a theorem, we require the user to discharge
       
   704   them. And the point with @{text bn} is that the respectfullness theorem
       
   705   looks as follows
       
   706 
       
   707   @{text [display, indent=10] "(\<approx>\<^isub>\<alpha> \<doublearr> =) bn bn"}
       
   708 
       
   709   \noindent
       
   710   and the user cannot discharge it---because it is not true. To see this,
       
   711   we can just unfold the definition of @{text "\<doublearr>"} \eqref{relfun} 
       
   712   using extensionality to obtain
       
   713 
       
   714   @{text [display, indent=10] "\<forall>t\<^isub>1 t\<^isub>2. if t\<^isub>1 \<approx>\<^isub>\<alpha> t\<^isub>2 implies bn(t\<^isub>1) = bn(t\<^isub>2)"}
       
   715  
       
   716   \noindent
       
   717   In contrast, if we lift a theorem about @{text "append"} to a theorem describing 
       
   718   the union of finite sets, then we need to discharge the proof obligation
       
   719 
       
   720   @{text [display, indent=10] "(rel_list R \<doublearr> rel_list R \<doublearr> rel_listR) append append"}
       
   721 
       
   722   \noindent
       
   723   under the assumption @{text "Quotient R Abs Rep"}.
       
   724 
   658 
   725 
   659   class returned by this constant depends only on the equivalence
   726   class returned by this constant depends only on the equivalence
   660   classes of the arguments applied to the constant. To automatically
   727   classes of the arguments applied to the constant. To automatically
   661   lift a theorem that talks about a raw constant, to a theorem about
   728   lift a theorem that talks about a raw constant, to a theorem about
   662   the quotient type a respectfulness theorem is required.
   729   the quotient type a respectfulness theorem is required.
   959 
  1026 
   960   @{thm [display, indent=10] (concl) map_prs(1)[of R\<^isub>1 Abs\<^isub>1 Rep\<^isub>1 R\<^isub>2 Abs\<^isub>2 Rep\<^isub>2]}
  1027   @{thm [display, indent=10] (concl) map_prs(1)[of R\<^isub>1 Abs\<^isub>1 Rep\<^isub>1 R\<^isub>2 Abs\<^isub>2 Rep\<^isub>2]}
   961 
  1028 
   962   *}
  1029   *}
   963 
  1030 
   964 section {* Examples *}
  1031 section {* Examples \label{sec:examples} *}
   965 
  1032 
   966 (* Mention why equivalence *)
  1033 (* Mention why equivalence *)
   967 
  1034 
   968 text {*
  1035 text {*
   969 
  1036