src/HOL/Tools/Lifting/lifting_setup.ML
author huffman
Sat, 21 Apr 2012 10:59:52 +0200
changeset 47647 ec29cc09599d
parent 47623 01e4fdf9d748
child 47698 18202d3d5832
permissions -rw-r--r--
renamed contravariant relator set_rel to vset_rel, to make room for new covariant relator
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_lifting_infr: bool -> thm -> local_theory -> local_theory
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
    12
47566
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
    13
  val setup_by_quotient: bool -> thm -> thm option -> local_theory -> local_theory
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    14
47566
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
    15
  val setup_by_typedef_thm: bool -> thm -> local_theory -> local_theory
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    16
end;
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    17
47334
4708384e759d fix typo in ML structure name
huffman
parents: 47308
diff changeset
    18
structure Lifting_Setup: LIFTING_SETUP =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    19
struct
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    20
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    21
infix 0 MRSL
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    22
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    23
fun ants MRSL thm = fold (fn rl => fn thm => rl RS thm) ants thm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    24
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    25
exception SETUP_LIFTING_INFR of string
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    26
47361
87c0eaf04bad support non-open typedefs; define cr_rel in terms of a rep function for typedefs
kuncar
parents: 47352
diff changeset
    27
fun define_cr_rel rep_fun lthy =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    28
  let
47361
87c0eaf04bad support non-open typedefs; define cr_rel in terms of a rep function for typedefs
kuncar
parents: 47352
diff changeset
    29
    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
    30
    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
    31
    val def_term = Abs ("x", rty, Abs ("y", qty, rep_fun_graph));
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    32
    val qty_name = (fst o dest_Type) qty
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    33
    val cr_rel_name = Binding.prefix_name "cr_" (Binding.qualified_name qty_name)  
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    34
    val (fixed_def_term, lthy') = yield_singleton (Variable.importT_terms) def_term lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    35
    val ((_, (_ , def_thm)), lthy'') =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    36
      Local_Theory.define ((cr_rel_name, NoSyn), ((Thm.def_binding cr_rel_name, []), fixed_def_term)) lthy'
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    37
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    38
    (def_thm, lthy'')
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    39
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    40
47608
572d7e51de4d rename no_code to no_abs_code - more appropriate name
kuncar
parents: 47583
diff changeset
    41
fun define_abs_type no_abs_code quot_thm lthy =
572d7e51de4d rename no_code to no_abs_code - more appropriate name
kuncar
parents: 47583
diff changeset
    42
  if not no_abs_code andalso Lifting_Def.can_generate_code_cert quot_thm then
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    43
    let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    44
      val abs_type_thm = quot_thm RS @{thm Quotient_abs_rep}
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    45
      val add_abstype_attribute = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    46
          Thm.declaration_attribute (fn thm => Context.mapping (Code.add_abstype thm) I)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    47
        val add_abstype_attrib = Attrib.internal (K add_abstype_attribute);
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    48
    in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    49
      lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    50
        |> (snd oo Local_Theory.note) ((Binding.empty, [add_abstype_attrib]), [abs_type_thm])
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    51
    end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    52
  else
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    53
    lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    54
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
    55
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
    56
  let
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
    57
    val ((_, [quot_thm_fixed]), ctxt') = Variable.importT [quot_thm] ctxt 
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
    58
    val (rty, qty) = Lifting_Term.quot_thm_rty_qty quot_thm_fixed
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
    59
    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
    60
    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
    61
    val extra_rty_tfrees =
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
    62
      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
    63
        [] => []
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
    64
      | 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
    65
                                 Pretty.brk 1] @ 
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
    66
                                 ((Pretty.commas o map (Pretty.str o quote)) extras) @
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
    67
                                 [Pretty.str "."])]
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
    68
    val not_type_constr = 
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
    69
      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
    70
         Type _ => []
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
    71
         | _ => [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
    72
                                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
    73
                                Pretty.brk 1,
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
    74
                                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
    75
    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
    76
  in
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
    77
    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
    78
                                                @ (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
    79
  end
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
    80
47608
572d7e51de4d rename no_code to no_abs_code - more appropriate name
kuncar
parents: 47583
diff changeset
    81
fun setup_lifting_infr no_abs_code quot_thm lthy =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    82
  let
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
    83
    val _ = quot_thm_sanity_check lthy quot_thm
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    84
    val (_, qtyp) = Lifting_Term.quot_thm_rty_qty quot_thm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    85
    val qty_full_name = (fst o dest_Type) qtyp
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    86
    val quotients = { quot_thm = quot_thm }
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    87
    fun quot_info phi = Lifting_Info.transform_quotients phi quotients
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    88
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    89
    lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    90
      |> Local_Theory.declaration {syntax = false, pervasive = true}
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    91
        (fn phi => Lifting_Info.update_quotients qty_full_name (quot_info phi))
47608
572d7e51de4d rename no_code to no_abs_code - more appropriate name
kuncar
parents: 47583
diff changeset
    92
      |> define_abs_type no_abs_code quot_thm
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    93
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    94
47608
572d7e51de4d rename no_code to no_abs_code - more appropriate name
kuncar
parents: 47583
diff changeset
    95
fun setup_by_quotient no_abs_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
    96
  let
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
    97
    val transfer_attr = Attrib.internal (K Transfer.transfer_add)
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
    98
    val (_, qty) = Lifting_Term.quot_thm_rty_qty quot_thm
47575
b90cd7016d4f generate abs_induct rules for quotient types
huffman
parents: 47566
diff changeset
    99
    val induct_attr = Attrib.internal (K (Induct.induct_type (fst (dest_Type qty))))
47583
f3f0e06549c2 create thm names correctly
kuncar
parents: 47575
diff changeset
   100
    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
   101
    fun qualify suffix = Binding.qualified true suffix qty_name
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   102
    val lthy' = case maybe_reflp_thm of
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   103
      SOME reflp_thm => lthy
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   104
        |> (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
   105
          [[quot_thm, reflp_thm] MRSL @{thm Quotient_bi_total}])
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   106
        |> (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
   107
          [[quot_thm, reflp_thm] MRSL @{thm Quotient_id_abs_transfer}])
47575
b90cd7016d4f generate abs_induct rules for quotient types
huffman
parents: 47566
diff changeset
   108
        |> (snd oo Local_Theory.note) ((qualify "abs_induct", [induct_attr]),
b90cd7016d4f generate abs_induct rules for quotient types
huffman
parents: 47566
diff changeset
   109
          [[quot_thm, reflp_thm] MRSL @{thm Quotient_total_abs_induct}])
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   110
      | NONE => lthy
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   111
        |> (snd oo Local_Theory.note) ((qualify "All_transfer", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   112
          [quot_thm RS @{thm Quotient_All_transfer}])
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   113
        |> (snd oo Local_Theory.note) ((qualify "Ex_transfer", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   114
          [quot_thm RS @{thm Quotient_Ex_transfer}])
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   115
        |> (snd oo Local_Theory.note) ((qualify "forall_transfer", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   116
          [quot_thm RS @{thm Quotient_forall_transfer}])
47575
b90cd7016d4f generate abs_induct rules for quotient types
huffman
parents: 47566
diff changeset
   117
        |> (snd oo Local_Theory.note) ((qualify "abs_induct", [induct_attr]),
b90cd7016d4f generate abs_induct rules for quotient types
huffman
parents: 47566
diff changeset
   118
          [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
   119
  in
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   120
    lthy'
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   121
      |> (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
   122
        [quot_thm RS @{thm Quotient_right_unique}])
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   123
      |> (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
   124
        [quot_thm RS @{thm Quotient_right_total}])
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   125
      |> (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
   126
        [quot_thm RS @{thm Quotient_rel_eq_transfer}])
47608
572d7e51de4d rename no_code to no_abs_code - more appropriate name
kuncar
parents: 47583
diff changeset
   127
      |> setup_lifting_infr no_abs_code quot_thm
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   128
  end
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   129
47608
572d7e51de4d rename no_code to no_abs_code - more appropriate name
kuncar
parents: 47583
diff changeset
   130
fun setup_by_typedef_thm no_abs_code typedef_thm lthy =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   131
  let
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   132
    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
   133
    val (_ $ rep_fun $ _ $ typedef_set) = (HOLogic.dest_Trueprop o prop_of) typedef_thm
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   134
    val (T_def, lthy') = define_cr_rel rep_fun lthy
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   135
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   136
    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
   137
      Const ("Orderings.top_class.top", _) => 
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   138
        [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
   139
      | Const (@{const_name "Collect"}, _) $ Abs (_, _, _) => 
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   140
        [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
   141
      | _ => 
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   142
        [typedef_thm, T_def] MRSL @{thm typedef_to_Quotient}
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   143
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   144
    val (_, qty) = Lifting_Term.quot_thm_rty_qty quot_thm
47583
f3f0e06549c2 create thm names correctly
kuncar
parents: 47575
diff changeset
   145
    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
   146
    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
   147
    val simplify = Raw_Simplifier.rewrite_rule [mk_meta_eq @{thm mem_Collect_eq}]
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   148
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   149
    val lthy'' = case typedef_set of
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   150
      Const ("Orderings.top_class.top", _) => 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   151
        let
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   152
          val equivp_thm = typedef_thm RS @{thm UNIV_typedef_to_equivp}
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   153
          val reflp_thm = equivp_thm RS @{thm equivp_reflp2}
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   154
        in
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   155
          lthy'
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   156
            |> (snd oo Local_Theory.note) ((qualify "bi_total", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   157
              [[quot_thm, reflp_thm] MRSL @{thm Quotient_bi_total}])
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   158
            |> (snd oo Local_Theory.note) ((qualify "id_abs_transfer", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   159
              [[quot_thm, reflp_thm] MRSL @{thm Quotient_id_abs_transfer}])
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   160
        end
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   161
      | _ => lthy'
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   162
        |> (snd oo Local_Theory.note) ((qualify "All_transfer", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   163
          [[typedef_thm, T_def] MRSL @{thm typedef_All_transfer}])
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   164
        |> (snd oo Local_Theory.note) ((qualify "Ex_transfer", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   165
          [[typedef_thm, T_def] MRSL @{thm typedef_Ex_transfer}])
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   166
        |> (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
   167
          [simplify ([typedef_thm, T_def] MRSL @{thm typedef_forall_transfer})])
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   168
  in
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   169
    lthy''
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   170
      |> (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
   171
        [[typedef_thm, T_def] MRSL @{thm typedef_bi_unique}])
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   172
      |> (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
   173
        [[typedef_thm, T_def] MRSL @{thm typedef_rep_transfer}])
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   174
      |> (snd oo Local_Theory.note) ((qualify "right_unique", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   175
        [[quot_thm] MRSL @{thm Quotient_right_unique}])
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   176
      |> (snd oo Local_Theory.note) ((qualify "right_total", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   177
        [[quot_thm] MRSL @{thm Quotient_right_total}])
47608
572d7e51de4d rename no_code to no_abs_code - more appropriate name
kuncar
parents: 47583
diff changeset
   178
      |> setup_lifting_infr no_abs_code quot_thm
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   179
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   180
47608
572d7e51de4d rename no_code to no_abs_code - more appropriate name
kuncar
parents: 47583
diff changeset
   181
fun setup_lifting_cmd no_abs_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
   182
  let 
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   183
    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
   184
    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
   185
      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
   186
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   187
    fun sanity_check_reflp_thm reflp_thm = 
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   188
      let
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   189
        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
   190
          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
   191
      in
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   192
        case reflp_tm of
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   193
          Const (@{const_name reflp}, _) $ _ => ()
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   194
          | _ => 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
   195
      end
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   196
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   197
    fun setup_quotient () = 
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   198
      case opt_reflp_xthm of
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   199
        SOME reflp_xthm => 
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   200
          let
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   201
            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
   202
            val _ = sanity_check_reflp_thm reflp_thm
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   203
          in
47608
572d7e51de4d rename no_code to no_abs_code - more appropriate name
kuncar
parents: 47583
diff changeset
   204
            setup_by_quotient no_abs_code input_thm (SOME reflp_thm) lthy
47566
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   205
          end
47608
572d7e51de4d rename no_code to no_abs_code - more appropriate name
kuncar
parents: 47583
diff changeset
   206
        | NONE => setup_by_quotient no_abs_code input_thm NONE lthy
47566
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   207
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   208
    fun setup_typedef () = 
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   209
      case opt_reflp_xthm of
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   210
        SOME _ => error "The reflexivity theorem cannot be specified if the type_definition theorem is used."
47608
572d7e51de4d rename no_code to no_abs_code - more appropriate name
kuncar
parents: 47583
diff changeset
   211
        | NONE => setup_by_typedef_thm no_abs_code input_thm lthy
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   212
  in
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   213
    case input_term of
47566
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   214
      (Const (@{const_name Quotient}, _) $ _ $ _ $ _ $ _) => setup_quotient ()
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   215
      | (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
   216
      | _ => 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
   217
  end
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   218
47608
572d7e51de4d rename no_code to no_abs_code - more appropriate name
kuncar
parents: 47583
diff changeset
   219
val opt_no_abs_code =
572d7e51de4d rename no_code to no_abs_code - more appropriate name
kuncar
parents: 47583
diff changeset
   220
  Scan.optional (@{keyword "("} |-- Parse.!!! ((Parse.reserved "no_abs_code" >> K true) --| @{keyword ")"})) false
47566
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   221
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   222
val _ = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   223
  Outer_Syntax.local_theory @{command_spec "setup_lifting"}
47352
e0bff2ae939f fix typos
huffman
parents: 47334
diff changeset
   224
    "Setup lifting infrastructure" 
47608
572d7e51de4d rename no_code to no_abs_code - more appropriate name
kuncar
parents: 47583
diff changeset
   225
      (opt_no_abs_code -- Parse_Spec.xthm -- Scan.option Parse_Spec.xthm >> 
572d7e51de4d rename no_code to no_abs_code - more appropriate name
kuncar
parents: 47583
diff changeset
   226
        (fn ((no_abs_code, xthm), opt_reflp_xthm) => setup_lifting_cmd no_abs_code xthm opt_reflp_xthm))
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   227
end;