src/HOL/Tools/Lifting/lifting_def.ML
author haftmann
Mon, 12 Oct 2020 07:25:38 +0000
changeset 72450 24bd1316eaae
parent 72154 2b41b710f6ef
child 74282 c2ee8d993d6a
permissions -rw-r--r--
consolidated names and operations
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_def.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
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     4
Definitions for constants on quotient types.
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_DEF =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     8
sig
60230
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
     9
  datatype code_eq = UNKNOWN_EQ | NONE_EQ | ABS_EQ | REP_EQ
60219
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    10
  type lift_def
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    11
  val rty_of_lift_def: lift_def -> typ
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    12
  val qty_of_lift_def: lift_def -> typ
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    13
  val rhs_of_lift_def: lift_def -> term
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    14
  val lift_const_of_lift_def: lift_def -> term
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    15
  val def_thm_of_lift_def: lift_def -> thm
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    16
  val rsp_thm_of_lift_def: lift_def -> thm
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    17
  val abs_eq_of_lift_def: lift_def -> thm
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    18
  val rep_eq_of_lift_def: lift_def -> thm option
60227
eacf75e4da95 return also which code equation was used; tuned
kuncar
parents: 60225
diff changeset
    19
  val code_eq_of_lift_def: lift_def -> code_eq
60219
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    20
  val transfer_rules_of_lift_def: lift_def -> thm list
60224
e759afe46a8c export the result of lifting_def
kuncar
parents: 60221
diff changeset
    21
  val morph_lift_def: morphism -> lift_def -> lift_def
e759afe46a8c export the result of lifting_def
kuncar
parents: 60221
diff changeset
    22
  val inst_of_lift_def: Proof.context -> typ -> lift_def -> lift_def
e759afe46a8c export the result of lifting_def
kuncar
parents: 60221
diff changeset
    23
  val mk_lift_const_of_lift_def: typ -> lift_def -> term
60219
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    24
60225
eb4e322734bf note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents: 60224
diff changeset
    25
  type config = { notes: bool }
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents: 60227
diff changeset
    26
  val map_config: (bool -> bool) -> config -> config
60225
eb4e322734bf note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents: 60224
diff changeset
    27
  val default_config: config
eb4e322734bf note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents: 60224
diff changeset
    28
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    29
  val generate_parametric_transfer_rule:
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    30
    Proof.context -> thm -> thm -> thm
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    31
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
    32
  val add_lift_def:
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
    33
    config -> binding * mixfix -> typ -> term -> thm -> thm list -> local_theory ->
60225
eb4e322734bf note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents: 60224
diff changeset
    34
      lift_def * local_theory
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
    35
60227
eacf75e4da95 return also which code equation was used; tuned
kuncar
parents: 60225
diff changeset
    36
  val prepare_lift_def:
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
    37
    (binding * mixfix -> typ -> term -> thm -> thm list -> Proof.context ->
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
    38
      lift_def * local_theory) ->
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
    39
    binding * mixfix -> typ -> term -> thm list -> local_theory ->
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
    40
    term option * (thm -> Proof.context -> lift_def * local_theory)
60227
eacf75e4da95 return also which code equation was used; tuned
kuncar
parents: 60225
diff changeset
    41
eacf75e4da95 return also which code equation was used; tuned
kuncar
parents: 60225
diff changeset
    42
  val gen_lift_def:
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
    43
    (binding * mixfix -> typ -> term -> thm -> thm list -> local_theory ->
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
    44
      lift_def * local_theory) ->
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
    45
    binding * mixfix -> typ -> term -> (Proof.context -> tactic) -> thm list ->
60227
eacf75e4da95 return also which code equation was used; tuned
kuncar
parents: 60225
diff changeset
    46
    local_theory -> lift_def * local_theory
eacf75e4da95 return also which code equation was used; tuned
kuncar
parents: 60225
diff changeset
    47
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
    48
  val lift_def:
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
    49
    config -> binding * mixfix -> typ -> term -> (Proof.context -> tactic) -> thm list ->
60219
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    50
    local_theory -> lift_def * local_theory
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    51
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    52
  val can_generate_code_cert: thm -> bool
53651
ee90c67502c9 restoring Transfer/Lifting context
kuncar
parents: 53207
diff changeset
    53
end
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    54
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    55
structure Lifting_Def: LIFTING_DEF =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    56
struct
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    57
47698
18202d3d5832 move MRSL to a separate file
kuncar
parents: 47675
diff changeset
    58
open Lifting_Util
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    59
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    60
infix 0 MRSL
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    61
60230
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
    62
datatype code_eq = UNKNOWN_EQ | NONE_EQ | ABS_EQ | REP_EQ
60227
eacf75e4da95 return also which code equation was used; tuned
kuncar
parents: 60225
diff changeset
    63
60219
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    64
datatype lift_def = LIFT_DEF of {
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    65
  rty: typ,
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    66
  qty: typ,
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    67
  rhs: term,
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    68
  lift_const: term,
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    69
  def_thm: thm,
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    70
  rsp_thm: thm,
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    71
  abs_eq: thm,
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    72
  rep_eq: thm option,
60227
eacf75e4da95 return also which code equation was used; tuned
kuncar
parents: 60225
diff changeset
    73
  code_eq: code_eq,
60219
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    74
  transfer_rules: thm list
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    75
};
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    76
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    77
fun rep_lift_def (LIFT_DEF lift_def) = lift_def;
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    78
val rty_of_lift_def = #rty o rep_lift_def;
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    79
val qty_of_lift_def = #qty o rep_lift_def;
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    80
val rhs_of_lift_def = #rhs o rep_lift_def;
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    81
val lift_const_of_lift_def = #lift_const o rep_lift_def;
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    82
val def_thm_of_lift_def = #def_thm o rep_lift_def;
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    83
val rsp_thm_of_lift_def = #rsp_thm o rep_lift_def;
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    84
val abs_eq_of_lift_def = #abs_eq o rep_lift_def;
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    85
val rep_eq_of_lift_def = #rep_eq o rep_lift_def;
60227
eacf75e4da95 return also which code equation was used; tuned
kuncar
parents: 60225
diff changeset
    86
val code_eq_of_lift_def = #code_eq o rep_lift_def;
60219
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    87
val transfer_rules_of_lift_def = #transfer_rules o rep_lift_def;
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    88
60227
eacf75e4da95 return also which code equation was used; tuned
kuncar
parents: 60225
diff changeset
    89
fun mk_lift_def rty qty rhs lift_const def_thm rsp_thm abs_eq rep_eq code_eq transfer_rules =
60219
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    90
  LIFT_DEF {rty = rty, qty = qty,
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    91
            rhs = rhs, lift_const = lift_const,
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
    92
            def_thm = def_thm, rsp_thm = rsp_thm, abs_eq = abs_eq, rep_eq = rep_eq,
60227
eacf75e4da95 return also which code equation was used; tuned
kuncar
parents: 60225
diff changeset
    93
            code_eq = code_eq, transfer_rules = transfer_rules };
60219
2bce9a690b1e lift_definition: return the result of lifting
kuncar
parents: 60218
diff changeset
    94
60227
eacf75e4da95 return also which code equation was used; tuned
kuncar
parents: 60225
diff changeset
    95
fun map_lift_def f1 f2 f3 f4 f5 f6 f7 f8 f9 f10
60224
e759afe46a8c export the result of lifting_def
kuncar
parents: 60221
diff changeset
    96
  (LIFT_DEF {rty = rty, qty = qty, rhs = rhs, lift_const = lift_const,
60227
eacf75e4da95 return also which code equation was used; tuned
kuncar
parents: 60225
diff changeset
    97
  def_thm = def_thm, rsp_thm = rsp_thm, abs_eq = abs_eq, rep_eq = rep_eq, code_eq = code_eq,
60224
e759afe46a8c export the result of lifting_def
kuncar
parents: 60221
diff changeset
    98
  transfer_rules = transfer_rules }) =
e759afe46a8c export the result of lifting_def
kuncar
parents: 60221
diff changeset
    99
  LIFT_DEF {rty = f1 rty, qty = f2 qty, rhs = f3 rhs, lift_const = f4 lift_const,
e759afe46a8c export the result of lifting_def
kuncar
parents: 60221
diff changeset
   100
            def_thm = f5 def_thm, rsp_thm = f6 rsp_thm, abs_eq = f7 abs_eq, rep_eq = f8 rep_eq,
60227
eacf75e4da95 return also which code equation was used; tuned
kuncar
parents: 60225
diff changeset
   101
            code_eq = f9 code_eq, transfer_rules = f10 transfer_rules }
60224
e759afe46a8c export the result of lifting_def
kuncar
parents: 60221
diff changeset
   102
e759afe46a8c export the result of lifting_def
kuncar
parents: 60221
diff changeset
   103
fun morph_lift_def phi =
e759afe46a8c export the result of lifting_def
kuncar
parents: 60221
diff changeset
   104
  let
e759afe46a8c export the result of lifting_def
kuncar
parents: 60221
diff changeset
   105
    val mtyp = Morphism.typ phi
e759afe46a8c export the result of lifting_def
kuncar
parents: 60221
diff changeset
   106
    val mterm = Morphism.term phi
e759afe46a8c export the result of lifting_def
kuncar
parents: 60221
diff changeset
   107
    val mthm = Morphism.thm phi
e759afe46a8c export the result of lifting_def
kuncar
parents: 60221
diff changeset
   108
  in
60227
eacf75e4da95 return also which code equation was used; tuned
kuncar
parents: 60225
diff changeset
   109
    map_lift_def mtyp mtyp mterm mterm mthm mthm mthm (Option.map mthm) I (map mthm)
60224
e759afe46a8c export the result of lifting_def
kuncar
parents: 60221
diff changeset
   110
  end
e759afe46a8c export the result of lifting_def
kuncar
parents: 60221
diff changeset
   111
e759afe46a8c export the result of lifting_def
kuncar
parents: 60221
diff changeset
   112
fun mk_inst_of_lift_def qty lift_def = Vartab.empty |> Type.raw_match (qty_of_lift_def lift_def, qty)
e759afe46a8c export the result of lifting_def
kuncar
parents: 60221
diff changeset
   113
e759afe46a8c export the result of lifting_def
kuncar
parents: 60221
diff changeset
   114
fun mk_lift_const_of_lift_def qty lift_def = Envir.subst_term_types (mk_inst_of_lift_def qty lift_def)
e759afe46a8c export the result of lifting_def
kuncar
parents: 60221
diff changeset
   115
  (lift_const_of_lift_def lift_def)
e759afe46a8c export the result of lifting_def
kuncar
parents: 60221
diff changeset
   116
67709
3c9e0b4709e7 tuned -- use existing Morphism.instantiate_morphism;
wenzelm
parents: 67703
diff changeset
   117
fun inst_of_lift_def ctxt qty lift_def =
3c9e0b4709e7 tuned -- use existing Morphism.instantiate_morphism;
wenzelm
parents: 67703
diff changeset
   118
  let
3c9e0b4709e7 tuned -- use existing Morphism.instantiate_morphism;
wenzelm
parents: 67703
diff changeset
   119
    val instT =
3c9e0b4709e7 tuned -- use existing Morphism.instantiate_morphism;
wenzelm
parents: 67703
diff changeset
   120
      Vartab.fold (fn (a, (S, T)) => cons ((a, S), Thm.ctyp_of ctxt T))
3c9e0b4709e7 tuned -- use existing Morphism.instantiate_morphism;
wenzelm
parents: 67703
diff changeset
   121
        (mk_inst_of_lift_def qty lift_def) []
3c9e0b4709e7 tuned -- use existing Morphism.instantiate_morphism;
wenzelm
parents: 67703
diff changeset
   122
    val phi = Morphism.instantiate_morphism (instT, [])
3c9e0b4709e7 tuned -- use existing Morphism.instantiate_morphism;
wenzelm
parents: 67703
diff changeset
   123
  in morph_lift_def phi lift_def end
3c9e0b4709e7 tuned -- use existing Morphism.instantiate_morphism;
wenzelm
parents: 67703
diff changeset
   124
60224
e759afe46a8c export the result of lifting_def
kuncar
parents: 60221
diff changeset
   125
60225
eb4e322734bf note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents: 60224
diff changeset
   126
(* Config *)
eb4e322734bf note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents: 60224
diff changeset
   127
eb4e322734bf note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents: 60224
diff changeset
   128
type config = { notes: bool };
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents: 60227
diff changeset
   129
fun map_config f1 { notes = notes } = { notes = f1 notes }
60225
eb4e322734bf note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents: 60224
diff changeset
   130
val default_config = { notes = true };
eb4e322734bf note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents: 60224
diff changeset
   131
51994
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   132
(* Reflexivity prover *)
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   133
55609
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
   134
fun mono_eq_prover ctxt prop =
51994
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   135
  let
56518
beb3b6851665 left_total and left_unique rules are now transfer rules (cleaner solution, reflexvity_rule attribute not needed anymore)
kuncar
parents: 56245
diff changeset
   136
    val refl_rules = Lifting_Info.get_reflexivity_rules ctxt
beb3b6851665 left_total and left_unique rules are now transfer rules (cleaner solution, reflexvity_rule attribute not needed anymore)
kuncar
parents: 56245
diff changeset
   137
    val transfer_rules = Transfer.get_transfer_raw ctxt
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   138
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   139
    fun main_tac i = (REPEAT_ALL_NEW (DETERM o resolve_tac ctxt refl_rules) THEN_ALL_NEW
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59458
diff changeset
   140
      (REPEAT_ALL_NEW (DETERM o resolve_tac ctxt transfer_rules))) i
51994
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   141
  in
56540
8267d1ff646f fix the reflexivity prover
kuncar
parents: 56524
diff changeset
   142
    SOME (Goal.prove ctxt [] [] prop (K (main_tac 1)))
55609
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
   143
      handle ERROR _ => NONE
51994
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   144
  end
56518
beb3b6851665 left_total and left_unique rules are now transfer rules (cleaner solution, reflexvity_rule attribute not needed anymore)
kuncar
parents: 56245
diff changeset
   145
57642
5bc43a73d768 prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents: 56540
diff changeset
   146
fun try_prove_refl_rel ctxt rel =
5bc43a73d768 prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents: 56540
diff changeset
   147
  let
5bc43a73d768 prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents: 56540
diff changeset
   148
    fun mk_ge_eq x =
5bc43a73d768 prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents: 56540
diff changeset
   149
      let
5bc43a73d768 prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents: 56540
diff changeset
   150
        val T = fastype_of x
5bc43a73d768 prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents: 56540
diff changeset
   151
      in
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67710
diff changeset
   152
        Const (\<^const_name>\<open>less_eq\<close>, T --> T --> HOLogic.boolT) $
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67710
diff changeset
   153
          (Const (\<^const_name>\<open>HOL.eq\<close>, T)) $ x
57642
5bc43a73d768 prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents: 56540
diff changeset
   154
      end;
5bc43a73d768 prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents: 56540
diff changeset
   155
    val goal = HOLogic.mk_Trueprop (mk_ge_eq rel);
5bc43a73d768 prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents: 56540
diff changeset
   156
  in
5bc43a73d768 prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents: 56540
diff changeset
   157
     mono_eq_prover ctxt goal
5bc43a73d768 prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents: 56540
diff changeset
   158
  end;
5bc43a73d768 prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents: 56540
diff changeset
   159
51994
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   160
fun try_prove_reflexivity ctxt prop =
55609
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
   161
  let
59630
c9aa1c90796f clarified context;
wenzelm
parents: 59621
diff changeset
   162
    val cprop = Thm.cterm_of ctxt prop
55609
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
   163
    val rule = @{thm ge_eq_refl}
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   164
    val concl_pat = Drule.strip_imp_concl (Thm.cprop_of rule)
55609
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
   165
    val insts = Thm.first_order_match (concl_pat, cprop)
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
   166
    val rule = Drule.instantiate_normalize insts rule
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   167
    val prop = hd (Thm.prems_of rule)
55609
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
   168
  in
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
   169
    case mono_eq_prover ctxt prop of
55610
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   170
      SOME thm => SOME (thm RS rule)
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   171
    | NONE => NONE
55609
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
   172
  end
51994
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   173
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   174
(*
54335
03b10317ba78 update documentation of important public ML functions in Lifting
kuncar
parents: 53951
diff changeset
   175
  Generates a parametrized transfer rule.
03b10317ba78 update documentation of important public ML functions in Lifting
kuncar
parents: 53951
diff changeset
   176
  transfer_rule - of the form T t f
03b10317ba78 update documentation of important public ML functions in Lifting
kuncar
parents: 53951
diff changeset
   177
  parametric_transfer_rule - of the form par_R t' t
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   178
54947
kuncar
parents: 54742
diff changeset
   179
  Result: par_T t' f, after substituing op= for relations in par_R that relate
54335
03b10317ba78 update documentation of important public ML functions in Lifting
kuncar
parents: 53951
diff changeset
   180
    a type constructor to the same type constructor, it is a merge of (par_R' OO T) t' f
03b10317ba78 update documentation of important public ML functions in Lifting
kuncar
parents: 53951
diff changeset
   181
    using Lifting_Term.merge_transfer_relations
03b10317ba78 update documentation of important public ML functions in Lifting
kuncar
parents: 53951
diff changeset
   182
*)
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   183
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   184
fun generate_parametric_transfer_rule ctxt0 transfer_rule parametric_transfer_rule =
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   185
  let
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   186
    fun preprocess ctxt thm =
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   187
      let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   188
        val tm = (strip_args 2 o HOLogic.dest_Trueprop o Thm.concl_of) thm;
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   189
        val param_rel = (snd o dest_comb o fst o dest_comb) tm;
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   190
        val free_vars = Term.add_vars param_rel [];
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   191
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   192
        fun make_subst (xi, typ) subst =
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   193
          let
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   194
            val [rty, rty'] = binder_types typ
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   195
          in
67703
8c4806fe827f tuned signature -- eliminated clones;
wenzelm
parents: 66251
diff changeset
   196
            if Term.is_TVar rty andalso Term.is_Type rty' then
60784
4f590c08fd5d updated to infer_instantiate;
wenzelm
parents: 60239
diff changeset
   197
              (xi, Thm.cterm_of ctxt (HOLogic.eq_const rty')) :: subst
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   198
            else
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   199
              subst
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   200
          end;
60784
4f590c08fd5d updated to infer_instantiate;
wenzelm
parents: 60239
diff changeset
   201
4f590c08fd5d updated to infer_instantiate;
wenzelm
parents: 60239
diff changeset
   202
        val inst_thm = infer_instantiate ctxt (fold make_subst free_vars []) thm;
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   203
      in
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   204
        Conv.fconv_rule
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   205
          ((Conv.concl_conv (Thm.nprems_of inst_thm) o
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   206
            HOLogic.Trueprop_conv o Conv.fun2_conv o Conv.arg1_conv)
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54335
diff changeset
   207
            (Raw_Simplifier.rewrite ctxt false (Transfer.get_sym_relator_eq ctxt))) inst_thm
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   208
      end
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   209
59630
c9aa1c90796f clarified context;
wenzelm
parents: 59621
diff changeset
   210
    fun inst_relcomppI ctxt ant1 ant2 =
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   211
      let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   212
        val t1 = (HOLogic.dest_Trueprop o Thm.concl_of) ant1
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   213
        val t2 = (HOLogic.dest_Trueprop o Thm.prop_of) ant2
59630
c9aa1c90796f clarified context;
wenzelm
parents: 59621
diff changeset
   214
        val fun1 = Thm.cterm_of ctxt (strip_args 2 t1)
c9aa1c90796f clarified context;
wenzelm
parents: 59621
diff changeset
   215
        val args1 = map (Thm.cterm_of ctxt) (get_args 2 t1)
c9aa1c90796f clarified context;
wenzelm
parents: 59621
diff changeset
   216
        val fun2 = Thm.cterm_of ctxt (strip_args 2 t2)
c9aa1c90796f clarified context;
wenzelm
parents: 59621
diff changeset
   217
        val args2 = map (Thm.cterm_of ctxt) (get_args 1 t2)
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   218
        val relcomppI = Drule.incr_indexes2 ant1 ant2 @{thm relcomppI}
60784
4f590c08fd5d updated to infer_instantiate;
wenzelm
parents: 60239
diff changeset
   219
        val vars = map #1 (rev (Term.add_vars (Thm.prop_of relcomppI) []))
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   220
      in
60784
4f590c08fd5d updated to infer_instantiate;
wenzelm
parents: 60239
diff changeset
   221
        infer_instantiate ctxt (vars ~~ ([fun1] @ args1 @ [fun2] @ args2)) relcomppI
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   222
      end
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   223
54995
68228c162ed5 more accurate context;
wenzelm
parents: 54947
diff changeset
   224
    fun zip_transfer_rules ctxt thm =
68228c162ed5 more accurate context;
wenzelm
parents: 54947
diff changeset
   225
      let
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67710
diff changeset
   226
        fun mk_POS ty = Const (\<^const_name>\<open>POS\<close>, ty --> ty --> HOLogic.boolT)
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   227
        val rel = (Thm.dest_fun2 o Thm.dest_arg o Thm.cprop_of) thm
59586
ddf6deaadfe8 clarified signature;
wenzelm
parents: 59582
diff changeset
   228
        val typ = Thm.typ_of_cterm rel
59630
c9aa1c90796f clarified context;
wenzelm
parents: 59621
diff changeset
   229
        val POS_const = Thm.cterm_of ctxt (mk_POS typ)
c9aa1c90796f clarified context;
wenzelm
parents: 59621
diff changeset
   230
        val var = Thm.cterm_of ctxt (Var (("X", Thm.maxidx_of_cterm rel + 1), typ))
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   231
        val goal =
59630
c9aa1c90796f clarified context;
wenzelm
parents: 59621
diff changeset
   232
          Thm.apply (Thm.cterm_of ctxt HOLogic.Trueprop) (Thm.apply (Thm.apply POS_const rel) var)
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   233
      in
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   234
        [Lifting_Term.merge_transfer_relations ctxt goal, thm] MRSL @{thm POS_apply}
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   235
      end
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   236
59630
c9aa1c90796f clarified context;
wenzelm
parents: 59621
diff changeset
   237
    val thm =
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   238
      inst_relcomppI ctxt0 parametric_transfer_rule transfer_rule
59630
c9aa1c90796f clarified context;
wenzelm
parents: 59621
diff changeset
   239
        OF [parametric_transfer_rule, transfer_rule]
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   240
    val preprocessed_thm = preprocess ctxt0 thm
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   241
    val (fixed_thm, ctxt1) = ctxt0
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   242
      |> yield_singleton (apfst snd oo Variable.import true) preprocessed_thm
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   243
    val assms = cprems_of fixed_thm
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   244
    val add_transfer_rule = Thm.attribute_declaration Transfer.transfer_add
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   245
    val (prems, ctxt2) = ctxt1 |> fold_map Thm.assume_hyps assms
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   246
    val ctxt3 =  ctxt2 |> Context.proof_map (fold add_transfer_rule prems)
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   247
    val zipped_thm =
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   248
      fixed_thm
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   249
      |> undisch_all
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   250
      |> zip_transfer_rules ctxt3
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   251
      |> implies_intr_list assms
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   252
      |> singleton (Variable.export ctxt3 ctxt0)
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   253
  in
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   254
    zipped_thm
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   255
  end
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   256
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   257
fun print_generate_transfer_info msg =
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   258
  let
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   259
    val error_msg = cat_lines
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   260
      ["Generation of a parametric transfer rule failed.",
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   261
      (Pretty.string_of (Pretty.block
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   262
         [Pretty.str "Reason:", Pretty.brk 2, msg]))]
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   263
  in
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   264
    error error_msg
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   265
  end
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   266
53951
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
   267
fun map_ter _ x [] = x
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
   268
    | map_ter f _ xs = map f xs
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
   269
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
   270
fun generate_transfer_rules lthy quot_thm rsp_thm def_thm par_thms =
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   271
  let
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   272
    val transfer_rule =
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   273
      ([quot_thm, rsp_thm, def_thm] MRSL @{thm Quotient_to_transfer})
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   274
      |> Lifting_Term.parametrize_transfer_rule lthy
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   275
  in
53951
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
   276
    (map_ter (generate_parametric_transfer_rule lthy transfer_rule) [transfer_rule] par_thms
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
   277
    handle Lifting_Term.MERGE_TRANSFER_REL msg => (print_generate_transfer_info msg; [transfer_rule]))
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   278
  end
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   279
47698
18202d3d5832 move MRSL to a separate file
kuncar
parents: 47675
diff changeset
   280
(* Generation of the code certificate from the rsp theorem *)
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   281
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   282
fun get_body_types (Type ("fun", [_, U]), Type ("fun", [_, V])) = get_body_types (U, V)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   283
  | get_body_types (U, V)  = (U, V)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   284
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   285
fun get_binder_types (Type ("fun", [T, U]), Type ("fun", [V, W])) = (T, V) :: get_binder_types (U, W)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   286
  | get_binder_types _ = []
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   287
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67710
diff changeset
   288
fun get_binder_types_by_rel (Const (\<^const_name>\<open>rel_fun\<close>, _) $ _ $ S) (Type ("fun", [T, U]), Type ("fun", [V, W])) =
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: 47852
diff changeset
   289
    (T, V) :: get_binder_types_by_rel S (U, W)
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: 47852
diff changeset
   290
  | get_binder_types_by_rel _ _ = []
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: 47852
diff changeset
   291
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67710
diff changeset
   292
fun get_body_type_by_rel (Const (\<^const_name>\<open>rel_fun\<close>, _) $ _ $ S) (Type ("fun", [_, U]), Type ("fun", [_, V])) =
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: 47852
diff changeset
   293
    get_body_type_by_rel S (U, V)
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: 47852
diff changeset
   294
  | get_body_type_by_rel _ (U, V)  = (U, V)
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: 47852
diff changeset
   295
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67710
diff changeset
   296
fun get_binder_rels (Const (\<^const_name>\<open>rel_fun\<close>, _) $ R $ S) = R :: get_binder_rels S
57642
5bc43a73d768 prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents: 56540
diff changeset
   297
  | get_binder_rels _ = []
5bc43a73d768 prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents: 56540
diff changeset
   298
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   299
fun force_rty_type ctxt rty rhs =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   300
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   301
    val thy = Proof_Context.theory_of ctxt
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   302
    val rhs_schematic = singleton (Variable.polymorphic ctxt) rhs
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   303
    val rty_schematic = fastype_of rhs_schematic
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   304
    val match = Sign.typ_match thy (rty_schematic, rty) Vartab.empty
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   305
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   306
    Envir.subst_term_types match rhs_schematic
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   307
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   308
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   309
fun unabs_def ctxt def =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   310
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   311
    val (_, rhs) = Thm.dest_equals (Thm.cprop_of def)
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   312
    fun dest_abs (Abs (var_name, T, _)) = (var_name, T)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   313
      | dest_abs tm = raise TERM("get_abs_var",[tm])
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   314
    val (var_name, T) = dest_abs (Thm.term_of rhs)
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   315
    val (new_var_names, ctxt') = Variable.variant_fixes [var_name] ctxt
59630
c9aa1c90796f clarified context;
wenzelm
parents: 59621
diff changeset
   316
    val refl_thm = Thm.reflexive (Thm.cterm_of ctxt' (Free (hd new_var_names, T)))
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   317
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   318
    Thm.combination def refl_thm |>
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   319
    singleton (Proof_Context.export ctxt' ctxt)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   320
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   321
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   322
fun unabs_all_def ctxt def =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   323
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   324
    val (_, rhs) = Thm.dest_equals (Thm.cprop_of def)
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   325
    val xs = strip_abs_vars (Thm.term_of rhs)
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   326
  in
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   327
    fold (K (unabs_def ctxt)) xs def
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   328
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   329
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   330
val map_fun_unfolded =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   331
  @{thm map_fun_def[abs_def]} |>
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67710
diff changeset
   332
  unabs_def \<^context> |>
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67710
diff changeset
   333
  unabs_def \<^context> |>
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67710
diff changeset
   334
  Local_Defs.unfold0 \<^context> [@{thm comp_def}]
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   335
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   336
fun unfold_fun_maps ctm =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   337
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   338
    fun unfold_conv ctm =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   339
      case (Thm.term_of ctm) of
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67710
diff changeset
   340
        Const (\<^const_name>\<open>map_fun\<close>, _) $ _ $ _ =>
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   341
          (Conv.arg_conv unfold_conv then_conv Conv.rewr_conv map_fun_unfolded) ctm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   342
        | _ => Conv.all_conv ctm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   343
  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: 47852
diff changeset
   344
    (Conv.fun_conv unfold_conv) ctm
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: 47852
diff changeset
   345
  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: 47852
diff changeset
   346
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: 47852
diff changeset
   347
fun unfold_fun_maps_beta ctm =
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: 47852
diff changeset
   348
  let val try_beta_conv = Conv.try_conv (Thm.beta_conversion false)
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   349
  in
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   350
    (unfold_fun_maps then_conv try_beta_conv) ctm
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   351
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   352
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   353
fun prove_rel ctxt rsp_thm (rty, qty) =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   354
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   355
    val ty_args = get_binder_types (rty, qty)
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   356
    fun disch_arg args_ty thm =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   357
      let
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47503
diff changeset
   358
        val quot_thm = Lifting_Term.prove_quot_thm ctxt args_ty
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   359
      in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   360
        [quot_thm, thm] MRSL @{thm apply_rsp''}
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   361
      end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   362
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   363
    fold disch_arg ty_args rsp_thm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   364
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   365
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   366
exception CODE_CERT_GEN of string
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   367
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   368
fun simplify_code_eq ctxt def_thm =
63170
eae6549dbea2 tuned proofs, to allow unfold_abs_def;
wenzelm
parents: 63003
diff changeset
   369
  Local_Defs.unfold0 ctxt [@{thm o_apply}, @{thm map_fun_def}, @{thm id_apply}] def_thm
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   370
47852
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   371
(*
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   372
  quot_thm - quotient theorem (Quotient R Abs Rep T).
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   373
  returns: whether the Lifting package is capable to generate code for the abstract type
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   374
    represented by quot_thm
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   375
*)
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   376
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   377
fun can_generate_code_cert quot_thm  =
47951
8c8a03765de7 quot_del attribute, it allows us to deregister quotient types
kuncar
parents: 47937
diff changeset
   378
  case quot_thm_rel quot_thm of
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67710
diff changeset
   379
    Const (\<^const_name>\<open>HOL.eq\<close>, _) => true
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67710
diff changeset
   380
    | Const (\<^const_name>\<open>eq_onp\<close>, _) $ _  => true
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   381
    | _ => false
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   382
55610
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   383
fun generate_rep_eq ctxt def_thm rsp_thm (rty, qty) =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   384
  let
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: 47852
diff changeset
   385
    val unfolded_def = Conv.fconv_rule (Conv.arg_conv unfold_fun_maps_beta) def_thm
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   386
    val unabs_def = unabs_all_def ctxt unfolded_def
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   387
  in
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   388
    if body_type rty = body_type qty then
67710
cc2db3239932 added HOLogic.mk_obj_eq convenience and eliminated some clones;
wenzelm
parents: 67709
diff changeset
   389
      SOME (simplify_code_eq ctxt (HOLogic.mk_obj_eq unabs_def))
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   390
    else
55610
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   391
      let
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   392
        val quot_thm = Lifting_Term.prove_quot_thm ctxt (get_body_types (rty, qty))
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55773
diff changeset
   393
        val rel_fun = prove_rel ctxt rsp_thm (rty, qty)
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55773
diff changeset
   394
        val rep_abs_thm = [quot_thm, rel_fun] MRSL @{thm Quotient_rep_abs_eq}
55610
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   395
      in
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   396
        case mono_eq_prover ctxt (hd (Thm.prems_of rep_abs_thm)) of
55610
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   397
          SOME mono_eq_thm =>
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   398
            let
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   399
              val rep_abs_eq = mono_eq_thm RS rep_abs_thm
59630
c9aa1c90796f clarified context;
wenzelm
parents: 59621
diff changeset
   400
              val rep = Thm.cterm_of ctxt (quot_thm_rep quot_thm)
67710
cc2db3239932 added HOLogic.mk_obj_eq convenience and eliminated some clones;
wenzelm
parents: 67709
diff changeset
   401
              val rep_refl = HOLogic.mk_obj_eq (Thm.reflexive rep)
cc2db3239932 added HOLogic.mk_obj_eq convenience and eliminated some clones;
wenzelm
parents: 67709
diff changeset
   402
              val repped_eq = [rep_refl, HOLogic.mk_obj_eq unabs_def] MRSL @{thm cong}
55610
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   403
              val code_cert = [repped_eq, rep_abs_eq] MRSL trans
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   404
            in
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   405
              SOME (simplify_code_eq ctxt code_cert)
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   406
            end
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   407
          | NONE => NONE
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   408
      end
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   409
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   410
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: 47852
diff changeset
   411
fun generate_abs_eq ctxt def_thm rsp_thm quot_thm =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   412
  let
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: 47852
diff changeset
   413
    val abs_eq_with_assms =
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: 47852
diff changeset
   414
      let
47951
8c8a03765de7 quot_del attribute, it allows us to deregister quotient types
kuncar
parents: 47937
diff changeset
   415
        val (rty, qty) = quot_thm_rty_qty quot_thm
8c8a03765de7 quot_del attribute, it allows us to deregister quotient types
kuncar
parents: 47937
diff changeset
   416
        val rel = quot_thm_rel 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: 47852
diff changeset
   417
        val ty_args = get_binder_types_by_rel rel (rty, qty)
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: 47852
diff changeset
   418
        val body_type = get_body_type_by_rel rel (rty, qty)
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: 47852
diff changeset
   419
        val quot_ret_thm = Lifting_Term.prove_quot_thm ctxt body_type
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   420
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   421
        val rep_abs_folded_unmapped_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: 47852
diff changeset
   422
          let
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: 47852
diff changeset
   423
            val rep_id = [quot_thm, def_thm] MRSL @{thm Quotient_Rep_eq}
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   424
            val ctm = Thm.dest_equals_lhs (Thm.cprop_of rep_id)
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: 47852
diff changeset
   425
            val unfolded_maps_eq = unfold_fun_maps ctm
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: 47852
diff changeset
   426
            val t1 = [quot_thm, def_thm, rsp_thm] MRSL @{thm Quotient_rep_abs_fold_unmap}
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: 47852
diff changeset
   427
            val prems_pat = (hd o Drule.cprems_of) t1
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   428
            val insts = Thm.first_order_match (prems_pat, Thm.cprop_of unfolded_maps_eq)
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: 47852
diff changeset
   429
          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: 47852
diff changeset
   430
            unfolded_maps_eq RS (Drule.instantiate_normalize insts t1)
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: 47852
diff changeset
   431
          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: 47852
diff changeset
   432
      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: 47852
diff changeset
   433
        rep_abs_folded_unmapped_thm
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55773
diff changeset
   434
        |> fold (fn _ => fn thm => thm RS @{thm rel_funD2}) ty_args
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: 47852
diff changeset
   435
        |> (fn x => x RS (@{thm Quotient_rel_abs2} OF [quot_ret_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: 47852
diff changeset
   436
      end
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   437
57642
5bc43a73d768 prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents: 56540
diff changeset
   438
    val prem_rels = get_binder_rels (quot_thm_rel quot_thm);
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   439
    val proved_assms = prem_rels |> map (try_prove_refl_rel ctxt)
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   440
      |> map_index (apfst (fn x => x + 1)) |> filter (is_some o snd) |> map (apsnd the)
57642
5bc43a73d768 prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents: 56540
diff changeset
   441
      |> map (apsnd (fn assm => assm RS @{thm ge_eq_refl}))
5bc43a73d768 prevent beta-contraction in proving extra assumptions for abs_eq
kuncar
parents: 56540
diff changeset
   442
    val abs_eq = fold_rev (fn (i, assm) => fn thm => assm RSN (i, thm)) proved_assms abs_eq_with_assms
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   443
  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: 47852
diff changeset
   444
    simplify_code_eq ctxt abs_eq
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: 47852
diff changeset
   445
  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: 47852
diff changeset
   446
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: 47852
diff changeset
   447
55724
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   448
fun register_code_eq_thy abs_eq_thm opt_rep_eq_thm (rty, qty) thy =
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: 47852
diff changeset
   449
  let
55724
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   450
    fun no_abstr (t $ u) = no_abstr t andalso no_abstr u
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   451
      | no_abstr (Abs (_, _, t)) = no_abstr t
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   452
      | no_abstr (Const (name, _)) = not (Code.is_abstr thy name)
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   453
      | no_abstr _ = true
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   454
    fun is_valid_eq eqn = can (Code.assert_eqn thy) (mk_meta_eq eqn, true)
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   455
      andalso no_abstr (Thm.prop_of eqn)
55724
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   456
    fun is_valid_abs_eq abs_eq = can (Code.assert_abs_eqn thy NONE) (mk_meta_eq abs_eq)
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   457
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   458
  in
55724
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   459
    if is_valid_eq abs_eq_thm then
66251
cd935b7cb3fb proper concept of code declaration wrt. atomicity and Isar declarations
haftmann
parents: 66017
diff changeset
   460
      (ABS_EQ, Code.declare_default_eqns_global [(abs_eq_thm, true)] thy)
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: 47852
diff changeset
   461
    else
55724
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   462
      let
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   463
        val (rty_body, qty_body) = get_body_types (rty, qty)
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: 47852
diff changeset
   464
      in
55724
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   465
        if rty_body = qty_body then
66251
cd935b7cb3fb proper concept of code declaration wrt. atomicity and Isar declarations
haftmann
parents: 66017
diff changeset
   466
          (REP_EQ, Code.declare_default_eqns_global [(the opt_rep_eq_thm, true)] thy)
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: 47852
diff changeset
   467
        else
55724
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   468
          if is_some opt_rep_eq_thm andalso is_valid_abs_eq (the opt_rep_eq_thm)
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   469
          then
66251
cd935b7cb3fb proper concept of code declaration wrt. atomicity and Isar declarations
haftmann
parents: 66017
diff changeset
   470
            (REP_EQ, Code.declare_abstract_eqn_global (the opt_rep_eq_thm) thy)
55724
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   471
          else
60227
eacf75e4da95 return also which code equation was used; tuned
kuncar
parents: 60225
diff changeset
   472
            (NONE_EQ, thy)
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: 47852
diff changeset
   473
      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: 47852
diff changeset
   474
  end
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   475
55724
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   476
local
57663
b590fcd03a4a store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents: 57642
diff changeset
   477
  fun no_no_code ctxt (rty, qty) =
b590fcd03a4a store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents: 57642
diff changeset
   478
    if same_type_constrs (rty, qty) then
b590fcd03a4a store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents: 57642
diff changeset
   479
      forall (no_no_code ctxt) (Targs rty ~~ Targs qty)
b590fcd03a4a store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents: 57642
diff changeset
   480
    else
67703
8c4806fe827f tuned signature -- eliminated clones;
wenzelm
parents: 66251
diff changeset
   481
      if Term.is_Type qty then
57663
b590fcd03a4a store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents: 57642
diff changeset
   482
        if Lifting_Info.is_no_code_type ctxt (Tname qty) then false
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   483
        else
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   484
          let
57663
b590fcd03a4a store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents: 57642
diff changeset
   485
            val (rty', rtyq) = Lifting_Term.instantiate_rtys ctxt (rty, qty)
b590fcd03a4a store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents: 57642
diff changeset
   486
            val (rty's, rtyqs) = (Targs rty', Targs rtyq)
b590fcd03a4a store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents: 57642
diff changeset
   487
          in
b590fcd03a4a store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents: 57642
diff changeset
   488
            forall (no_no_code ctxt) (rty's ~~ rtyqs)
b590fcd03a4a store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents: 57642
diff changeset
   489
          end
b590fcd03a4a store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents: 57642
diff changeset
   490
      else
b590fcd03a4a store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents: 57642
diff changeset
   491
        true
60230
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   492
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   493
  fun encode_code_eq ctxt abs_eq opt_rep_eq (rty, qty) =
60230
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   494
    let
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   495
      fun mk_type typ = typ |> Logic.mk_type |> Thm.cterm_of ctxt |> Drule.mk_term
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   496
    in
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   497
      Conjunction.intr_balanced [abs_eq, (the_default TrueI opt_rep_eq), mk_type rty, mk_type qty]
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   498
    end
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   499
60230
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   500
  exception DECODE
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   501
60230
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   502
  fun decode_code_eq thm =
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   503
    if Thm.nprems_of thm > 0 then raise DECODE
60230
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   504
    else
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   505
      let
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   506
        val [abs_eq, rep_eq, rty, qty] = Conjunction.elim_balanced 4 thm
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   507
        val opt_rep_eq = if Thm.eq_thm_prop (rep_eq, TrueI) then NONE else SOME rep_eq
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   508
        fun dest_type typ = typ |> Drule.dest_term |> Thm.term_of |> Logic.dest_type
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   509
      in
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   510
        (abs_eq, opt_rep_eq, (dest_type rty, dest_type qty))
60230
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   511
      end
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   512
60230
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   513
  structure Data = Generic_Data
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   514
  (
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   515
    type T = code_eq option
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   516
    val empty = NONE
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   517
    val extend = I
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   518
    fun merge _ = NONE
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   519
  );
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   520
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   521
  fun register_encoded_code_eq thm thy =
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   522
    let
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   523
      val (abs_eq_thm, opt_rep_eq_thm, (rty, qty)) = decode_code_eq thm
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   524
      val (code_eq, thy) = register_code_eq_thy abs_eq_thm opt_rep_eq_thm (rty, qty) thy
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   525
    in
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   526
      Context.theory_map (Data.put (SOME code_eq)) thy
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   527
    end
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   528
    handle DECODE => thy
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   529
60230
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   530
  val register_code_eq_attribute = Thm.declaration_attribute
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   531
    (fn thm => Context.mapping (register_encoded_code_eq thm) I)
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   532
  val register_code_eq_attrib = Attrib.internal (K register_code_eq_attribute)
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   533
55724
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   534
in
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   535
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   536
fun register_code_eq abs_eq_thm opt_rep_eq_thm (rty, qty) lthy =
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   537
  let
60230
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   538
    val encoded_code_eq = encode_code_eq lthy abs_eq_thm opt_rep_eq_thm (rty, qty)
55724
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   539
  in
60230
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   540
    if no_no_code lthy (rty, qty) then
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   541
      let
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   542
        val lthy' = lthy
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   543
          |> (#2 oo Local_Theory.note) ((Binding.empty, [register_code_eq_attrib]), [encoded_code_eq])
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   544
        val opt_code_eq = Data.get (Context.Theory (Proof_Context.theory_of lthy'))
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   545
        val code_eq =
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   546
          if is_some opt_code_eq then the opt_code_eq
60230
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   547
          else UNKNOWN_EQ (* UNKNOWN_EQ means that we are in a locale and we do not know
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   548
            which code equation is going to be used. This is going to be resolved at the
4857d553c52c go back to the complicated code equation registration (because of type classes) that was lost in 922586b1bc87; make it even more hackish to get which code equation was used
kuncar
parents: 60229
diff changeset
   549
            point when an interpretation of the locale is executed. *)
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   550
        val lthy'' = lthy'
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   551
          |> Local_Theory.declaration {syntax = false, pervasive = true} (K (Data.put NONE))
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   552
      in (code_eq, lthy'') end
57663
b590fcd03a4a store explicitly quotient types with no_code => more precise registration of code equations
kuncar
parents: 57642
diff changeset
   553
    else
60227
eacf75e4da95 return also which code equation was used; tuned
kuncar
parents: 60225
diff changeset
   554
      (NONE_EQ, lthy)
55724
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   555
  end
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   556
end
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   557
47852
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   558
(*
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   559
  Defines an operation on an abstract type in terms of a corresponding operation
47852
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   560
    on a representation type.
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   561
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   562
  var - a binding and a mixfix of the new constant being defined
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   563
  qty - an abstract type of the new constant
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   564
  rhs - a term representing the new constant on the raw level
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: 47852
diff changeset
   565
  rsp_thm - a respectfulness theorem in the internal tagged form (like '(R ===> R ===> R) f f'),
47852
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   566
    i.e. "(Lifting_Term.equiv_relation (fastype_of rhs, qty)) $ rhs $ rhs"
54335
03b10317ba78 update documentation of important public ML functions in Lifting
kuncar
parents: 53951
diff changeset
   567
  par_thms - a parametricity theorem for rhs
47852
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   568
*)
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   569
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   570
fun add_lift_def (config: config) (binding, mx) qty rhs rsp_thm par_thms lthy0 =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   571
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   572
    val rty = fastype_of rhs
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   573
    val quot_thm = Lifting_Term.prove_quot_thm lthy0 (rty, qty)
47951
8c8a03765de7 quot_del attribute, it allows us to deregister quotient types
kuncar
parents: 47937
diff changeset
   574
    val absrep_trm =  quot_thm_abs quot_thm
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   575
    val rty_forced = (domain_type o fastype_of) absrep_trm
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   576
    val forced_rhs = force_rty_type lthy0 rty_forced rhs
63341
40f58bb9c846 clarified derived bindings (for PIDE reports);
wenzelm
parents: 63239
diff changeset
   577
    val lhs = Free (Binding.name_of binding, qty)
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   578
    val prop = Logic.mk_equals (lhs, absrep_trm $ forced_rhs)
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   579
    val (_, prop') = Local_Defs.cert_def lthy0 (K []) prop
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   580
    val (_, newrhs) = Local_Defs.abs_def prop'
63341
40f58bb9c846 clarified derived bindings (for PIDE reports);
wenzelm
parents: 63239
diff changeset
   581
    val var = ((#notes config = false ? Binding.concealed) binding, mx)
40f58bb9c846 clarified derived bindings (for PIDE reports);
wenzelm
parents: 63239
diff changeset
   582
    val def_name = Thm.make_def_binding (#notes config) (#1 var)
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   583
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   584
    val ((lift_const, (_ , def_thm)), lthy1) = lthy0
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   585
      |> Local_Theory.define (var, ((def_name, []), newrhs))
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   586
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   587
    val transfer_rules = generate_transfer_rules lthy1 quot_thm rsp_thm def_thm par_thms
49975
faf4afed009f transfer package: more flexible handling of equality relations using is_equality predicate
huffman
parents: 49885
diff changeset
   588
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   589
    val abs_eq_thm = generate_abs_eq lthy1 def_thm rsp_thm quot_thm
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   590
    val opt_rep_eq_thm = generate_rep_eq lthy1 def_thm rsp_thm (rty_forced, qty)
47351
0193e663a19e lift_definition command generates transfer rule
huffman
parents: 47308
diff changeset
   591
60225
eb4e322734bf note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents: 60224
diff changeset
   592
    fun notes names =
eb4e322734bf note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents: 60224
diff changeset
   593
      let
63341
40f58bb9c846 clarified derived bindings (for PIDE reports);
wenzelm
parents: 63239
diff changeset
   594
        val lhs_name = Binding.reset_pos (#1 var)
63003
bf5fcc65586b clarified signature;
wenzelm
parents: 62514
diff changeset
   595
        val rsp_thmN = Binding.qualify_name true lhs_name "rsp"
bf5fcc65586b clarified signature;
wenzelm
parents: 62514
diff changeset
   596
        val abs_eq_thmN = Binding.qualify_name true lhs_name "abs_eq"
bf5fcc65586b clarified signature;
wenzelm
parents: 62514
diff changeset
   597
        val rep_eq_thmN = Binding.qualify_name true lhs_name "rep_eq"
bf5fcc65586b clarified signature;
wenzelm
parents: 62514
diff changeset
   598
        val transfer_ruleN = Binding.qualify_name true lhs_name "transfer"
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   599
        val notes =
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   600
          [(rsp_thmN, [], [rsp_thm]),
60225
eb4e322734bf note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents: 60224
diff changeset
   601
          (transfer_ruleN, @{attributes [transfer_rule]}, transfer_rules),
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   602
          (abs_eq_thmN, [], [abs_eq_thm])]
60225
eb4e322734bf note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents: 60224
diff changeset
   603
          @ (case opt_rep_eq_thm of SOME rep_eq_thm => [(rep_eq_thmN, [], [rep_eq_thm])] | NONE => [])
eb4e322734bf note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents: 60224
diff changeset
   604
      in
eb4e322734bf note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents: 60224
diff changeset
   605
        if names then map (fn (name, attrs, thms) => ((name, []), [(thms, attrs)])) notes
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   606
        else map_filter (fn (_, attrs, thms) => if null attrs then NONE
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   607
          else SOME (Binding.empty_atts, [(thms, attrs)])) notes
60225
eb4e322734bf note theorems by Local_Theory.notes (it is faster); make note of the generated theorems optional
kuncar
parents: 60224
diff changeset
   608
      end
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   609
    val (code_eq, lthy2) = lthy1
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   610
      |> register_code_eq abs_eq_thm opt_rep_eq_thm (rty_forced, qty)
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   611
    val lift_def = mk_lift_def rty_forced qty newrhs lift_const def_thm rsp_thm abs_eq_thm
60227
eacf75e4da95 return also which code equation was used; tuned
kuncar
parents: 60225
diff changeset
   612
          opt_rep_eq_thm code_eq transfer_rules
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   613
  in
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   614
    lthy2
72450
24bd1316eaae consolidated names and operations
haftmann
parents: 72154
diff changeset
   615
    |> (snd o Local_Theory.begin_nested)
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   616
    |> Local_Theory.notes (notes (#notes config)) |> snd
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   617
    |> `(fn lthy => morph_lift_def (Local_Theory.target_morphism lthy) lift_def)
72450
24bd1316eaae consolidated names and operations
haftmann
parents: 72154
diff changeset
   618
    ||> Local_Theory.end_nested
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   619
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   620
56540
8267d1ff646f fix the reflexivity prover
kuncar
parents: 56524
diff changeset
   621
(* This is not very cheap way of getting the rules but we have only few active
8267d1ff646f fix the reflexivity prover
kuncar
parents: 56524
diff changeset
   622
  liftings in the current setting *)
8267d1ff646f fix the reflexivity prover
kuncar
parents: 56524
diff changeset
   623
fun get_cr_pcr_eqs ctxt =
8267d1ff646f fix the reflexivity prover
kuncar
parents: 56524
diff changeset
   624
  let
8267d1ff646f fix the reflexivity prover
kuncar
parents: 56524
diff changeset
   625
    fun collect (data : Lifting_Info.quotient) l =
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   626
      if is_some (#pcr_info data)
70473
9179e7a98196 clarified signature;
wenzelm
parents: 70320
diff changeset
   627
      then ((Thm.symmetric o safe_mk_meta_eq o Thm.transfer' ctxt o #pcr_cr_eq o the o #pcr_info) data :: l)
56540
8267d1ff646f fix the reflexivity prover
kuncar
parents: 56524
diff changeset
   628
      else l
8267d1ff646f fix the reflexivity prover
kuncar
parents: 56524
diff changeset
   629
    val table = Lifting_Info.get_quotients ctxt
8267d1ff646f fix the reflexivity prover
kuncar
parents: 56524
diff changeset
   630
  in
8267d1ff646f fix the reflexivity prover
kuncar
parents: 56524
diff changeset
   631
    Symtab.fold (fn (_, data) => fn l => collect data l) table []
8267d1ff646f fix the reflexivity prover
kuncar
parents: 56524
diff changeset
   632
  end
8267d1ff646f fix the reflexivity prover
kuncar
parents: 56524
diff changeset
   633
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   634
fun prepare_lift_def add_lift_def var qty rhs par_thms ctxt =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   635
  let
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   636
    val rsp_rel = Lifting_Term.equiv_relation ctxt (fastype_of rhs, qty)
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   637
    val rty_forced = (domain_type o fastype_of) rsp_rel;
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   638
    val forced_rhs = force_rty_type ctxt rty_forced rhs;
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   639
    val cr_to_pcr_conv = HOLogic.Trueprop_conv (Conv.fun2_conv
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   640
      (Raw_Simplifier.rewrite ctxt false (get_cr_pcr_eqs ctxt)))
56540
8267d1ff646f fix the reflexivity prover
kuncar
parents: 56524
diff changeset
   641
    val (prsp_tm, rsp_prsp_eq) = HOLogic.mk_Trueprop (rsp_rel $ forced_rhs $ forced_rhs)
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   642
      |> Thm.cterm_of ctxt
56540
8267d1ff646f fix the reflexivity prover
kuncar
parents: 56524
diff changeset
   643
      |> cr_to_pcr_conv
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   644
      |> `Thm.concl_of
56540
8267d1ff646f fix the reflexivity prover
kuncar
parents: 56524
diff changeset
   645
      |>> Logic.dest_equals
8267d1ff646f fix the reflexivity prover
kuncar
parents: 56524
diff changeset
   646
      |>> snd
8267d1ff646f fix the reflexivity prover
kuncar
parents: 56524
diff changeset
   647
    val to_rsp = rsp_prsp_eq RS Drule.equal_elim_rule2
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   648
    val opt_proven_rsp_thm = try_prove_reflexivity ctxt prsp_tm
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   649
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   650
    fun after_qed internal_rsp_thm =
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   651
      add_lift_def var qty rhs (internal_rsp_thm RS to_rsp) par_thms
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   652
  in
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   653
    case opt_proven_rsp_thm of
60218
7e24e172052e lift_definition: interface also with tactic
kuncar
parents: 60217
diff changeset
   654
      SOME thm => (NONE, K (after_qed thm))
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   655
      | NONE => (SOME prsp_tm, after_qed)
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   656
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   657
60227
eacf75e4da95 return also which code equation was used; tuned
kuncar
parents: 60225
diff changeset
   658
fun gen_lift_def add_lift_def var qty rhs tac par_thms lthy =
60218
7e24e172052e lift_definition: interface also with tactic
kuncar
parents: 60217
diff changeset
   659
  let
60227
eacf75e4da95 return also which code equation was used; tuned
kuncar
parents: 60225
diff changeset
   660
    val (goal, after_qed) = prepare_lift_def add_lift_def var qty rhs par_thms lthy
60218
7e24e172052e lift_definition: interface also with tactic
kuncar
parents: 60217
diff changeset
   661
  in
7e24e172052e lift_definition: interface also with tactic
kuncar
parents: 60217
diff changeset
   662
    case goal of
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   663
      SOME goal =>
4eaf35781b23 tuned signature;
wenzelm
parents: 63341
diff changeset
   664
        let
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   665
          val rsp_thm =
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   666
            Goal.prove_sorry lthy [] [] goal (tac o #context)
70494
41108e3e9ca5 formal position for PThm nodes;
wenzelm
parents: 70473
diff changeset
   667
            |> Thm.close_derivation \<^here>
60218
7e24e172052e lift_definition: interface also with tactic
kuncar
parents: 60217
diff changeset
   668
        in
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   669
          after_qed rsp_thm lthy
60218
7e24e172052e lift_definition: interface also with tactic
kuncar
parents: 60217
diff changeset
   670
        end
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   671
      | NONE => after_qed Drule.dummy_thm lthy
60218
7e24e172052e lift_definition: interface also with tactic
kuncar
parents: 60217
diff changeset
   672
  end
7e24e172052e lift_definition: interface also with tactic
kuncar
parents: 60217
diff changeset
   673
70320
59258a3192bf misc tuning and clarification, notably wrt. flow of context;
wenzelm
parents: 69593
diff changeset
   674
val lift_def = gen_lift_def o add_lift_def
60227
eacf75e4da95 return also which code equation was used; tuned
kuncar
parents: 60225
diff changeset
   675
53651
ee90c67502c9 restoring Transfer/Lifting context
kuncar
parents: 53207
diff changeset
   676
end (* structure *)