src/HOL/Tools/Lifting/lifting_setup.ML
author wenzelm
Sun, 20 May 2012 11:34:33 +0200
changeset 47884 21c42b095c84
parent 47852 0c3b8d036a5c
child 47889 29212a4bb866
permissions -rw-r--r--
try to avoid races again (cf. 8c37cb84065f and fd3a36e48b09);
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
47361
87c0eaf04bad support non-open typedefs; define cr_rel in terms of a rep function for typedefs
kuncar
parents: 47352
diff changeset
    25
fun define_cr_rel 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));
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    30
    val qty_name = (fst o dest_Type) qty
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    31
    val cr_rel_name = Binding.prefix_name "cr_" (Binding.qualified_name qty_name)  
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'') =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    34
      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
    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
47779
5a10e24fe7ab tuned; don't generate abs code if quotient_type is used
kuncar
parents: 47698
diff changeset
    39
fun define_abs_type gen_abs_code quot_thm lthy =
5a10e24fe7ab tuned; don't generate abs code if quotient_type is used
kuncar
parents: 47698
diff changeset
    40
  if gen_abs_code andalso Lifting_Def.can_generate_code_cert quot_thm then
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    41
    let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    42
      val abs_type_thm = quot_thm RS @{thm Quotient_abs_rep}
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    43
      val add_abstype_attribute = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    44
          Thm.declaration_attribute (fn thm => Context.mapping (Code.add_abstype thm) I)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    45
        val add_abstype_attrib = Attrib.internal (K add_abstype_attribute);
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    46
    in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    47
      lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    48
        |> (snd oo Local_Theory.note) ((Binding.empty, [add_abstype_attrib]), [abs_type_thm])
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    49
    end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    50
  else
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    51
    lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    52
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
    53
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
    54
  let
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
    55
    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
    56
    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
    57
    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
    58
    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
    59
    val extra_rty_tfrees =
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
    60
      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
    61
        [] => []
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
    62
      | 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
    63
                                 Pretty.brk 1] @ 
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
    64
                                 ((Pretty.commas o map (Pretty.str o quote)) extras) @
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
    65
                                 [Pretty.str "."])]
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
    66
    val not_type_constr = 
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
    67
      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
    68
         Type _ => []
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
    69
         | _ => [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
    70
                                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
    71
                                Pretty.brk 1,
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
    72
                                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
    73
    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
    74
  in
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
    75
    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
    76
                                                @ (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
    77
  end
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
    78
47779
5a10e24fe7ab tuned; don't generate abs code if quotient_type is used
kuncar
parents: 47698
diff changeset
    79
fun setup_lifting_infr gen_abs_code quot_thm lthy =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    80
  let
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47361
diff changeset
    81
    val _ = quot_thm_sanity_check lthy quot_thm
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    82
    val (_, qtyp) = Lifting_Term.quot_thm_rty_qty quot_thm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    83
    val qty_full_name = (fst o dest_Type) qtyp
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    84
    val quotients = { quot_thm = quot_thm }
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    85
    fun quot_info phi = Lifting_Info.transform_quotients phi quotients
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    86
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    87
    lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    88
      |> Local_Theory.declaration {syntax = false, pervasive = true}
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    89
        (fn phi => Lifting_Info.update_quotients qty_full_name (quot_info phi))
47779
5a10e24fe7ab tuned; don't generate abs code if quotient_type is used
kuncar
parents: 47698
diff changeset
    90
      |> define_abs_type gen_abs_code quot_thm
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    91
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    92
47852
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
    93
(*
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
    94
  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
    95
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
    96
  gen_abs_code - flag if an abstract type given by quot_thm should be registred 
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
    97
    as an abstract type in the code generator
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
    98
  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
    99
  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
   100
    (in the form "reflp R")
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   101
*)
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   102
47779
5a10e24fe7ab tuned; don't generate abs code if quotient_type is used
kuncar
parents: 47698
diff changeset
   103
fun setup_by_quotient gen_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
   104
  let
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   105
    val transfer_attr = Attrib.internal (K Transfer.transfer_add)
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   106
    val (_, qty) = Lifting_Term.quot_thm_rty_qty quot_thm
47575
b90cd7016d4f generate abs_induct rules for quotient types
huffman
parents: 47566
diff changeset
   107
    val induct_attr = Attrib.internal (K (Induct.induct_type (fst (dest_Type qty))))
47583
f3f0e06549c2 create thm names correctly
kuncar
parents: 47575
diff changeset
   108
    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
   109
    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
   110
    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
   111
      SOME reflp_thm => lthy
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   112
        |> (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
   113
          [[quot_thm, reflp_thm] MRSL @{thm Quotient_bi_total}])
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   114
        |> (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
   115
          [[quot_thm, reflp_thm] MRSL @{thm Quotient_id_abs_transfer}])
47575
b90cd7016d4f generate abs_induct rules for quotient types
huffman
parents: 47566
diff changeset
   116
        |> (snd oo Local_Theory.note) ((qualify "abs_induct", [induct_attr]),
b90cd7016d4f generate abs_induct rules for quotient types
huffman
parents: 47566
diff changeset
   117
          [[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
   118
      | NONE => lthy
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   119
        |> (snd oo Local_Theory.note) ((qualify "All_transfer", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   120
          [quot_thm RS @{thm Quotient_All_transfer}])
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   121
        |> (snd oo Local_Theory.note) ((qualify "Ex_transfer", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   122
          [quot_thm RS @{thm Quotient_Ex_transfer}])
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   123
        |> (snd oo Local_Theory.note) ((qualify "forall_transfer", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   124
          [quot_thm RS @{thm Quotient_forall_transfer}])
47575
b90cd7016d4f generate abs_induct rules for quotient types
huffman
parents: 47566
diff changeset
   125
        |> (snd oo Local_Theory.note) ((qualify "abs_induct", [induct_attr]),
b90cd7016d4f generate abs_induct rules for quotient types
huffman
parents: 47566
diff changeset
   126
          [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
   127
  in
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   128
    lthy'
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   129
      |> (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
   130
        [quot_thm RS @{thm Quotient_right_unique}])
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   131
      |> (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
   132
        [quot_thm RS @{thm Quotient_right_total}])
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   133
      |> (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
   134
        [quot_thm RS @{thm Quotient_rel_eq_transfer}])
47779
5a10e24fe7ab tuned; don't generate abs code if quotient_type is used
kuncar
parents: 47698
diff changeset
   135
      |> setup_lifting_infr gen_abs_code quot_thm
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   136
  end
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   137
47852
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   138
(*
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   139
  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
   140
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   141
  gen_abs_code - flag if an abstract type given by typedef_thm should be registred 
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   142
    as an abstract type in the code generator
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   143
  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
   144
*)
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47779
diff changeset
   145
47779
5a10e24fe7ab tuned; don't generate abs code if quotient_type is used
kuncar
parents: 47698
diff changeset
   146
fun setup_by_typedef_thm gen_abs_code typedef_thm lthy =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   147
  let
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   148
    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
   149
    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
   150
    val (T_def, lthy') = define_cr_rel rep_fun lthy
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   151
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   152
    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
   153
      Const ("Orderings.top_class.top", _) => 
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   154
        [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
   155
      | Const (@{const_name "Collect"}, _) $ Abs (_, _, _) => 
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   156
        [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
   157
      | _ => 
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   158
        [typedef_thm, T_def] MRSL @{thm typedef_to_Quotient}
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   159
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   160
    val (_, qty) = Lifting_Term.quot_thm_rty_qty quot_thm
47583
f3f0e06549c2 create thm names correctly
kuncar
parents: 47575
diff changeset
   161
    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
   162
    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
   163
    val simplify = Raw_Simplifier.rewrite_rule [mk_meta_eq @{thm mem_Collect_eq}]
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   164
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   165
    val lthy'' = case typedef_set of
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   166
      Const ("Orderings.top_class.top", _) => 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   167
        let
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   168
          val equivp_thm = typedef_thm RS @{thm UNIV_typedef_to_equivp}
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   169
          val reflp_thm = equivp_thm RS @{thm equivp_reflp2}
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   170
        in
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   171
          lthy'
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   172
            |> (snd oo Local_Theory.note) ((qualify "bi_total", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   173
              [[quot_thm, reflp_thm] MRSL @{thm Quotient_bi_total}])
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   174
            |> (snd oo Local_Theory.note) ((qualify "id_abs_transfer", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   175
              [[quot_thm, reflp_thm] MRSL @{thm Quotient_id_abs_transfer}])
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   176
        end
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   177
      | _ => lthy'
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   178
        |> (snd oo Local_Theory.note) ((qualify "All_transfer", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   179
          [[typedef_thm, T_def] MRSL @{thm typedef_All_transfer}])
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   180
        |> (snd oo Local_Theory.note) ((qualify "Ex_transfer", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   181
          [[typedef_thm, T_def] MRSL @{thm typedef_Ex_transfer}])
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   182
        |> (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
   183
          [simplify ([typedef_thm, T_def] MRSL @{thm typedef_forall_transfer})])
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   184
  in
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   185
    lthy''
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   186
      |> (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
   187
        [[typedef_thm, T_def] MRSL @{thm typedef_bi_unique}])
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   188
      |> (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
   189
        [[typedef_thm, T_def] MRSL @{thm typedef_rep_transfer}])
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   190
      |> (snd oo Local_Theory.note) ((qualify "right_unique", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   191
        [[quot_thm] MRSL @{thm Quotient_right_unique}])
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   192
      |> (snd oo Local_Theory.note) ((qualify "right_total", [transfer_attr]), 
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47535
diff changeset
   193
        [[quot_thm] MRSL @{thm Quotient_right_total}])
47779
5a10e24fe7ab tuned; don't generate abs code if quotient_type is used
kuncar
parents: 47698
diff changeset
   194
      |> setup_lifting_infr gen_abs_code quot_thm
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   195
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   196
47779
5a10e24fe7ab tuned; don't generate abs code if quotient_type is used
kuncar
parents: 47698
diff changeset
   197
fun setup_lifting_cmd gen_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
   198
  let 
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   199
    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
   200
    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
   201
      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
   202
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   203
    fun sanity_check_reflp_thm reflp_thm = 
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   204
      let
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   205
        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
   206
          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
   207
      in
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   208
        case reflp_tm of
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   209
          Const (@{const_name reflp}, _) $ _ => ()
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   210
          | _ => 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
   211
      end
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   212
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   213
    fun setup_quotient () = 
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   214
      case opt_reflp_xthm of
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   215
        SOME reflp_xthm => 
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   216
          let
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   217
            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
   218
            val _ = sanity_check_reflp_thm reflp_thm
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   219
          in
47779
5a10e24fe7ab tuned; don't generate abs code if quotient_type is used
kuncar
parents: 47698
diff changeset
   220
            setup_by_quotient gen_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
   221
          end
47779
5a10e24fe7ab tuned; don't generate abs code if quotient_type is used
kuncar
parents: 47698
diff changeset
   222
        | NONE => setup_by_quotient gen_abs_code input_thm NONE lthy
47566
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   223
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   224
    fun setup_typedef () = 
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   225
      case opt_reflp_xthm of
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   226
        SOME _ => error "The reflexivity theorem cannot be specified if the type_definition theorem is used."
47779
5a10e24fe7ab tuned; don't generate abs code if quotient_type is used
kuncar
parents: 47698
diff changeset
   227
        | NONE => setup_by_typedef_thm gen_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
   228
  in
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47379
diff changeset
   229
    case input_term of
47566
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   230
      (Const (@{const_name Quotient}, _) $ _ $ _ $ _ $ _) => setup_quotient ()
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   231
      | (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
   232
      | _ => 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
   233
  end
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   234
47779
5a10e24fe7ab tuned; don't generate abs code if quotient_type is used
kuncar
parents: 47698
diff changeset
   235
val opt_gen_abs_code =
5a10e24fe7ab tuned; don't generate abs code if quotient_type is used
kuncar
parents: 47698
diff changeset
   236
  Scan.optional (@{keyword "("} |-- Parse.!!! ((Parse.reserved "no_abs_code" >> K false) --| @{keyword ")"})) true
47566
c201a1fe0a81 setup_lifting: no_code switch and supoport for quotient theorems
kuncar
parents: 47545
diff changeset
   237
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   238
val _ = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   239
  Outer_Syntax.local_theory @{command_spec "setup_lifting"}
47352
e0bff2ae939f fix typos
huffman
parents: 47334
diff changeset
   240
    "Setup lifting infrastructure" 
47779
5a10e24fe7ab tuned; don't generate abs code if quotient_type is used
kuncar
parents: 47698
diff changeset
   241
      (opt_gen_abs_code -- Parse_Spec.xthm -- Scan.option Parse_Spec.xthm >> 
5a10e24fe7ab tuned; don't generate abs code if quotient_type is used
kuncar
parents: 47698
diff changeset
   242
        (fn ((gen_abs_code, xthm), opt_reflp_xthm) => setup_lifting_cmd gen_abs_code xthm opt_reflp_xthm))
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   243
end;