src/HOL/Tools/Transfer/transfer_bnf.ML
author blanchet
Tue, 28 Apr 2015 13:30:28 +0200
changeset 60207 81a0900f0ddc
parent 59823 a03696dc3283
child 60216 ef4f0b5b925e
permissions -rw-r--r--
allow sorts on dead variables in BNFs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
     1
(*  Title:      HOL/Tools/Transfer/transfer_bnf.ML
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
     2
    Author:     Ondrej Kuncar, TU Muenchen
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
     3
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
     4
Setup for Transfer for types that are BNF.
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
     5
*)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
     6
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
     7
signature TRANSFER_BNF =
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
     8
sig
59823
a03696dc3283 more graceful failure if some of the involved BNFs have no data
blanchet
parents: 59621
diff changeset
     9
  exception NO_PRED_DATA of unit
a03696dc3283 more graceful failure if some of the involved BNFs have no data
blanchet
parents: 59621
diff changeset
    10
59276
blanchet
parents: 58963
diff changeset
    11
  val transfer_plugin: string
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    12
  val base_name_of_bnf: BNF_Def.bnf -> binding
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    13
  val type_name_of_bnf: BNF_Def.bnf -> string
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    14
  val lookup_defined_pred_data: Proof.context -> string -> Transfer.pred_data
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    15
  val bnf_only_type_ctr: (BNF_Def.bnf -> 'a -> 'a) -> BNF_Def.bnf -> 'a -> 'a
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    16
end
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    17
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    18
structure Transfer_BNF : TRANSFER_BNF =
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    19
struct
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    20
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    21
open BNF_Util
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    22
open BNF_Def
56651
fc105315822a localize new size function generation code
blanchet
parents: 56648
diff changeset
    23
open BNF_FP_Util
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    24
open BNF_FP_Def_Sugar
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    25
59823
a03696dc3283 more graceful failure if some of the involved BNFs have no data
blanchet
parents: 59621
diff changeset
    26
exception NO_PRED_DATA of unit
a03696dc3283 more graceful failure if some of the involved BNFs have no data
blanchet
parents: 59621
diff changeset
    27
59276
blanchet
parents: 58963
diff changeset
    28
val transfer_plugin = Plugin_Name.declare_setup @{binding transfer}
blanchet
parents: 58963
diff changeset
    29
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    30
(* util functions *)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    31
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    32
fun base_name_of_bnf bnf = Binding.name (Binding.name_of (name_of_bnf bnf))
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    33
fun mk_Frees_free x Ts ctxt = Variable.variant_frees ctxt [] (mk_names (length Ts) x ~~ Ts) |> map Free
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    34
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    35
fun mk_Domainp P =
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    36
  let
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    37
    val PT = fastype_of P
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    38
    val argT = hd (binder_types PT)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    39
  in
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    40
    Const (@{const_name Domainp}, PT --> argT --> HOLogic.boolT) $ P
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    41
  end
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    42
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    43
fun mk_pred pred_def args T =
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    44
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
    45
    val pred_name = pred_def |> Thm.prop_of |> HOLogic.dest_Trueprop |> fst o HOLogic.dest_eq
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    46
      |> head_of |> fst o dest_Const
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    47
    val argsT = map fastype_of args
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    48
  in
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    49
    list_comb (Const (pred_name, argsT ---> (T --> HOLogic.boolT)), args)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    50
  end
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    51
57399
cfc19f0a6261 compile
blanchet
parents: 56677
diff changeset
    52
fun mk_eq_onp arg =
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    53
  let
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    54
    val argT = domain_type (fastype_of arg)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    55
  in
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    56
    Const (@{const_name eq_onp}, (argT --> HOLogic.boolT) --> argT --> argT --> HOLogic.boolT)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    57
      $ arg
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    58
  end
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    59
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    60
fun subst_conv thm =
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    61
  Conv.top_sweep_conv (K (Conv.rewr_conv (safe_mk_meta_eq thm))) @{context}
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    62
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    63
fun type_name_of_bnf bnf = T_of_bnf bnf |> dest_Type |> fst
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    64
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    65
fun is_Type (Type _) = true
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    66
  | is_Type _ = false
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    67
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    68
fun bnf_only_type_ctr f bnf = if is_Type (T_of_bnf bnf) then f bnf else I
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    69
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    70
fun bnf_of_fp_sugar (fp_sugar:fp_sugar) = nth (#bnfs (#fp_res fp_sugar)) (#fp_res_index fp_sugar)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    71
56648
2ffa440b3074 manual merge + added 'rel_distincts' field to record for symmetry
blanchet
parents: 56538
diff changeset
    72
fun fp_sugar_only_type_ctr f fp_sugars =
2ffa440b3074 manual merge + added 'rel_distincts' field to record for symmetry
blanchet
parents: 56538
diff changeset
    73
  (case filter (is_Type o T_of_bnf o bnf_of_fp_sugar) fp_sugars of
2ffa440b3074 manual merge + added 'rel_distincts' field to record for symmetry
blanchet
parents: 56538
diff changeset
    74
    [] => I
2ffa440b3074 manual merge + added 'rel_distincts' field to record for symmetry
blanchet
parents: 56538
diff changeset
    75
  | fp_sugars' => f fp_sugars')
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    76
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    77
(* relation constraints - bi_total & co. *)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    78
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    79
fun mk_relation_constraint name arg =
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    80
  (Const (name, fastype_of arg --> HOLogic.boolT)) $ arg
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    81
57399
cfc19f0a6261 compile
blanchet
parents: 56677
diff changeset
    82
fun side_constraint_tac bnf constr_defs ctxt i =
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    83
  let
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    84
    val thms = constr_defs @ map mk_sym [rel_eq_of_bnf bnf, rel_conversep_of_bnf bnf,
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    85
      rel_OO_of_bnf bnf]
57399
cfc19f0a6261 compile
blanchet
parents: 56677
diff changeset
    86
  in
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    87
    (SELECT_GOAL (Local_Defs.unfold_tac ctxt thms) THEN' rtac (rel_mono_of_bnf bnf)
58963
26bf09b95dda proper context for assume_tac (atac remains as fall-back without context);
wenzelm
parents: 58782
diff changeset
    88
      THEN_ALL_NEW assume_tac ctxt) i
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    89
  end
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    90
57399
cfc19f0a6261 compile
blanchet
parents: 56677
diff changeset
    91
fun bi_constraint_tac constr_iff sided_constr_intros ctxt i =
cfc19f0a6261 compile
blanchet
parents: 56677
diff changeset
    92
  (SELECT_GOAL (Local_Defs.unfold_tac ctxt [constr_iff]) THEN'
58963
26bf09b95dda proper context for assume_tac (atac remains as fall-back without context);
wenzelm
parents: 58782
diff changeset
    93
    CONJ_WRAP' (fn thm => rtac thm THEN_ALL_NEW
26bf09b95dda proper context for assume_tac (atac remains as fall-back without context);
wenzelm
parents: 58782
diff changeset
    94
      (REPEAT_DETERM o etac conjE THEN' assume_tac ctxt)) sided_constr_intros) i
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    95
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    96
fun generate_relation_constraint_goal ctxt bnf constraint_def =
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
    97
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
    98
    val constr_name =
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
    99
      constraint_def |> Thm.prop_of |> HOLogic.dest_Trueprop |> fst o HOLogic.dest_eq
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   100
      |> head_of |> fst o dest_Const
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   101
    val live = live_of_bnf bnf
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   102
    val (((As, Bs), Ds), ctxt) = ctxt
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   103
      |> mk_TFrees live
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   104
      ||>> mk_TFrees live
60207
81a0900f0ddc allow sorts on dead variables in BNFs
blanchet
parents: 59823
diff changeset
   105
      ||>> mk_TFrees' (map Type.sort_of_atyp (deads_of_bnf bnf))
57399
cfc19f0a6261 compile
blanchet
parents: 56677
diff changeset
   106
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   107
    val relator = mk_rel_of_bnf Ds As Bs bnf
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   108
    val relsT = map2 mk_pred2T As Bs
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   109
    val (args, ctxt) = Ctr_Sugar_Util.mk_Frees "R" relsT ctxt
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   110
    val concl = HOLogic.mk_Trueprop (mk_relation_constraint constr_name (list_comb (relator, args)))
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   111
    val assms = map (HOLogic.mk_Trueprop o (mk_relation_constraint constr_name)) args
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   112
    val goal = Logic.list_implies (assms, concl)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   113
  in
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   114
    (goal, ctxt)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   115
  end
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   116
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   117
fun prove_relation_side_constraint ctxt bnf constraint_def =
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   118
  let
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   119
    val old_ctxt = ctxt
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   120
    val (goal, ctxt) = generate_relation_constraint_goal ctxt bnf constraint_def
57890
1e13f63fb452 use Goal.prove_sorry instead of Goal.prove where possible (= no schematics in goal and tactic is expected to succeed)
traytel
parents: 57399
diff changeset
   121
    val thm = Goal.prove_sorry ctxt [] [] goal
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   122
      (fn {context = ctxt, prems = _} => side_constraint_tac bnf [constraint_def] ctxt 1)
57890
1e13f63fb452 use Goal.prove_sorry instead of Goal.prove where possible (= no schematics in goal and tactic is expected to succeed)
traytel
parents: 57399
diff changeset
   123
      |> Thm.close_derivation
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   124
  in
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   125
    Drule.zero_var_indexes (singleton (Variable.export ctxt old_ctxt) thm)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   126
  end
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   127
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   128
fun prove_relation_bi_constraint ctxt bnf constraint_def side_constraints =
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   129
  let
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   130
    val old_ctxt = ctxt
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   131
    val (goal, ctxt) = generate_relation_constraint_goal ctxt bnf constraint_def
57890
1e13f63fb452 use Goal.prove_sorry instead of Goal.prove where possible (= no schematics in goal and tactic is expected to succeed)
traytel
parents: 57399
diff changeset
   132
    val thm = Goal.prove_sorry ctxt [] [] goal
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   133
      (fn {context = ctxt, prems = _} => bi_constraint_tac constraint_def side_constraints ctxt 1)
57890
1e13f63fb452 use Goal.prove_sorry instead of Goal.prove where possible (= no schematics in goal and tactic is expected to succeed)
traytel
parents: 57399
diff changeset
   134
      |> Thm.close_derivation
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   135
  in
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   136
    Drule.zero_var_indexes (singleton (Variable.export ctxt old_ctxt) thm)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   137
  end
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   138
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   139
val defs = [("left_total_rel", @{thm left_total_alt_def}), ("right_total_rel", @{thm right_total_alt_def}),
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   140
  ("left_unique_rel", @{thm left_unique_alt_def}), ("right_unique_rel", @{thm right_unique_alt_def})]
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   141
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   142
fun prove_relation_constraints bnf lthy =
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   143
  let
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   144
    val transfer_attr = @{attributes [transfer_rule]}
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   145
    val Tname = base_name_of_bnf bnf
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   146
    fun qualify suffix = Binding.qualified true suffix Tname
57399
cfc19f0a6261 compile
blanchet
parents: 56677
diff changeset
   147
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   148
    val defs = map (apsnd (prove_relation_side_constraint lthy bnf)) defs
57399
cfc19f0a6261 compile
blanchet
parents: 56677
diff changeset
   149
    val bi_total = prove_relation_bi_constraint lthy bnf @{thm bi_total_alt_def}
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   150
      [snd (nth defs 0), snd (nth defs 1)]
57399
cfc19f0a6261 compile
blanchet
parents: 56677
diff changeset
   151
    val bi_unique = prove_relation_bi_constraint lthy bnf @{thm bi_unique_alt_def}
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   152
      [snd (nth defs 2), snd (nth defs 3)]
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   153
    val defs = ("bi_total_rel", bi_total) :: ("bi_unique_rel", bi_unique) :: defs
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   154
    val notes = maps (fn (name, thm) => [((qualify name, []), [([thm], transfer_attr)])]) defs
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   155
  in
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   156
    notes
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   157
  end
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   158
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   159
(* relator_eq *)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   160
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   161
fun relator_eq bnf =
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   162
  [((Binding.empty, []), [([rel_eq_of_bnf bnf], @{attributes [relator_eq]})])]
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   163
58722
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   164
(* transfer rules *)
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   165
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   166
fun bnf_transfer_rules bnf =
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   167
  let
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   168
    val transfer_rules = map_transfer_of_bnf bnf :: rel_transfer_of_bnf bnf 
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   169
      :: set_transfer_of_bnf bnf
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   170
    val transfer_attr = @{attributes [transfer_rule]}
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   171
  in
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   172
    map (fn thm => ((Binding.empty,[]),[([thm],transfer_attr)])) transfer_rules
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   173
  end
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   174
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   175
(* predicator definition and Domainp and eq_onp theorem *)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   176
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   177
fun define_pred bnf lthy =
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   178
  let
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   179
    fun mk_pred_name c = Binding.prefix_name "pred_" c
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   180
    val live = live_of_bnf bnf
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   181
    val Tname = base_name_of_bnf bnf
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   182
    val ((As, Ds), lthy) = lthy
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   183
      |> mk_TFrees live
60207
81a0900f0ddc allow sorts on dead variables in BNFs
blanchet
parents: 59823
diff changeset
   184
      ||>> mk_TFrees' (map Type.sort_of_atyp (deads_of_bnf bnf))
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   185
    val T = mk_T_of_bnf Ds As bnf
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   186
    val sets = mk_sets_of_bnf (replicate live Ds) (replicate live As) bnf
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   187
    val argTs = map mk_pred1T As
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   188
    val args = mk_Frees_free "P" argTs lthy
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   189
    val conjs = map (fn (set, arg) => mk_Ball (set $ Bound 0) arg) (sets ~~ args)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   190
    val rhs = Abs ("x", T, foldr1 HOLogic.mk_conj conjs)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   191
    val pred_name = mk_pred_name Tname
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   192
    val headT = argTs ---> (T --> HOLogic.boolT)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   193
    val head = Free (Binding.name_of pred_name, headT)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   194
    val lhs = list_comb (head, args)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   195
    val def = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
57399
cfc19f0a6261 compile
blanchet
parents: 56677
diff changeset
   196
    val ((_, (_, pred_def)), lthy) = Specification.definition ((SOME (pred_name, SOME headT, NoSyn)),
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   197
      ((Binding.empty, []), def)) lthy
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   198
  in
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   199
    (pred_def, lthy)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   200
  end
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   201
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   202
fun Domainp_tac bnf pred_def ctxt i =
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   203
  let
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   204
    val n = live_of_bnf bnf
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   205
    val set_map's = set_map_of_bnf bnf
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   206
  in
57399
cfc19f0a6261 compile
blanchet
parents: 56677
diff changeset
   207
    EVERY' [rtac ext, SELECT_GOAL (Local_Defs.unfold_tac ctxt [@{thm Domainp.simps},
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   208
        in_rel_of_bnf bnf, pred_def]), rtac iffI,
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59276
diff changeset
   209
        REPEAT_DETERM o eresolve_tac ctxt [exE, conjE, CollectE], hyp_subst_tac ctxt,
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   210
        CONJ_WRAP' (fn set_map => EVERY' [rtac ballI, dtac (set_map RS equalityD1 RS set_mp),
58963
26bf09b95dda proper context for assume_tac (atac remains as fall-back without context);
wenzelm
parents: 58782
diff changeset
   211
          etac imageE, dtac set_rev_mp, assume_tac ctxt,
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59276
diff changeset
   212
          REPEAT_DETERM o eresolve_tac ctxt [CollectE, @{thm case_prodE}],
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   213
          hyp_subst_tac ctxt, rtac @{thm iffD2[OF arg_cong2[of _ _ _ _ Domainp, OF refl fst_conv]]},
57399
cfc19f0a6261 compile
blanchet
parents: 56677
diff changeset
   214
          etac @{thm DomainPI}]) set_map's,
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59276
diff changeset
   215
        REPEAT_DETERM o etac conjE,
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59276
diff changeset
   216
        REPEAT_DETERM o resolve_tac ctxt [exI, (refl RS conjI), rotate_prems 1 conjI],
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   217
        rtac refl, rtac (box_equals OF [map_cong0_of_bnf bnf, map_comp_of_bnf bnf RS sym,
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   218
          map_id_of_bnf bnf]),
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   219
        REPEAT_DETERM_N n o (EVERY' [rtac @{thm box_equals[OF _ sym[OF o_apply] sym[OF id_apply]]},
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   220
          rtac @{thm fst_conv}]), rtac CollectI,
57399
cfc19f0a6261 compile
blanchet
parents: 56677
diff changeset
   221
        CONJ_WRAP' (fn set_map => EVERY' [rtac (set_map RS @{thm ord_eq_le_trans}),
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59276
diff changeset
   222
          REPEAT_DETERM o resolve_tac ctxt [@{thm image_subsetI}, CollectI, @{thm case_prodI}],
58963
26bf09b95dda proper context for assume_tac (atac remains as fall-back without context);
wenzelm
parents: 58782
diff changeset
   223
          dtac (rotate_prems 1 bspec), assume_tac ctxt,
26bf09b95dda proper context for assume_tac (atac remains as fall-back without context);
wenzelm
parents: 58782
diff changeset
   224
          etac @{thm DomainpE}, etac @{thm someI}]) set_map's
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   225
      ] i
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   226
  end
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   227
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   228
fun prove_Domainp_rel ctxt bnf pred_def =
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   229
  let
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   230
    val live = live_of_bnf bnf
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   231
    val old_ctxt = ctxt
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   232
    val (((As, Bs), Ds), ctxt) = ctxt
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   233
      |> mk_TFrees live
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   234
      ||>> mk_TFrees live
60207
81a0900f0ddc allow sorts on dead variables in BNFs
blanchet
parents: 59823
diff changeset
   235
      ||>> mk_TFrees' (map Type.sort_of_atyp (deads_of_bnf bnf))
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   236
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   237
    val relator = mk_rel_of_bnf Ds As Bs bnf
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   238
    val relsT = map2 mk_pred2T As Bs
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   239
    val T = mk_T_of_bnf Ds As bnf
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   240
    val (args, ctxt) = Ctr_Sugar_Util.mk_Frees "R" relsT ctxt
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   241
    val lhs = mk_Domainp (list_comb (relator, args))
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   242
    val rhs = mk_pred pred_def (map mk_Domainp args) T
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   243
    val goal = HOLogic.mk_eq (lhs, rhs) |> HOLogic.mk_Trueprop
57890
1e13f63fb452 use Goal.prove_sorry instead of Goal.prove where possible (= no schematics in goal and tactic is expected to succeed)
traytel
parents: 57399
diff changeset
   244
    val thm = Goal.prove_sorry ctxt [] [] goal
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   245
      (fn {context = ctxt, prems = _} => Domainp_tac bnf pred_def ctxt 1)
57890
1e13f63fb452 use Goal.prove_sorry instead of Goal.prove where possible (= no schematics in goal and tactic is expected to succeed)
traytel
parents: 57399
diff changeset
   246
      |> Thm.close_derivation
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   247
  in
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   248
    Drule.zero_var_indexes (singleton (Variable.export ctxt old_ctxt) thm)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   249
  end
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   250
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   251
fun pred_eq_onp_tac bnf pred_def ctxt i =
57399
cfc19f0a6261 compile
blanchet
parents: 56677
diff changeset
   252
  (SELECT_GOAL (Local_Defs.unfold_tac ctxt [@{thm eq_onp_Grp},
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   253
    @{thm Ball_Collect}, pred_def]) THEN' CONVERSION (subst_conv (map_id0_of_bnf bnf RS sym))
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   254
  THEN' rtac (rel_Grp_of_bnf bnf)) i
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   255
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   256
fun prove_rel_eq_onp ctxt bnf pred_def =
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   257
  let
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   258
    val live = live_of_bnf bnf
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   259
    val old_ctxt = ctxt
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   260
    val ((As, Ds), ctxt) = ctxt
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   261
      |> mk_TFrees live
60207
81a0900f0ddc allow sorts on dead variables in BNFs
blanchet
parents: 59823
diff changeset
   262
      ||>> mk_TFrees' (map Type.sort_of_atyp (deads_of_bnf bnf))
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   263
    val T = mk_T_of_bnf Ds As bnf
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   264
    val argTs = map mk_pred1T As
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   265
    val (args, ctxt) = mk_Frees "P" argTs ctxt
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   266
    val relator = mk_rel_of_bnf Ds As As bnf
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   267
    val lhs = list_comb (relator, map mk_eq_onp args)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   268
    val rhs = mk_eq_onp (mk_pred pred_def args T)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   269
    val goal = HOLogic.mk_eq (lhs, rhs) |> HOLogic.mk_Trueprop
57890
1e13f63fb452 use Goal.prove_sorry instead of Goal.prove where possible (= no schematics in goal and tactic is expected to succeed)
traytel
parents: 57399
diff changeset
   270
    val thm = Goal.prove_sorry ctxt [] [] goal
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   271
      (fn {context = ctxt, prems = _} => pred_eq_onp_tac bnf pred_def ctxt 1)
57890
1e13f63fb452 use Goal.prove_sorry instead of Goal.prove where possible (= no schematics in goal and tactic is expected to succeed)
traytel
parents: 57399
diff changeset
   272
      |> Thm.close_derivation
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   273
  in
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   274
    Drule.zero_var_indexes (singleton (Variable.export ctxt old_ctxt) thm)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   275
  end
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   276
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   277
fun predicator bnf lthy =
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   278
  let
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   279
    val (pred_def, lthy) = define_pred bnf lthy
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   280
    val pred_def = Morphism.thm (Local_Theory.target_morphism lthy) pred_def
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   281
    val Domainp_rel = prove_Domainp_rel lthy bnf pred_def
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   282
    val rel_eq_onp = prove_rel_eq_onp lthy bnf pred_def
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   283
    fun qualify defname suffix = Binding.qualified true suffix defname
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   284
    val Domainp_rel_thm_name = qualify (base_name_of_bnf bnf) "Domainp_rel"
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   285
    val rel_eq_onp_thm_name = qualify (base_name_of_bnf bnf) "rel_eq_onp"
57399
cfc19f0a6261 compile
blanchet
parents: 56677
diff changeset
   286
    val rel_eq_onp_internal = Conv.fconv_rule (HOLogic.Trueprop_conv (Conv.arg1_conv
56677
660ffb526069 predicator simplification rules: support also partially specialized types e.g. 'a * nat
kuncar
parents: 56651
diff changeset
   287
      (Raw_Simplifier.rewrite lthy false @{thms eq_onp_top_eq_eq[symmetric, THEN eq_reflection]})))
660ffb526069 predicator simplification rules: support also partially specialized types e.g. 'a * nat
kuncar
parents: 56651
diff changeset
   288
        rel_eq_onp
660ffb526069 predicator simplification rules: support also partially specialized types e.g. 'a * nat
kuncar
parents: 56651
diff changeset
   289
    val pred_data = {rel_eq_onp = rel_eq_onp_internal}
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   290
    val type_name = type_name_of_bnf bnf
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   291
    val relator_domain_attr = @{attributes [relator_domain]}
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   292
    val notes = [((Domainp_rel_thm_name, []), [([Domainp_rel], relator_domain_attr)]),
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   293
      ((rel_eq_onp_thm_name, []), [([rel_eq_onp], [])])]
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   294
  in
58356
2f04f1fd28aa added missing 'restore' in 'transfer' plugin
blanchet
parents: 58234
diff changeset
   295
    lthy
2f04f1fd28aa added missing 'restore' in 'transfer' plugin
blanchet
parents: 58234
diff changeset
   296
    |> Local_Theory.notes notes
2f04f1fd28aa added missing 'restore' in 'transfer' plugin
blanchet
parents: 58234
diff changeset
   297
    |> snd
2f04f1fd28aa added missing 'restore' in 'transfer' plugin
blanchet
parents: 58234
diff changeset
   298
    |> Local_Theory.declaration {syntax = false, pervasive = true}
2f04f1fd28aa added missing 'restore' in 'transfer' plugin
blanchet
parents: 58234
diff changeset
   299
      (fn phi => Transfer.update_pred_data type_name (Transfer.morph_pred_data phi pred_data))
2f04f1fd28aa added missing 'restore' in 'transfer' plugin
blanchet
parents: 58234
diff changeset
   300
    |> Local_Theory.restore
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   301
  end
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   302
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   303
(* BNF interpretation *)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   304
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   305
fun transfer_bnf_interpretation bnf lthy =
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   306
  let
58356
2f04f1fd28aa added missing 'restore' in 'transfer' plugin
blanchet
parents: 58234
diff changeset
   307
    val dead = dead_of_bnf bnf
2f04f1fd28aa added missing 'restore' in 'transfer' plugin
blanchet
parents: 58234
diff changeset
   308
    val constr_notes = if dead > 0 then [] else prove_relation_constraints bnf lthy
2f04f1fd28aa added missing 'restore' in 'transfer' plugin
blanchet
parents: 58234
diff changeset
   309
    val relator_eq_notes = if dead > 0 then [] else relator_eq bnf
58722
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   310
    val transfer_rule_notes = if dead > 0 then [] else bnf_transfer_rules bnf
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   311
  in
58356
2f04f1fd28aa added missing 'restore' in 'transfer' plugin
blanchet
parents: 58234
diff changeset
   312
    lthy
58722
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   313
    |> Local_Theory.notes (constr_notes @ relator_eq_notes @ transfer_rule_notes)
58356
2f04f1fd28aa added missing 'restore' in 'transfer' plugin
blanchet
parents: 58234
diff changeset
   314
    |> snd
2f04f1fd28aa added missing 'restore' in 'transfer' plugin
blanchet
parents: 58234
diff changeset
   315
    |> predicator bnf
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   316
  end
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   317
58659
6c9821c32dd5 Local_Interpretation is superseded by Plugin with formal Plugin_Name management, avoiding undeclared strings;
wenzelm
parents: 58458
diff changeset
   318
val _ =
6c9821c32dd5 Local_Interpretation is superseded by Plugin with formal Plugin_Name management, avoiding undeclared strings;
wenzelm
parents: 58458
diff changeset
   319
  Theory.setup
6c9821c32dd5 Local_Interpretation is superseded by Plugin with formal Plugin_Name management, avoiding undeclared strings;
wenzelm
parents: 58458
diff changeset
   320
    (bnf_interpretation transfer_plugin (bnf_only_type_ctr transfer_bnf_interpretation))
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   321
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   322
(* simplification rules for the predicator *)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   323
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   324
fun lookup_defined_pred_data lthy name =
59823
a03696dc3283 more graceful failure if some of the involved BNFs have no data
blanchet
parents: 59621
diff changeset
   325
  case Transfer.lookup_pred_data lthy name of
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   326
    SOME data => data
59823
a03696dc3283 more graceful failure if some of the involved BNFs have no data
blanchet
parents: 59621
diff changeset
   327
  | NONE => raise NO_PRED_DATA ()
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   328
58782
7305bad408b5 made SML/NJ happy;
wenzelm
parents: 58722
diff changeset
   329
fun lives_of_fp (fp_sugar: fp_sugar) =
58721
f9a966c834bc refactored
kuncar
parents: 58659
diff changeset
   330
  let
f9a966c834bc refactored
kuncar
parents: 58659
diff changeset
   331
    val As = snd (dest_Type (#T fp_sugar))
f9a966c834bc refactored
kuncar
parents: 58659
diff changeset
   332
    val liveness = liveness_of_fp_bnf (length As) (hd (#bnfs (#fp_res fp_sugar)));
f9a966c834bc refactored
kuncar
parents: 58659
diff changeset
   333
  in
f9a966c834bc refactored
kuncar
parents: 58659
diff changeset
   334
    map_filter (uncurry (fn true => SOME | false => K NONE)) (liveness ~~ As)
f9a966c834bc refactored
kuncar
parents: 58659
diff changeset
   335
  end
f9a966c834bc refactored
kuncar
parents: 58659
diff changeset
   336
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   337
fun prove_pred_inject lthy (fp_sugar:fp_sugar) =
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   338
  let
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   339
    val involved_types = distinct op= (
57399
cfc19f0a6261 compile
blanchet
parents: 56677
diff changeset
   340
        map type_name_of_bnf (#fp_nesting_bnfs fp_sugar)
cfc19f0a6261 compile
blanchet
parents: 56677
diff changeset
   341
      @ map type_name_of_bnf (#live_nesting_bnfs fp_sugar)
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   342
      @ map type_name_of_bnf (#bnfs (#fp_res fp_sugar)))
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   343
    val eq_onps = map (Transfer.rel_eq_onp o lookup_defined_pred_data lthy) involved_types
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   344
    val old_lthy = lthy
60207
81a0900f0ddc allow sorts on dead variables in BNFs
blanchet
parents: 59823
diff changeset
   345
    val (As, lthy) = mk_TFrees' (map Type.sort_of_atyp (lives_of_fp fp_sugar)) lthy
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   346
    val predTs = map mk_pred1T As
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   347
    val (preds, lthy) = mk_Frees "P" predTs lthy
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   348
    val args = map mk_eq_onp preds
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59582
diff changeset
   349
    val cTs = map (SOME o Thm.ctyp_of lthy) (maps (replicate 2) As)
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59582
diff changeset
   350
    val cts = map (SOME o Thm.cterm_of lthy) args
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   351
    fun get_rhs thm = thm |> Thm.concl_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> snd
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   352
    fun is_eqn thm = can get_rhs thm
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   353
    fun rel2pred_massage thm =
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   354
      let
56677
660ffb526069 predicator simplification rules: support also partially specialized types e.g. 'a * nat
kuncar
parents: 56651
diff changeset
   355
        val live_step = @{lemma "x = y \<Longrightarrow> (eq_onp P a a \<and> x) = (P a \<and> y)" by (simp only: eq_onp_same_args)}
660ffb526069 predicator simplification rules: support also partially specialized types e.g. 'a * nat
kuncar
parents: 56651
diff changeset
   356
        val kill_top1 = @{lemma "(top x \<and> P) = P" by blast}
660ffb526069 predicator simplification rules: support also partially specialized types e.g. 'a * nat
kuncar
parents: 56651
diff changeset
   357
        val kill_top2 = @{lemma "(P \<and> top x) = P" by blast}
660ffb526069 predicator simplification rules: support also partially specialized types e.g. 'a * nat
kuncar
parents: 56651
diff changeset
   358
        fun pred_eq_onp_conj conjs = List.foldr (fn (_, thm) => thm RS live_step)
660ffb526069 predicator simplification rules: support also partially specialized types e.g. 'a * nat
kuncar
parents: 56651
diff changeset
   359
          @{thm refl[of True]} conjs
56538
7c3b6b799b94 observe also DEADID BNFs and associate the conjunction in rel_inject to the right
kuncar
parents: 56524
diff changeset
   360
        val conjuncts = if is_eqn thm then thm |> get_rhs |> HOLogic.dest_conj else []
56677
660ffb526069 predicator simplification rules: support also partially specialized types e.g. 'a * nat
kuncar
parents: 56651
diff changeset
   361
        val kill_top = Local_Defs.unfold lthy [kill_top2] #> Local_Defs.unfold lthy [kill_top1]
56538
7c3b6b799b94 observe also DEADID BNFs and associate the conjunction in rel_inject to the right
kuncar
parents: 56524
diff changeset
   362
        val kill_True = Local_Defs.unfold lthy [@{thm HOL.simp_thms(21)}]
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   363
      in
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   364
        thm
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   365
        |> Drule.instantiate' cTs cts
57399
cfc19f0a6261 compile
blanchet
parents: 56677
diff changeset
   366
        |> Conv.fconv_rule (HOLogic.Trueprop_conv (Conv.arg_conv
56677
660ffb526069 predicator simplification rules: support also partially specialized types e.g. 'a * nat
kuncar
parents: 56651
diff changeset
   367
          (Raw_Simplifier.rewrite lthy false @{thms eq_onp_top_eq_eq[symmetric, THEN eq_reflection]})))
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   368
        |> Local_Defs.unfold lthy eq_onps
57399
cfc19f0a6261 compile
blanchet
parents: 56677
diff changeset
   369
        |> (fn thm => if conjuncts <> [] then @{thm box_equals}
56538
7c3b6b799b94 observe also DEADID BNFs and associate the conjunction in rel_inject to the right
kuncar
parents: 56524
diff changeset
   370
              OF [thm, @{thm eq_onp_same_args}, pred_eq_onp_conj conjuncts |> kill_True]
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   371
            else thm RS (@{thm eq_onp_same_args} RS iffD1))
56677
660ffb526069 predicator simplification rules: support also partially specialized types e.g. 'a * nat
kuncar
parents: 56651
diff changeset
   372
        |> kill_top
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   373
      end
58458
0c9d59cb3af9 refactor fp_sugar move theorems
desharna
parents: 58356
diff changeset
   374
    val rel_injects = #rel_injects (#fp_bnf_sugar fp_sugar)
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   375
  in
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   376
    rel_injects
56538
7c3b6b799b94 observe also DEADID BNFs and associate the conjunction in rel_inject to the right
kuncar
parents: 56524
diff changeset
   377
    |> map (Local_Defs.unfold lthy [@{thm conj_assoc}])
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   378
    |> map rel2pred_massage
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   379
    |> Variable.export lthy old_lthy
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   380
    |> map Drule.zero_var_indexes
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   381
  end
59823
a03696dc3283 more graceful failure if some of the involved BNFs have no data
blanchet
parents: 59621
diff changeset
   382
  handle NO_PRED_DATA () => []
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   383
56677
660ffb526069 predicator simplification rules: support also partially specialized types e.g. 'a * nat
kuncar
parents: 56651
diff changeset
   384
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   385
(* fp_sugar interpretation *)
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   386
58722
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   387
fun fp_sugar_transfer_rules (fp_sugar:fp_sugar) =
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   388
  let
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   389
    val fp_ctr_sugar = #fp_ctr_sugar fp_sugar
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   390
    val transfer_rules = #ctr_transfers fp_ctr_sugar @ #case_transfers fp_ctr_sugar 
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   391
      @ #disc_transfers fp_ctr_sugar @ #sel_transfers fp_ctr_sugar 
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   392
      @ (#co_rec_transfers o #fp_co_induct_sugar) fp_sugar
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   393
    val transfer_attr = @{attributes [transfer_rule]}
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   394
  in
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   395
    map (fn thm => ((Binding.empty, []), [([thm], transfer_attr)])) transfer_rules
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   396
  end
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   397
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   398
fun pred_injects fp_sugar lthy =
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   399
  let
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   400
    val pred_injects = prove_pred_inject lthy fp_sugar
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   401
    fun qualify defname suffix = Binding.qualified true suffix defname
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   402
    val pred_inject_thm_name = qualify (base_name_of_bnf (bnf_of_fp_sugar fp_sugar)) "pred_inject"
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   403
    val simp_attrs = @{attributes [simp]}
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   404
  in
58722
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   405
    [((pred_inject_thm_name, []), [(pred_injects, simp_attrs)])]
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   406
  end
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   407
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   408
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   409
fun transfer_fp_sugars_interpretation fp_sugar lthy =
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   410
  let
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   411
    val pred_injects_notes = pred_injects fp_sugar lthy
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   412
    val transfer_rules_notes = fp_sugar_transfer_rules fp_sugar
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   413
  in
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   414
    lthy
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   415
    |> Local_Theory.notes (pred_injects_notes @ transfer_rules_notes)
9cd739562c71 register transfer rules from BNF and FP_Sugar
kuncar
parents: 58721
diff changeset
   416
    |> snd
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   417
  end
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   418
58659
6c9821c32dd5 Local_Interpretation is superseded by Plugin with formal Plugin_Name management, avoiding undeclared strings;
wenzelm
parents: 58458
diff changeset
   419
val _ =
6c9821c32dd5 Local_Interpretation is superseded by Plugin with formal Plugin_Name management, avoiding undeclared strings;
wenzelm
parents: 58458
diff changeset
   420
  Theory.setup (fp_sugars_interpretation transfer_plugin
6c9821c32dd5 Local_Interpretation is superseded by Plugin with formal Plugin_Name management, avoiding undeclared strings;
wenzelm
parents: 58458
diff changeset
   421
    (fp_sugar_only_type_ctr (fold transfer_fp_sugars_interpretation)))
56524
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   422
f4ba736040fa setup for Transfer and Lifting from BNF; tuned thm names
kuncar
parents:
diff changeset
   423
end