src/Pure/Isar/interpretation.ML
author wenzelm
Fri Feb 23 14:12:48 2018 +0100 (24 months ago ago)
changeset 67702 2d9918f5b33c
parent 67450 b0ae74b86ef3
child 67740 b6ce18784872
permissions -rw-r--r--
command 'interpret' no longer exposes resulting theorems as literal facts;
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*)
wenzelm@62680
    14
  val interpret: Expression.expression_i -> term rewrites -> Proof.state -> Proof.state
wenzelm@62680
    15
  val interpret_cmd: Expression.expression -> string rewrites -> Proof.state -> Proof.state
haftmann@61673
    16
haftmann@61858
    17
  (*interpretation in local theories*)
haftmann@61858
    18
  val interpretation: Expression.expression_i ->
haftmann@61858
    19
    term rewrites -> local_theory -> Proof.state
haftmann@61858
    20
  val interpretation_cmd: Expression.expression ->
haftmann@61858
    21
    string rewrites -> local_theory -> Proof.state
haftmann@61858
    22
haftmann@61858
    23
  (*interpretation into global theories*)
haftmann@61673
    24
  val global_interpretation: Expression.expression_i ->
haftmann@61858
    25
    term defines -> term rewrites -> local_theory -> Proof.state
haftmann@61858
    26
  val global_interpretation_cmd: Expression.expression ->
haftmann@61858
    27
    string defines -> string rewrites -> local_theory -> Proof.state
haftmann@61858
    28
haftmann@61858
    29
  (*interpretation between locales*)
haftmann@61673
    30
  val sublocale: Expression.expression_i ->
haftmann@61673
    31
    term defines -> term rewrites -> local_theory -> Proof.state
haftmann@61673
    32
  val sublocale_cmd: Expression.expression ->
haftmann@61673
    33
    string defines -> string rewrites -> local_theory -> Proof.state
haftmann@61858
    34
  val global_sublocale: string -> Expression.expression_i ->
haftmann@61858
    35
    term defines -> term rewrites -> theory -> Proof.state
haftmann@61858
    36
  val global_sublocale_cmd: xstring * Position.T -> Expression.expression ->
haftmann@61858
    37
    string defines -> string rewrites -> theory -> Proof.state
haftmann@61673
    38
haftmann@61673
    39
  (*mixed Isar interface*)
haftmann@61858
    40
  val isar_interpretation: Expression.expression_i ->
haftmann@61858
    41
    term rewrites -> local_theory -> Proof.state
haftmann@61858
    42
  val isar_interpretation_cmd: Expression.expression ->
haftmann@61858
    43
    string rewrites -> local_theory -> Proof.state
haftmann@61669
    44
end;
haftmann@61669
    45
haftmann@61669
    46
structure Interpretation : INTERPRETATION =
haftmann@61669
    47
struct
haftmann@61669
    48
haftmann@61673
    49
(** common interpretation machinery **)
haftmann@61673
    50
haftmann@61673
    51
type 'a defines = (Attrib.binding * ((binding * mixfix) * 'a)) list
haftmann@61673
    52
type 'a rewrites = (Attrib.binding * 'a) list
haftmann@61673
    53
haftmann@61673
    54
(* reading of locale expressions with rewrite morphisms *)
haftmann@61670
    55
haftmann@61672
    56
local
haftmann@61669
    57
ballarin@67450
    58
fun augment_with_def prep_term ((name, atts), ((b, mx), raw_rhs)) lthy =
haftmann@61670
    59
  let
haftmann@61775
    60
    val rhs = prep_term lthy raw_rhs;
haftmann@61775
    61
    val lthy' = Variable.declare_term rhs lthy;
haftmann@61775
    62
    val ((_, (_, def)), lthy'') =
haftmann@61775
    63
      Local_Theory.define ((b, mx), ((Thm.def_binding_optional b name, atts), rhs)) lthy';
haftmann@61775
    64
  in (def, lthy'') end;
haftmann@61669
    65
ballarin@67450
    66
fun augment_with_defs _ [] _ ctxt = ([], ctxt)
haftmann@61691
    67
      (*quasi-inhomogeneous type: definitions demand local theory rather than bare proof context*)
haftmann@61775
    68
  | augment_with_defs prep_term raw_defs deps lthy =
haftmann@61691
    69
      let
haftmann@61775
    70
        val (_, inner_lthy) =
haftmann@61775
    71
          Local_Theory.open_target lthy
haftmann@61775
    72
          ||> fold Locale.activate_declarations deps;
haftmann@61775
    73
        val (inner_defs, inner_lthy') =
ballarin@67450
    74
          fold_map (augment_with_def prep_term) raw_defs inner_lthy;
haftmann@61775
    75
        val lthy' =
haftmann@61775
    76
          inner_lthy'
haftmann@61708
    77
          |> Local_Theory.close_target;
haftmann@61708
    78
        val def_eqns =
haftmann@61775
    79
          map (singleton (Proof_Context.export inner_lthy' lthy') o Thm.symmetric) inner_defs
haftmann@61775
    80
      in (def_eqns, lthy') end;
haftmann@61775
    81
ballarin@67450
    82
fun prep_eqns _ _ [] _ _ = ([], [])
haftmann@61775
    83
  | prep_eqns prep_props prep_attr raw_eqns deps ctxt =
haftmann@61775
    84
      let
ballarin@67450
    85
        (* FIXME incompatibility, creating context for parsing rewrites equation may fail in
ballarin@67450
    86
           presence of replaces clause *)
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
ballarin@67450
    96
    val ((propss, eq_propss, deps, eqnss, 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;
ballarin@67450
   102
  in (((propss, eq_propss, deps, eqnss, 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
wenzelm@63382
   121
fun meta_rewrite eqns ctxt =
wenzelm@63382
   122
  (map (Local_Defs.abs_def_rule ctxt) (maps snd eqns), ctxt);
haftmann@61670
   123
ballarin@67450
   124
fun note_eqns_register pos note activate deps eqnss witss def_eqns thms attrss export export' ctxt =
haftmann@61670
   125
  let
ballarin@67450
   126
    val (thmss, thms') = split_last (unflat ((map o map) fst eqnss @ [attrss]) thms);
ballarin@67450
   127
    val factss =
ballarin@67450
   128
      map2 (map2 (fn b => fn eq => (b, [([Morphism.thm export eq], [])]))) ((map o map) fst eqnss) thmss;
ballarin@67450
   129
    val (eqnss', ctxt') = fold_map (fn facts => note Thm.theoremK facts #-> meta_rewrite) factss ctxt;
wenzelm@63382
   130
    val facts =
wenzelm@63382
   131
      (Binding.empty_atts, [(map (Morphism.thm (export' $> export)) def_eqns, [])]) ::
wenzelm@63382
   132
        map2 (fn attrs => fn eqn => (attrs, [([Morphism.thm (export' $> export) eqn], [])]))
ballarin@67450
   133
          attrss thms';
ballarin@67450
   134
    val (eqns', ctxt'') = ctxt'
haftmann@61670
   135
      |> note Thm.theoremK facts
haftmann@61670
   136
      |-> meta_rewrite;
wenzelm@63382
   137
    val dep_morphs =
wenzelm@63382
   138
      map2 (fn (dep, morph) => fn wits =>
wenzelm@63383
   139
        let val morph' = morph
wenzelm@63383
   140
          $> Element.satisfy_morphism (map (Element.transform_witness export') wits)
wenzelm@63383
   141
          $> Morphism.binding_morphism "position" (Binding.set_pos pos)
wenzelm@63383
   142
        in (dep, morph') end) deps witss;
ballarin@67450
   143
    fun activate' (dep_morph, eqns) ctxt =
wenzelm@63382
   144
      activate dep_morph
ballarin@67450
   145
        (Option.map (rpair true) (Element.eq_morphism (Proof_Context.theory_of ctxt) (eqns @ eqns')))
wenzelm@63382
   146
        export ctxt;
ballarin@67450
   147
  in ctxt'' |> fold activate' (dep_morphs ~~ eqnss') end;
haftmann@61670
   148
haftmann@61670
   149
in
haftmann@61670
   150
wenzelm@63381
   151
fun generic_interpretation prep_interpretation setup_proof note add_registration
haftmann@61670
   152
    expression raw_defs raw_eqns initial_ctxt =
haftmann@61670
   153
  let
ballarin@67450
   154
    val (((propss, eq_propss, deps, eqnss, export, export'), (def_eqns, eqns, attrss)), goal_ctxt) =
haftmann@61670
   155
      prep_interpretation expression raw_defs raw_eqns initial_ctxt;
wenzelm@63383
   156
    val pos = Position.thread_data ();
haftmann@61670
   157
    fun after_qed witss eqns =
ballarin@67450
   158
      note_eqns_register pos note add_registration deps eqnss witss def_eqns eqns attrss export export';
ballarin@67450
   159
  in setup_proof after_qed propss (flat (eq_propss @ [eqns])) goal_ctxt end;
haftmann@61670
   160
haftmann@61670
   161
end;
haftmann@61670
   162
haftmann@61670
   163
haftmann@61673
   164
(** interfaces **)
haftmann@61673
   165
haftmann@61673
   166
(* interpretation in proofs *)
haftmann@61670
   167
haftmann@61672
   168
local
haftmann@61672
   169
wenzelm@62680
   170
fun gen_interpret prep_interpretation expression raw_eqns state =
haftmann@61669
   171
  let
haftmann@61669
   172
    val _ = Proof.assert_forward_or_chain state;
haftmann@61669
   173
    fun lift_after_qed after_qed witss eqns =
haftmann@61669
   174
      Proof.map_context (after_qed witss eqns) #> Proof.reset_facts;
haftmann@61669
   175
    fun setup_proof after_qed propss eqns goal_ctxt =
haftmann@61669
   176
      Element.witness_local_proof_eqs (lift_after_qed after_qed) "interpret"
wenzelm@62680
   177
        propss eqns goal_ctxt state;
wenzelm@67702
   178
    fun add_registration reg mixin export ctxt = ctxt
wenzelm@67702
   179
      |> Proof_Context.set_stmt false
wenzelm@67702
   180
      |> Context.proof_map (Locale.add_registration reg mixin export)
wenzelm@67702
   181
      |> Proof_Context.restore_stmt ctxt;
haftmann@61669
   182
  in
wenzelm@63380
   183
    Proof.context_of state
wenzelm@63380
   184
    |> generic_interpretation prep_interpretation setup_proof
wenzelm@67702
   185
      Attrib.local_notes add_registration expression [] raw_eqns
haftmann@61669
   186
  end;
haftmann@61669
   187
haftmann@61673
   188
in
haftmann@61670
   189
wenzelm@63380
   190
val interpret = gen_interpret cert_interpretation;
wenzelm@63380
   191
val interpret_cmd = gen_interpret read_interpretation;
haftmann@61669
   192
haftmann@61673
   193
end;
haftmann@61670
   194
haftmann@61670
   195
haftmann@61858
   196
(* interpretation in local theories *)
haftmann@61858
   197
haftmann@61858
   198
fun interpretation expression =
haftmann@61858
   199
  generic_interpretation cert_interpretation Element.witness_proof_eqs
wenzelm@63381
   200
    Local_Theory.notes_kind Locale.activate_fragment expression [];
haftmann@61858
   201
wenzelm@63381
   202
fun interpretation_cmd expression =
haftmann@61858
   203
  generic_interpretation read_interpretation Element.witness_proof_eqs
wenzelm@63381
   204
    Local_Theory.notes_kind Locale.activate_fragment expression [];
haftmann@61858
   205
haftmann@61858
   206
haftmann@61858
   207
(* interpretation into global theories *)
haftmann@61858
   208
haftmann@61858
   209
fun global_interpretation expression =
wenzelm@63381
   210
  generic_interpretation cert_interpretation Element.witness_proof_eqs
haftmann@61858
   211
    Local_Theory.notes_kind Local_Theory.theory_registration expression;
haftmann@61858
   212
wenzelm@63381
   213
fun global_interpretation_cmd expression =
wenzelm@63381
   214
  generic_interpretation read_interpretation Element.witness_proof_eqs
wenzelm@63381
   215
    Local_Theory.notes_kind Local_Theory.theory_registration expression;
haftmann@61858
   216
haftmann@61858
   217
haftmann@61858
   218
(* interpretation between locales *)
haftmann@61858
   219
haftmann@61858
   220
fun sublocale expression =
wenzelm@63381
   221
  generic_interpretation cert_interpretation Element.witness_proof_eqs
haftmann@61858
   222
    Local_Theory.notes_kind Local_Theory.locale_dependency expression;
haftmann@61858
   223
wenzelm@63381
   224
fun sublocale_cmd expression =
wenzelm@63381
   225
  generic_interpretation read_interpretation Element.witness_proof_eqs
wenzelm@63381
   226
    Local_Theory.notes_kind Local_Theory.locale_dependency expression;
haftmann@61673
   227
haftmann@61673
   228
local
haftmann@61669
   229
haftmann@61673
   230
fun gen_global_sublocale prep_loc prep_interpretation
haftmann@61673
   231
    raw_locale expression raw_defs raw_eqns thy =
haftmann@61673
   232
  let
haftmann@66336
   233
    val lthy = Named_Target.init (prep_loc thy raw_locale) thy;
haftmann@61673
   234
    fun setup_proof after_qed =
haftmann@61673
   235
      Element.witness_proof_eqs
haftmann@61673
   236
        (fn wits => fn eqs => after_qed wits eqs #> Local_Theory.exit);
haftmann@61673
   237
  in
haftmann@61673
   238
    lthy |>
wenzelm@63381
   239
      generic_interpretation prep_interpretation setup_proof
haftmann@61858
   240
        Local_Theory.notes_kind Local_Theory.locale_dependency expression raw_defs raw_eqns
haftmann@61673
   241
  end;
haftmann@61669
   242
haftmann@61673
   243
in
haftmann@61669
   244
haftmann@61673
   245
fun global_sublocale expression =
haftmann@61673
   246
  gen_global_sublocale (K I) cert_interpretation expression;
haftmann@61858
   247
haftmann@61673
   248
fun global_sublocale_cmd raw_expression =
haftmann@61673
   249
  gen_global_sublocale Locale.check read_interpretation raw_expression;
haftmann@61669
   250
haftmann@61673
   251
end;
haftmann@61673
   252
haftmann@61673
   253
haftmann@61673
   254
(* mixed Isar interface *)
haftmann@61673
   255
haftmann@61673
   256
local
haftmann@61670
   257
haftmann@61858
   258
fun register_or_activate lthy =
haftmann@61673
   259
  if Named_Target.is_theory lthy
haftmann@61858
   260
  then Local_Theory.theory_registration
haftmann@61673
   261
  else Locale.activate_fragment;
haftmann@61669
   262
haftmann@61858
   263
fun gen_isar_interpretation prep_interpretation expression raw_eqns lthy =
haftmann@61673
   264
  generic_interpretation prep_interpretation Element.witness_proof_eqs
wenzelm@63381
   265
    Local_Theory.notes_kind (register_or_activate lthy) expression [] raw_eqns lthy;
haftmann@61669
   266
haftmann@61673
   267
in
haftmann@61673
   268
haftmann@61858
   269
fun isar_interpretation expression =
haftmann@61858
   270
  gen_isar_interpretation cert_interpretation expression;
haftmann@61858
   271
fun isar_interpretation_cmd raw_expression =
haftmann@61858
   272
  gen_isar_interpretation read_interpretation raw_expression;
haftmann@61669
   273
haftmann@61669
   274
end;
haftmann@61669
   275
haftmann@61669
   276
end;