src/Pure/Isar/interpretation.ML
author haftmann
Sat Dec 19 11:05:04 2015 +0100 (2015-12-19 ago)
changeset 61858 f6ded81f5690
parent 61823 5daa82ba4078
child 62680 646b84666a56
permissions -rw-r--r--
abandoned attempt to unify sublocale and interpretation into global theories
haftmann@61669
     1
(*  Title:      Pure/Isar/interpretation.ML
haftmann@61669
     2
    Author:     Clemens Ballarin, TU Muenchen
haftmann@61670
     3
    Author:     Florian Haftmann, TU Muenchen
haftmann@61669
     4
haftmann@61669
     5
Locale interpretation.
haftmann@61669
     6
*)
haftmann@61669
     7
haftmann@61669
     8
signature INTERPRETATION =
haftmann@61669
     9
sig
haftmann@61673
    10
  type 'a defines = (Attrib.binding * ((binding * mixfix) * 'a)) list
haftmann@61673
    11
  type 'a rewrites = (Attrib.binding * 'a) list
haftmann@61673
    12
haftmann@61673
    13
  (*interpretation in proofs*)
haftmann@61858
    14
  val interpret: Expression.expression_i ->
haftmann@61858
    15
    term rewrites -> bool -> Proof.state -> Proof.state
haftmann@61858
    16
  val interpret_cmd: Expression.expression ->
haftmann@61858
    17
    string rewrites -> bool -> Proof.state -> Proof.state
haftmann@61673
    18
haftmann@61858
    19
  (*interpretation in local theories*)
haftmann@61858
    20
  val interpretation: Expression.expression_i ->
haftmann@61858
    21
    term rewrites -> local_theory -> Proof.state
haftmann@61858
    22
  val interpretation_cmd: Expression.expression ->
haftmann@61858
    23
    string rewrites -> local_theory -> Proof.state
haftmann@61858
    24
haftmann@61858
    25
  (*interpretation into global theories*)
haftmann@61673
    26
  val global_interpretation: Expression.expression_i ->
haftmann@61858
    27
    term defines -> term rewrites -> local_theory -> Proof.state
haftmann@61858
    28
  val global_interpretation_cmd: Expression.expression ->
haftmann@61858
    29
    string defines -> string rewrites -> local_theory -> Proof.state
haftmann@61858
    30
haftmann@61858
    31
  (*interpretation between locales*)
haftmann@61673
    32
  val sublocale: Expression.expression_i ->
haftmann@61673
    33
    term defines -> term rewrites -> local_theory -> Proof.state
haftmann@61673
    34
  val sublocale_cmd: Expression.expression ->
haftmann@61673
    35
    string defines -> string rewrites -> local_theory -> Proof.state
haftmann@61858
    36
  val global_sublocale: string -> Expression.expression_i ->
haftmann@61858
    37
    term defines -> term rewrites -> theory -> Proof.state
haftmann@61858
    38
  val global_sublocale_cmd: xstring * Position.T -> Expression.expression ->
haftmann@61858
    39
    string defines -> string rewrites -> theory -> Proof.state
haftmann@61673
    40
haftmann@61673
    41
  (*mixed Isar interface*)
haftmann@61858
    42
  val isar_interpretation: Expression.expression_i ->
haftmann@61858
    43
    term rewrites -> local_theory -> Proof.state
haftmann@61858
    44
  val isar_interpretation_cmd: Expression.expression ->
haftmann@61858
    45
    string rewrites -> local_theory -> Proof.state
haftmann@61669
    46
end;
haftmann@61669
    47
haftmann@61669
    48
structure Interpretation : INTERPRETATION =
haftmann@61669
    49
struct
haftmann@61669
    50
haftmann@61673
    51
(** common interpretation machinery **)
haftmann@61673
    52
haftmann@61673
    53
type 'a defines = (Attrib.binding * ((binding * mixfix) * 'a)) list
haftmann@61673
    54
type 'a rewrites = (Attrib.binding * 'a) list
haftmann@61673
    55
haftmann@61673
    56
(* reading of locale expressions with rewrite morphisms *)
haftmann@61670
    57
haftmann@61672
    58
local
haftmann@61669
    59
haftmann@61775
    60
fun augment_with_def prep_term deps ((name, atts), ((b, mx), raw_rhs)) lthy =
haftmann@61670
    61
  let
haftmann@61775
    62
    val rhs = prep_term lthy raw_rhs;
haftmann@61775
    63
    val lthy' = Variable.declare_term rhs lthy;
haftmann@61775
    64
    val ((_, (_, def)), lthy'') =
haftmann@61775
    65
      Local_Theory.define ((b, mx), ((Thm.def_binding_optional b name, atts), rhs)) lthy';
haftmann@61775
    66
  in (def, lthy'') end;
haftmann@61669
    67
haftmann@61775
    68
fun augment_with_defs prep_term [] deps ctxt = ([], ctxt)
haftmann@61691
    69
      (*quasi-inhomogeneous type: definitions demand local theory rather than bare proof context*)
haftmann@61775
    70
  | augment_with_defs prep_term raw_defs deps lthy =
haftmann@61691
    71
      let
haftmann@61775
    72
        val (_, inner_lthy) =
haftmann@61775
    73
          Local_Theory.open_target lthy
haftmann@61775
    74
          ||> fold Locale.activate_declarations deps;
haftmann@61775
    75
        val (inner_defs, inner_lthy') =
haftmann@61775
    76
          fold_map (augment_with_def prep_term deps) raw_defs inner_lthy;
haftmann@61775
    77
        val lthy' =
haftmann@61775
    78
          inner_lthy'
haftmann@61708
    79
          |> Local_Theory.close_target;
haftmann@61708
    80
        val def_eqns =
haftmann@61775
    81
          map (singleton (Proof_Context.export inner_lthy' lthy') o Thm.symmetric) inner_defs
haftmann@61775
    82
      in (def_eqns, lthy') end;
haftmann@61775
    83
haftmann@61775
    84
fun prep_eqns prep_props prep_attr [] deps ctxt = ([], [])
haftmann@61775
    85
  | prep_eqns prep_props prep_attr raw_eqns deps ctxt =
haftmann@61775
    86
      let
haftmann@61775
    87
        val ctxt' = fold Locale.activate_declarations deps ctxt;
haftmann@61775
    88
        val eqns =
haftmann@61775
    89
          (Variable.export_terms ctxt' ctxt o prep_props ctxt' o map snd) raw_eqns;
haftmann@61775
    90
        val attrss = map (apsnd (map (prep_attr ctxt)) o fst) raw_eqns;
haftmann@61775
    91
      in (eqns, attrss) end;
haftmann@61691
    92
haftmann@61774
    93
fun prep_interpretation prep_expr prep_term prep_props prep_attr
haftmann@61691
    94
  expression raw_defs raw_eqns initial_ctxt =
haftmann@61691
    95
  let
haftmann@61691
    96
    val ((propss, deps, export), expr_ctxt) = prep_expr expression initial_ctxt;
haftmann@61775
    97
    val (def_eqns, def_ctxt) =
haftmann@61775
    98
      augment_with_defs prep_term raw_defs deps expr_ctxt;
haftmann@61775
    99
    val (eqns, attrss) = prep_eqns prep_props prep_attr raw_eqns deps def_ctxt;
haftmann@61672
   100
    val goal_ctxt = fold Variable.auto_fixes eqns def_ctxt;
haftmann@61772
   101
    val export' = Variable.export_morphism goal_ctxt expr_ctxt;
haftmann@61672
   102
  in (((propss, deps, export, export'), (def_eqns, eqns, attrss)), goal_ctxt) end;
haftmann@61669
   103
haftmann@61670
   104
in
haftmann@61670
   105
wenzelm@61698
   106
fun cert_interpretation expression =
haftmann@61774
   107
  prep_interpretation Expression.cert_goal_expression Syntax.check_term
haftmann@61774
   108
    Syntax.check_props (K I) expression;
haftmann@61672
   109
wenzelm@61698
   110
fun read_interpretation expression =
haftmann@61774
   111
  prep_interpretation Expression.read_goal_expression Syntax.read_term
haftmann@61774
   112
    Syntax.read_props Attrib.check_src expression;
haftmann@61669
   113
haftmann@61670
   114
end;
haftmann@61669
   115
haftmann@61670
   116
haftmann@61673
   117
(* interpretation machinery *)
haftmann@61670
   118
haftmann@61670
   119
local
haftmann@61670
   120
haftmann@61672
   121
fun meta_rewrite eqns ctxt =
haftmann@61672
   122
  (map (Local_Defs.meta_rewrite_rule ctxt #> Drule.abs_def) (maps snd eqns), ctxt);
haftmann@61670
   123
haftmann@61670
   124
fun note_eqns_register note activate deps witss def_eqns eqns attrss export export' ctxt =
haftmann@61670
   125
  let
haftmann@61670
   126
    val facts = (Attrib.empty_binding, [(map (Morphism.thm (export' $> export)) def_eqns, [])])
haftmann@61670
   127
      :: map2 (fn attrs => fn eqn => (attrs, [([Morphism.thm (export' $> export) eqn], [])])) attrss eqns;
haftmann@61670
   128
    val (eqns', ctxt') = ctxt
haftmann@61670
   129
      |> note Thm.theoremK facts
haftmann@61670
   130
      |-> meta_rewrite;
haftmann@61670
   131
    val dep_morphs = map2 (fn (dep, morph) => fn wits =>
haftmann@61670
   132
      (dep, morph $> Element.satisfy_morphism (map (Element.transform_witness export') wits))) deps witss;
haftmann@61670
   133
    fun activate' dep_morph ctxt = activate dep_morph
haftmann@61670
   134
      (Option.map (rpair true) (Element.eq_morphism (Proof_Context.theory_of ctxt) eqns')) export ctxt;
haftmann@61670
   135
  in
haftmann@61670
   136
    ctxt'
haftmann@61670
   137
    |> fold activate' dep_morphs
haftmann@61670
   138
  end;
haftmann@61670
   139
haftmann@61670
   140
in
haftmann@61670
   141
haftmann@61670
   142
fun generic_interpretation_with_defs prep_interpretation setup_proof note add_registration
haftmann@61670
   143
    expression raw_defs raw_eqns initial_ctxt =
haftmann@61670
   144
  let
haftmann@61670
   145
    val (((propss, deps, export, export'), (def_eqns, eqns, attrss)), goal_ctxt) =
haftmann@61670
   146
      prep_interpretation expression raw_defs raw_eqns initial_ctxt;
haftmann@61670
   147
    fun after_qed witss eqns =
haftmann@61670
   148
      note_eqns_register note add_registration deps witss def_eqns eqns attrss export export';
haftmann@61670
   149
  in setup_proof after_qed propss eqns goal_ctxt end;
haftmann@61670
   150
haftmann@61672
   151
fun generic_interpretation prep_interpretation setup_proof note add_registration expression =
haftmann@61672
   152
  generic_interpretation_with_defs prep_interpretation setup_proof note add_registration expression [];
haftmann@61672
   153
haftmann@61670
   154
end;
haftmann@61670
   155
haftmann@61670
   156
haftmann@61673
   157
(** interfaces **)
haftmann@61673
   158
haftmann@61673
   159
(* interpretation in proofs *)
haftmann@61670
   160
haftmann@61672
   161
local
haftmann@61672
   162
haftmann@61669
   163
fun gen_interpret prep_interpretation expression raw_eqns int state =
haftmann@61669
   164
  let
haftmann@61669
   165
    val _ = Proof.assert_forward_or_chain state;
haftmann@61669
   166
    val ctxt = Proof.context_of state;
haftmann@61669
   167
    fun lift_after_qed after_qed witss eqns =
haftmann@61669
   168
      Proof.map_context (after_qed witss eqns) #> Proof.reset_facts;
haftmann@61669
   169
    fun setup_proof after_qed propss eqns goal_ctxt =
haftmann@61669
   170
      Element.witness_local_proof_eqs (lift_after_qed after_qed) "interpret"
haftmann@61669
   171
        propss eqns goal_ctxt int state;
haftmann@61669
   172
  in
haftmann@61669
   173
    generic_interpretation prep_interpretation setup_proof
haftmann@61669
   174
      Attrib.local_notes (Context.proof_map ooo Locale.add_registration) expression raw_eqns ctxt
haftmann@61669
   175
  end;
haftmann@61669
   176
haftmann@61673
   177
in
haftmann@61670
   178
haftmann@61673
   179
fun interpret expression = gen_interpret cert_interpretation expression;
haftmann@61858
   180
haftmann@61673
   181
fun interpret_cmd raw_expression = gen_interpret read_interpretation raw_expression;
haftmann@61669
   182
haftmann@61673
   183
end;
haftmann@61670
   184
haftmann@61670
   185
haftmann@61858
   186
(* interpretation in local theories *)
haftmann@61858
   187
haftmann@61858
   188
fun interpretation expression =
haftmann@61858
   189
  generic_interpretation cert_interpretation Element.witness_proof_eqs
haftmann@61858
   190
    Local_Theory.notes_kind Locale.activate_fragment expression;
haftmann@61858
   191
haftmann@61858
   192
fun interpretation_cmd raw_expression =
haftmann@61858
   193
  generic_interpretation read_interpretation Element.witness_proof_eqs
haftmann@61858
   194
    Local_Theory.notes_kind Locale.activate_fragment raw_expression;
haftmann@61858
   195
haftmann@61858
   196
haftmann@61858
   197
(* interpretation into global theories *)
haftmann@61858
   198
haftmann@61858
   199
fun global_interpretation expression =
haftmann@61858
   200
  generic_interpretation_with_defs cert_interpretation Element.witness_proof_eqs
haftmann@61858
   201
    Local_Theory.notes_kind Local_Theory.theory_registration expression;
haftmann@61858
   202
haftmann@61858
   203
fun global_interpretation_cmd raw_expression =
haftmann@61858
   204
  generic_interpretation_with_defs read_interpretation Element.witness_proof_eqs
haftmann@61858
   205
    Local_Theory.notes_kind Local_Theory.theory_registration raw_expression;
haftmann@61858
   206
haftmann@61858
   207
haftmann@61858
   208
(* interpretation between locales *)
haftmann@61858
   209
haftmann@61858
   210
fun sublocale expression =
haftmann@61858
   211
  generic_interpretation_with_defs cert_interpretation Element.witness_proof_eqs
haftmann@61858
   212
    Local_Theory.notes_kind Local_Theory.locale_dependency expression;
haftmann@61858
   213
haftmann@61858
   214
fun sublocale_cmd raw_expression =
haftmann@61858
   215
  generic_interpretation_with_defs read_interpretation Element.witness_proof_eqs
haftmann@61858
   216
    Local_Theory.notes_kind Local_Theory.locale_dependency raw_expression;
haftmann@61673
   217
haftmann@61673
   218
local
haftmann@61669
   219
haftmann@61673
   220
fun gen_global_sublocale prep_loc prep_interpretation
haftmann@61673
   221
    raw_locale expression raw_defs raw_eqns thy =
haftmann@61673
   222
  let
haftmann@61673
   223
    val lthy = Named_Target.init (prep_loc thy raw_locale) thy;
haftmann@61673
   224
    fun setup_proof after_qed =
haftmann@61673
   225
      Element.witness_proof_eqs
haftmann@61673
   226
        (fn wits => fn eqs => after_qed wits eqs #> Local_Theory.exit);
haftmann@61673
   227
  in
haftmann@61673
   228
    lthy |>
haftmann@61673
   229
      generic_interpretation_with_defs prep_interpretation setup_proof
haftmann@61858
   230
        Local_Theory.notes_kind Local_Theory.locale_dependency expression raw_defs raw_eqns
haftmann@61673
   231
  end;
haftmann@61669
   232
haftmann@61673
   233
in
haftmann@61669
   234
haftmann@61673
   235
fun global_sublocale expression =
haftmann@61673
   236
  gen_global_sublocale (K I) cert_interpretation expression;
haftmann@61858
   237
haftmann@61673
   238
fun global_sublocale_cmd raw_expression =
haftmann@61673
   239
  gen_global_sublocale Locale.check read_interpretation raw_expression;
haftmann@61669
   240
haftmann@61673
   241
end;
haftmann@61673
   242
haftmann@61673
   243
haftmann@61673
   244
(* mixed Isar interface *)
haftmann@61673
   245
haftmann@61673
   246
local
haftmann@61670
   247
haftmann@61858
   248
fun register_or_activate lthy =
haftmann@61673
   249
  if Named_Target.is_theory lthy
haftmann@61858
   250
  then Local_Theory.theory_registration
haftmann@61673
   251
  else Locale.activate_fragment;
haftmann@61669
   252
haftmann@61858
   253
fun gen_isar_interpretation prep_interpretation expression raw_eqns lthy =
haftmann@61673
   254
  generic_interpretation prep_interpretation Element.witness_proof_eqs
haftmann@61858
   255
    Local_Theory.notes_kind (register_or_activate lthy) expression raw_eqns lthy;
haftmann@61669
   256
haftmann@61673
   257
in
haftmann@61673
   258
haftmann@61858
   259
fun isar_interpretation expression =
haftmann@61858
   260
  gen_isar_interpretation cert_interpretation expression;
haftmann@61858
   261
fun isar_interpretation_cmd raw_expression =
haftmann@61858
   262
  gen_isar_interpretation read_interpretation raw_expression;
haftmann@61669
   263
haftmann@61669
   264
end;
haftmann@61669
   265
haftmann@61669
   266
end;