src/HOL/Tools/Lifting/lifting_setup.ML
author wenzelm
Mon, 26 Nov 2012 17:13:44 +0100
changeset 50231 81a067b188b8
parent 50227 01d545993e8c
parent 50214 67fb9a168d10
child 50288 986598b0efd1
permissions -rw-r--r--
merged
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     1
(*  Title:      HOL/Tools/Lifting/lifting_setup.ML
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     2
    Author:     Ondrej Kuncar
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     3
47352
e0bff2ae939f fix typos
huffman
parents: 47334
diff changeset
     4
Setting up the lifting infrastructure.
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     5
*)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     6
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     7
signature LIFTING_SETUP =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     8
sig
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     9
  exception SETUP_LIFTING_INFR of string
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    10
47566
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
    11
  val setup_by_quotient: bool -> thm -> thm option -> local_theory -> local_theory
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    12
47566
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
    13
  val setup_by_typedef_thm: bool -> thm -> local_theory -> local_theory
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    14
end;
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    15
47334
4708384e759d fix typo in ML structure name
huffman
parents: 47308
diff changeset
    16
structure Lifting_Setup: LIFTING_SETUP =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    17
struct
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    18
47698
18202d3d5832 move MRSL to a separate file
kuncar
parents: 47623
diff changeset
    19
open Lifting_Util
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    20
47698
18202d3d5832 move MRSL to a separate file
kuncar
parents: 47623
diff changeset
    21
infix 0 MRSL
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    22
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    23
exception SETUP_LIFTING_INFR of string
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    24
50227
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    25
fun define_crel rep_fun lthy =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    26
  let
47361
87c0eaf04bad support non-open typedefs; define cr_rel in terms of a rep function for typedefs
kuncar
parents: 47352
diff changeset
    27
    val (qty, rty) = (dest_funT o fastype_of) rep_fun
87c0eaf04bad support non-open typedefs; define cr_rel in terms of a rep function for typedefs
kuncar
parents: 47352
diff changeset
    28
    val rep_fun_graph = (HOLogic.eq_const rty) $ Bound 1 $ (rep_fun $ Bound 0)
87c0eaf04bad support non-open typedefs; define cr_rel in terms of a rep function for typedefs
kuncar
parents: 47352
diff changeset
    29
    val def_term = Abs ("x", rty, Abs ("y", qty, rep_fun_graph));
50175
b27cf0646080 generate correct correspondence relation name
kuncar
parents: 47982
diff changeset
    30
    val qty_name = (Binding.name o Long_Name.base_name o fst o dest_Type) qty
50227
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    31
    val crel_name = Binding.prefix_name "cr_" qty_name
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    32
    val (fixed_def_term, lthy') = yield_singleton (Variable.importT_terms) def_term lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    33
    val ((_, (_ , def_thm)), lthy'') =
50227
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    34
      Local_Theory.define ((crel_name, NoSyn), ((Thm.def_binding crel_name, []), fixed_def_term)) lthy'
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    35
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    36
    (def_thm, lthy'')
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    37
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    38
50227
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    39
fun print_define_pcrel_warning msg = 
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    40
  let
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    41
    val warning_msg = cat_lines 
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    42
      ["Generation of a parametrized correspondence relation failed.",
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    43
      (Pretty.string_of (Pretty.block
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    44
         [Pretty.str "Reason:", Pretty.brk 2, msg]))]
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    45
  in
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    46
    warning warning_msg
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    47
  end
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    48
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    49
fun define_pcrel crel lthy =
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    50
  let
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    51
    val pcrel = Morphism.term (Local_Theory.target_morphism lthy) crel
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    52
    val [crel_poly] = Variable.polymorphic lthy [pcrel]
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    53
    val rty_raw = (domain_type o fastype_of) crel_poly
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    54
    val (quot_thm, assms) = Lifting_Term.prove_param_quot_thm lthy rty_raw
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    55
    val args = map (fn (_, q_thm) => quot_thm_crel q_thm) assms
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    56
    val parametrized_relator = quot_thm_crel quot_thm
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    57
    val [rty, rty'] = (binder_types o fastype_of) parametrized_relator
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    58
    val thy = Proof_Context.theory_of lthy
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    59
    val tyenv_match = Sign.typ_match thy (rty_raw, rty') Vartab.empty
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    60
    val crel_subst = Envir.subst_term (tyenv_match,Vartab.empty) crel_poly
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    61
    val lthy = Variable.declare_names parametrized_relator lthy
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    62
    val (crel_typed, lthy) = yield_singleton Variable.importT_terms crel_subst lthy
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    63
    val qty = (domain_type o range_type o fastype_of) crel_typed
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    64
    val relcomp_op = Const (@{const_name "relcompp"}, 
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    65
          (rty --> rty' --> HOLogic.boolT) --> 
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    66
          (rty' --> qty --> HOLogic.boolT) --> 
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    67
          rty --> qty --> HOLogic.boolT)
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    68
    val relator_type = foldr1 (op -->) ((map type_of args) @ [rty, qty, HOLogic.boolT])
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    69
    val qty_name = (fst o dest_Type) qty
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    70
    val pcrel_name = Binding.prefix_name "pcr_" ((Binding.name o Long_Name.base_name) qty_name)
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    71
    val lhs = Library.foldl (op $) ((Free (Binding.name_of pcrel_name, relator_type)), args)
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    72
    val rhs = relcomp_op $ parametrized_relator $ crel_typed
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    73
    val definition_term = Logic.mk_equals (lhs, rhs)
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    74
    val ((_, (_, def_thm)), lthy) = Specification.definition ((SOME (pcrel_name, SOME relator_type, NoSyn)), 
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    75
      ((Binding.empty, []), definition_term)) lthy
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    76
  in
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    77
    (SOME def_thm, lthy)
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    78
  end
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    79
  handle Lifting_Term.PARAM_QUOT_THM (_, msg) => (print_define_pcrel_warning msg; (NONE, lthy))
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
    80
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
    81
fun define_code_constr gen_code quot_thm lthy =
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
    82
  let
47951
8c8a03765de7 quot_del attribute, it allows us to deregister quotient types
kuncar
parents: 47943
diff changeset
    83
    val abs = quot_thm_abs quot_thm
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
    84
    val abs_background = Morphism.term (Local_Theory.target_morphism lthy) abs
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
    85
  in
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
    86
    if gen_code andalso is_Const abs_background then
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
    87
      let
50176
701747176952 simplified code
kuncar
parents: 50175
diff changeset
    88
        val (fixed_abs_background, lthy') = yield_singleton(Variable.importT_terms) abs_background lthy
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
    89
      in  
50176
701747176952 simplified code
kuncar
parents: 50175
diff changeset
    90
         Local_Theory.background_theory(Code.add_datatype [dest_Const fixed_abs_background]) lthy'
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
    91
      end
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
    92
    else
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
    93
      lthy
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
    94
  end
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
    95
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
    96
fun define_abs_type gen_code quot_thm lthy =
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
    97
  if gen_code andalso Lifting_Def.can_generate_code_cert quot_thm then
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    98
    let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    99
      val abs_type_thm = quot_thm RS @{thm Quotient_abs_rep}
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   100
      val add_abstype_attribute = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   101
          Thm.declaration_attribute (fn thm => Context.mapping (Code.add_abstype thm) I)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   102
        val add_abstype_attrib = Attrib.internal (K add_abstype_attribute);
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   103
    in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   104
      lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   105
        |> (snd oo Local_Theory.note) ((Binding.empty, [add_abstype_attrib]), [abs_type_thm])
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   106
    end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   107
  else
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   108
    lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   109
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
   110
fun quot_thm_sanity_check ctxt quot_thm =
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
   111
  let
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
   112
    val ((_, [quot_thm_fixed]), ctxt') = Variable.importT [quot_thm] ctxt 
47951
8c8a03765de7 quot_del attribute, it allows us to deregister quotient types
kuncar
parents: 47943
diff changeset
   113
    val (rty, qty) = quot_thm_rty_qty quot_thm_fixed
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
   114
    val rty_tfreesT = Term.add_tfree_namesT rty []
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
   115
    val qty_tfreesT = Term.add_tfree_namesT qty []
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
   116
    val extra_rty_tfrees =
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   117
      case subtract (op =) qty_tfreesT rty_tfreesT of
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
   118
        [] => []
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
   119
      | extras => [Pretty.block ([Pretty.str "Extra variables in the raw type:",
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
   120
                                 Pretty.brk 1] @ 
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
   121
                                 ((Pretty.commas o map (Pretty.str o quote)) extras) @
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   122
                                 [Pretty.str "."])]
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
   123
    val not_type_constr = 
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   124
      case qty of
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
   125
         Type _ => []
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
   126
         | _ => [Pretty.block [Pretty.str "The quotient type ",
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
   127
                                Pretty.quote (Syntax.pretty_typ ctxt' qty),
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
   128
                                Pretty.brk 1,
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   129
                                Pretty.str "is not a type constructor."]]
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
   130
    val errs = extra_rty_tfrees @ not_type_constr
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
   131
  in
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
   132
    if null errs then () else error (cat_lines (["Sanity check of the quotient theorem failed:",""] 
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
   133
                                                @ (map Pretty.string_of errs)))
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
   134
  end
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
   135
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   136
fun setup_lifting_infr gen_code quot_thm maybe_reflp_thm lthy =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   137
  let
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
   138
    val _ = quot_thm_sanity_check lthy quot_thm
47951
8c8a03765de7 quot_del attribute, it allows us to deregister quotient types
kuncar
parents: 47943
diff changeset
   139
    val (_, qtyp) = quot_thm_rty_qty quot_thm
50227
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
   140
    val (pcrel_def, lthy) = define_pcrel (quot_thm_crel quot_thm) lthy
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
   141
    val quotients = { quot_thm = quot_thm, pcrel_def = pcrel_def }
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
   142
    val qty_full_name = (fst o dest_Type) qtyp  
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   143
    fun quot_info phi = Lifting_Info.transform_quotients phi quotients
50227
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
   144
    val lthy = case maybe_reflp_thm of
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   145
      SOME reflp_thm => lthy
47982
7aa35601ff65 prove reflexivity also for the quotient composition relation; reflp_preserve renamed to reflexivity_rule
kuncar
parents: 47951
diff changeset
   146
        |> (snd oo Local_Theory.note) ((Binding.empty, [Lifting_Info.add_reflexivity_rule_attrib]),
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   147
              [reflp_thm])
47982
7aa35601ff65 prove reflexivity also for the quotient composition relation; reflp_preserve renamed to reflexivity_rule
kuncar
parents: 47951
diff changeset
   148
        |> (snd oo Local_Theory.note) ((Binding.empty, [Lifting_Info.add_reflexivity_rule_attrib]),
7aa35601ff65 prove reflexivity also for the quotient composition relation; reflp_preserve renamed to reflexivity_rule
kuncar
parents: 47951
diff changeset
   149
              [[quot_thm, reflp_thm] MRSL @{thm Quotient_to_left_total}])
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   150
        |> define_code_constr gen_code quot_thm
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   151
      | NONE => lthy
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   152
        |> define_abs_type gen_code quot_thm
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   153
  in
50227
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
   154
    lthy
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   155
      |> Local_Theory.declaration {syntax = false, pervasive = true}
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   156
        (fn phi => Lifting_Info.update_quotients qty_full_name (quot_info phi))
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   157
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   158
47852
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   159
(*
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   160
  Sets up the Lifting package by a quotient theorem.
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   161
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   162
  gen_code - flag if an abstract type given by quot_thm should be registred 
47852
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   163
    as an abstract type in the code generator
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   164
  quot_thm - a quotient theorem (Quotient R Abs Rep T)
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   165
  maybe_reflp_thm - a theorem saying that a relation from quot_thm is reflexive
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   166
    (in the form "reflp R")
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   167
*)
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   168
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   169
fun setup_by_quotient gen_code quot_thm maybe_reflp_thm lthy =
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   170
  let
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   171
    val transfer_attr = Attrib.internal (K Transfer.transfer_add)
47951
8c8a03765de7 quot_del attribute, it allows us to deregister quotient types
kuncar
parents: 47943
diff changeset
   172
    val (_, qty) = quot_thm_rty_qty quot_thm
47575
b90cd7016d4f generate abs_induct rules for quotient types
huffman
parents: 47566
diff changeset
   173
    val induct_attr = Attrib.internal (K (Induct.induct_type (fst (dest_Type qty))))
47583
f3f0e06549c2 create thm names correctly
kuncar
parents: 47575
diff changeset
   174
    val qty_name = (Binding.name o Long_Name.base_name o fst o dest_Type) qty
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   175
    fun qualify suffix = Binding.qualified true suffix qty_name
50227
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
   176
    val lthy = case maybe_reflp_thm of
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   177
      SOME reflp_thm => lthy
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   178
        |> (snd oo Local_Theory.note) ((qualify "bi_total", [transfer_attr]), 
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   179
          [[quot_thm, reflp_thm] MRSL @{thm Quotient_bi_total}])
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   180
        |> (snd oo Local_Theory.note) ((qualify "id_abs_transfer", [transfer_attr]), 
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   181
          [[quot_thm, reflp_thm] MRSL @{thm Quotient_id_abs_transfer}])
47575
b90cd7016d4f generate abs_induct rules for quotient types
huffman
parents: 47566
diff changeset
   182
        |> (snd oo Local_Theory.note) ((qualify "abs_induct", [induct_attr]),
b90cd7016d4f generate abs_induct rules for quotient types
huffman
parents: 47566
diff changeset
   183
          [[quot_thm, reflp_thm] MRSL @{thm Quotient_total_abs_induct}])
47889
29212a4bb866 lifting package produces abs_eq_iff rules for total quotients
huffman
parents: 47852
diff changeset
   184
        |> (snd oo Local_Theory.note) ((qualify "abs_eq_iff", []),
29212a4bb866 lifting package produces abs_eq_iff rules for total quotients
huffman
parents: 47852
diff changeset
   185
          [[quot_thm, reflp_thm] MRSL @{thm Quotient_total_abs_eq_iff}])
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   186
      | NONE => lthy
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   187
        |> (snd oo Local_Theory.note) ((qualify "All_transfer", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   188
          [quot_thm RS @{thm Quotient_All_transfer}])
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   189
        |> (snd oo Local_Theory.note) ((qualify "Ex_transfer", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   190
          [quot_thm RS @{thm Quotient_Ex_transfer}])
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   191
        |> (snd oo Local_Theory.note) ((qualify "forall_transfer", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   192
          [quot_thm RS @{thm Quotient_forall_transfer}])
47575
b90cd7016d4f generate abs_induct rules for quotient types
huffman
parents: 47566
diff changeset
   193
        |> (snd oo Local_Theory.note) ((qualify "abs_induct", [induct_attr]),
b90cd7016d4f generate abs_induct rules for quotient types
huffman
parents: 47566
diff changeset
   194
          [quot_thm RS @{thm Quotient_abs_induct}])
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   195
  in
50227
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
   196
    lthy
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   197
      |> (snd oo Local_Theory.note) ((qualify "right_unique", [transfer_attr]), 
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   198
        [quot_thm RS @{thm Quotient_right_unique}])
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   199
      |> (snd oo Local_Theory.note) ((qualify "right_total", [transfer_attr]), 
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   200
        [quot_thm RS @{thm Quotient_right_total}])
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   201
      |> (snd oo Local_Theory.note) ((qualify "rel_eq_transfer", [transfer_attr]), 
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   202
        [quot_thm RS @{thm Quotient_rel_eq_transfer}])
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   203
      |> setup_lifting_infr gen_code quot_thm maybe_reflp_thm
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   204
  end
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   205
47852
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   206
(*
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   207
  Sets up the Lifting package by a typedef theorem.
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   208
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   209
  gen_code - flag if an abstract type given by typedef_thm should be registred 
47852
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   210
    as an abstract type in the code generator
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   211
  typedef_thm - a typedef theorem (type_definition Rep Abs S)
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   212
*)
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   213
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   214
fun setup_by_typedef_thm gen_code typedef_thm lthy =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   215
  let
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   216
    val transfer_attr = Attrib.internal (K Transfer.transfer_add)
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   217
    val (_ $ rep_fun $ _ $ typedef_set) = (HOLogic.dest_Trueprop o prop_of) typedef_thm
50227
01d545993e8c generate a parameterized correspondence relation
kuncar
parents: 50176
diff changeset
   218
    val (T_def, lthy') = define_crel rep_fun lthy
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   219
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   220
    val quot_thm = case typedef_set of
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   221
      Const ("Orderings.top_class.top", _) => 
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   222
        [typedef_thm, T_def] MRSL @{thm UNIV_typedef_to_Quotient}
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   223
      | Const (@{const_name "Collect"}, _) $ Abs (_, _, _) => 
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   224
        [typedef_thm, T_def] MRSL @{thm open_typedef_to_Quotient}
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   225
      | _ => 
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   226
        [typedef_thm, T_def] MRSL @{thm typedef_to_Quotient}
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   227
47951
8c8a03765de7 quot_del attribute, it allows us to deregister quotient types
kuncar
parents: 47943
diff changeset
   228
    val (_, qty) = quot_thm_rty_qty quot_thm
47583
f3f0e06549c2 create thm names correctly
kuncar
parents: 47575
diff changeset
   229
    val qty_name = (Binding.name o Long_Name.base_name o fst o dest_Type) qty
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   230
    fun qualify suffix = Binding.qualified true suffix qty_name
47623
01e4fdf9d748 setup_lifting preprocesses forall_transfer rule by unfolding mem_Collect_eq
huffman
parents: 47608
diff changeset
   231
    val simplify = Raw_Simplifier.rewrite_rule [mk_meta_eq @{thm mem_Collect_eq}]
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   232
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   233
    val (maybe_reflp_thm, lthy'') = case typedef_set of
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   234
      Const ("Orderings.top_class.top", _) => 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   235
        let
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   236
          val equivp_thm = typedef_thm RS @{thm UNIV_typedef_to_equivp}
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   237
          val reflp_thm = equivp_thm RS @{thm equivp_reflp2}
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   238
        in
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   239
          lthy'
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   240
            |> (snd oo Local_Theory.note) ((qualify "bi_total", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   241
              [[quot_thm, reflp_thm] MRSL @{thm Quotient_bi_total}])
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   242
            |> (snd oo Local_Theory.note) ((qualify "id_abs_transfer", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   243
              [[quot_thm, reflp_thm] MRSL @{thm Quotient_id_abs_transfer}])
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   244
            |> pair (SOME reflp_thm)
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   245
        end
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   246
      | _ => lthy'
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   247
        |> (snd oo Local_Theory.note) ((qualify "All_transfer", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   248
          [[typedef_thm, T_def] MRSL @{thm typedef_All_transfer}])
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   249
        |> (snd oo Local_Theory.note) ((qualify "Ex_transfer", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   250
          [[typedef_thm, T_def] MRSL @{thm typedef_Ex_transfer}])
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   251
        |> (snd oo Local_Theory.note) ((qualify "forall_transfer", [transfer_attr]), 
47623
01e4fdf9d748 setup_lifting preprocesses forall_transfer rule by unfolding mem_Collect_eq
huffman
parents: 47608
diff changeset
   252
          [simplify ([typedef_thm, T_def] MRSL @{thm typedef_forall_transfer})])
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   253
        |> pair NONE
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   254
  in
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   255
    lthy''
47943
c09326cedb41 note Quotient theorem for typedefs in setup_lifting
kuncar
parents: 47937
diff changeset
   256
      |> (snd oo Local_Theory.note) ((Binding.prefix_name "Quotient_" qty_name, []), 
c09326cedb41 note Quotient theorem for typedefs in setup_lifting
kuncar
parents: 47937
diff changeset
   257
        [quot_thm])
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   258
      |> (snd oo Local_Theory.note) ((qualify "bi_unique", [transfer_attr]), 
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   259
        [[typedef_thm, T_def] MRSL @{thm typedef_bi_unique}])
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   260
      |> (snd oo Local_Theory.note) ((qualify "rep_transfer", [transfer_attr]), 
47535
0f94b02fda1c lifting_setup generates transfer rule for rep of typedefs
huffman
parents: 47534
diff changeset
   261
        [[typedef_thm, T_def] MRSL @{thm typedef_rep_transfer}])
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   262
      |> (snd oo Local_Theory.note) ((qualify "right_unique", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   263
        [[quot_thm] MRSL @{thm Quotient_right_unique}])
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   264
      |> (snd oo Local_Theory.note) ((qualify "right_total", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   265
        [[quot_thm] MRSL @{thm Quotient_right_total}])
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   266
      |> setup_lifting_infr gen_code quot_thm maybe_reflp_thm
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   267
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   268
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   269
fun setup_lifting_cmd gen_code xthm opt_reflp_xthm lthy =
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   270
  let 
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   271
    val input_thm = singleton (Attrib.eval_thms lthy) xthm
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   272
    val input_term = (HOLogic.dest_Trueprop o prop_of) input_thm
47566
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   273
      handle TERM _ => error "Unsupported type of a theorem. Only Quotient or type_definition are supported."
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   274
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   275
    fun sanity_check_reflp_thm reflp_thm = 
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   276
      let
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   277
        val reflp_tm = (HOLogic.dest_Trueprop o prop_of) reflp_thm
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   278
          handle TERM _ => error "Invalid form of the reflexivity theorem. Use \"reflp R\"."
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   279
      in
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   280
        case reflp_tm of
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   281
          Const (@{const_name reflp}, _) $ _ => ()
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   282
          | _ => error "Invalid form of the reflexivity theorem. Use \"reflp R\"."
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   283
      end
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   284
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   285
    fun setup_quotient () = 
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   286
      case opt_reflp_xthm of
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   287
        SOME reflp_xthm => 
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   288
          let
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   289
            val reflp_thm = singleton (Attrib.eval_thms lthy) reflp_xthm
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   290
            val _ = sanity_check_reflp_thm reflp_thm
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   291
          in
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   292
            setup_by_quotient gen_code input_thm (SOME reflp_thm) lthy
47566
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   293
          end
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   294
        | NONE => setup_by_quotient gen_code input_thm NONE lthy
47566
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   295
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   296
    fun setup_typedef () = 
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   297
      case opt_reflp_xthm of
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   298
        SOME _ => error "The reflexivity theorem cannot be specified if the type_definition theorem is used."
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   299
        | NONE => setup_by_typedef_thm gen_code input_thm lthy
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   300
  in
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   301
    case input_term of
47566
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   302
      (Const (@{const_name Quotient}, _) $ _ $ _ $ _ $ _) => setup_quotient ()
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   303
      | (Const (@{const_name type_definition}, _) $ _ $ _ $ _) => setup_typedef ()
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   304
      | _ => error "Unsupported type of a theorem. Only Quotient or type_definition are supported."
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   305
  end
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   306
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   307
val opt_gen_code =
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   308
  Scan.optional (@{keyword "("} |-- Parse.!!! ((Parse.reserved "no_code" >> K false) --| @{keyword ")"})) true
47566
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   309
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   310
val _ = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   311
  Outer_Syntax.local_theory @{command_spec "setup_lifting"}
50214
67fb9a168d10 tuned command descriptions;
wenzelm
parents: 50176
diff changeset
   312
    "setup lifting infrastructure" 
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   313
      (opt_gen_code -- Parse_Spec.xthm -- Scan.option Parse_Spec.xthm >> 
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47936
diff changeset
   314
        (fn ((gen_code, xthm), opt_reflp_xthm) => setup_lifting_cmd gen_code xthm opt_reflp_xthm))
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   315
end;