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