src/Pure/Isar/interpretation.ML
author haftmann
Sat Nov 14 17:37:44 2015 +0100 (2015-11-14 ago)
changeset 61673 fd4ac1530d63
parent 61672 87203a0f0041
child 61676 872b2ee75359
permissions -rw-r--r--
represent both algebraic and local-theory views on locale interpretation in interfaces
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@61673
    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@61673
    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@61672
    55
fun prep_interpretation prep_expr prep_term prep_prop prep_attr expression raw_defs raw_eqns initial_ctxt =
haftmann@61670
    56
  let
haftmann@61670
    57
    (*reading*)
haftmann@61672
    58
    val ((propss, deps, export), expr_ctxt) = prep_expr expression initial_ctxt;
haftmann@61672
    59
    val deps_ctxt = fold Locale.activate_declarations deps expr_ctxt;
haftmann@61670
    60
    val prep = Syntax.check_terms deps_ctxt #> Variable.export_terms deps_ctxt deps_ctxt;
haftmann@61670
    61
    val rhss = (prep o map (prep_term deps_ctxt o snd o snd)) raw_defs;
haftmann@61670
    62
    val eqns = (prep o map (prep_prop deps_ctxt o snd)) raw_eqns;
haftmann@61670
    63
haftmann@61670
    64
    (*defining*)
haftmann@61670
    65
    val pre_defs = map2 (fn ((name, atts), ((b, mx), _)) => fn rhs =>
haftmann@61670
    66
      ((b, mx), ((Thm.def_binding_optional b name, atts), rhs))) raw_defs rhss;
haftmann@61672
    67
    val (defs, forked_ctxt) = fold_map Local_Theory.define pre_defs expr_ctxt;
haftmann@61672
    68
    val def_ctxt = Proof_Context.transfer (Proof_Context.theory_of forked_ctxt) expr_ctxt;
haftmann@61670
    69
    val def_eqns = defs
haftmann@61670
    70
      |> map (Thm.symmetric o
haftmann@61672
    71
        Morphism.thm (Local_Theory.standard_morphism forked_ctxt def_ctxt) o snd o snd);
haftmann@61669
    72
haftmann@61672
    73
    (*setting up proof*)
haftmann@61672
    74
    val attrss = map (apsnd (map (prep_attr def_ctxt)) o fst) raw_eqns;
haftmann@61672
    75
    val goal_ctxt = fold Variable.auto_fixes eqns def_ctxt;
haftmann@61672
    76
    val export' = Variable.export_morphism goal_ctxt def_ctxt;
haftmann@61672
    77
  in (((propss, deps, export, export'), (def_eqns, eqns, attrss)), goal_ctxt) end;
haftmann@61669
    78
haftmann@61670
    79
in
haftmann@61670
    80
haftmann@61672
    81
val cert_interpretation =
haftmann@61672
    82
  prep_interpretation Expression.cert_goal_expression (K I) (K I) (K I);
haftmann@61672
    83
haftmann@61672
    84
val read_interpretation =
haftmann@61672
    85
  prep_interpretation Expression.read_goal_expression Syntax.parse_term Syntax.parse_prop Attrib.check_src;
haftmann@61669
    86
haftmann@61670
    87
end;
haftmann@61669
    88
haftmann@61670
    89
haftmann@61673
    90
(* interpretation machinery *)
haftmann@61670
    91
haftmann@61670
    92
local
haftmann@61670
    93
haftmann@61672
    94
fun meta_rewrite eqns ctxt =
haftmann@61672
    95
  (map (Local_Defs.meta_rewrite_rule ctxt #> Drule.abs_def) (maps snd eqns), ctxt);
haftmann@61670
    96
haftmann@61670
    97
fun note_eqns_register note activate deps witss def_eqns eqns attrss export export' ctxt =
haftmann@61670
    98
  let
haftmann@61670
    99
    val facts = (Attrib.empty_binding, [(map (Morphism.thm (export' $> export)) def_eqns, [])])
haftmann@61670
   100
      :: map2 (fn attrs => fn eqn => (attrs, [([Morphism.thm (export' $> export) eqn], [])])) attrss eqns;
haftmann@61670
   101
    val (eqns', ctxt') = ctxt
haftmann@61670
   102
      |> note Thm.theoremK facts
haftmann@61670
   103
      |-> meta_rewrite;
haftmann@61670
   104
    val dep_morphs = map2 (fn (dep, morph) => fn wits =>
haftmann@61670
   105
      (dep, morph $> Element.satisfy_morphism (map (Element.transform_witness export') wits))) deps witss;
haftmann@61670
   106
    fun activate' dep_morph ctxt = activate dep_morph
haftmann@61670
   107
      (Option.map (rpair true) (Element.eq_morphism (Proof_Context.theory_of ctxt) eqns')) export ctxt;
haftmann@61670
   108
  in
haftmann@61670
   109
    ctxt'
haftmann@61670
   110
    |> fold activate' dep_morphs
haftmann@61670
   111
  end;
haftmann@61670
   112
haftmann@61670
   113
in
haftmann@61670
   114
haftmann@61670
   115
fun generic_interpretation_with_defs prep_interpretation setup_proof note add_registration
haftmann@61670
   116
    expression raw_defs raw_eqns initial_ctxt =
haftmann@61670
   117
  let
haftmann@61670
   118
    val (((propss, deps, export, export'), (def_eqns, eqns, attrss)), goal_ctxt) =
haftmann@61670
   119
      prep_interpretation expression raw_defs raw_eqns initial_ctxt;
haftmann@61670
   120
    fun after_qed witss eqns =
haftmann@61670
   121
      note_eqns_register note add_registration deps witss def_eqns eqns attrss export export';
haftmann@61670
   122
  in setup_proof after_qed propss eqns goal_ctxt end;
haftmann@61670
   123
haftmann@61672
   124
fun generic_interpretation prep_interpretation setup_proof note add_registration expression =
haftmann@61672
   125
  generic_interpretation_with_defs prep_interpretation setup_proof note add_registration expression [];
haftmann@61672
   126
haftmann@61670
   127
end;
haftmann@61670
   128
haftmann@61670
   129
haftmann@61673
   130
(** interfaces **)
haftmann@61673
   131
haftmann@61673
   132
(* interpretation in proofs *)
haftmann@61670
   133
haftmann@61672
   134
local
haftmann@61672
   135
haftmann@61669
   136
fun gen_interpret prep_interpretation expression raw_eqns int state =
haftmann@61669
   137
  let
haftmann@61669
   138
    val _ = Proof.assert_forward_or_chain state;
haftmann@61669
   139
    val ctxt = Proof.context_of state;
haftmann@61669
   140
    fun lift_after_qed after_qed witss eqns =
haftmann@61669
   141
      Proof.map_context (after_qed witss eqns) #> Proof.reset_facts;
haftmann@61669
   142
    fun setup_proof after_qed propss eqns goal_ctxt =
haftmann@61669
   143
      Element.witness_local_proof_eqs (lift_after_qed after_qed) "interpret"
haftmann@61669
   144
        propss eqns goal_ctxt int state;
haftmann@61669
   145
  in
haftmann@61669
   146
    generic_interpretation prep_interpretation setup_proof
haftmann@61669
   147
      Attrib.local_notes (Context.proof_map ooo Locale.add_registration) expression raw_eqns ctxt
haftmann@61669
   148
  end;
haftmann@61669
   149
haftmann@61673
   150
in
haftmann@61670
   151
haftmann@61673
   152
fun interpret expression = gen_interpret cert_interpretation expression;
haftmann@61673
   153
fun interpret_cmd raw_expression = gen_interpret read_interpretation raw_expression;
haftmann@61669
   154
haftmann@61673
   155
end;
haftmann@61670
   156
haftmann@61670
   157
haftmann@61673
   158
(* algebraic-view *)
haftmann@61673
   159
haftmann@61673
   160
local
haftmann@61669
   161
haftmann@61673
   162
fun gen_global_interpretation prep_interpretation expression
haftmann@61673
   163
    raw_defs raw_eqns thy = 
haftmann@61673
   164
  let
haftmann@61673
   165
    val lthy = Named_Target.theory_init thy;
haftmann@61673
   166
    fun setup_proof after_qed =
haftmann@61673
   167
      Element.witness_proof_eqs
haftmann@61673
   168
        (fn wits => fn eqs => after_qed wits eqs #> Local_Theory.exit);
haftmann@61673
   169
  in
haftmann@61673
   170
    lthy |>
haftmann@61673
   171
      generic_interpretation_with_defs prep_interpretation setup_proof
haftmann@61673
   172
        Local_Theory.notes_kind Local_Theory.subscription expression raw_defs raw_eqns
haftmann@61673
   173
  end;
haftmann@61673
   174
haftmann@61673
   175
fun gen_global_sublocale prep_loc prep_interpretation
haftmann@61673
   176
    raw_locale expression raw_defs raw_eqns thy =
haftmann@61673
   177
  let
haftmann@61673
   178
    val lthy = Named_Target.init (prep_loc thy raw_locale) thy;
haftmann@61673
   179
    fun setup_proof after_qed =
haftmann@61673
   180
      Element.witness_proof_eqs
haftmann@61673
   181
        (fn wits => fn eqs => after_qed wits eqs #> Local_Theory.exit);
haftmann@61673
   182
  in
haftmann@61673
   183
    lthy |>
haftmann@61673
   184
      generic_interpretation_with_defs prep_interpretation setup_proof
haftmann@61673
   185
        Local_Theory.notes_kind Local_Theory.subscription expression raw_defs raw_eqns
haftmann@61673
   186
  end;
haftmann@61669
   187
haftmann@61669
   188
fun subscribe_locale_only lthy =
haftmann@61669
   189
  let
haftmann@61669
   190
    val _ =
haftmann@61669
   191
      if Named_Target.is_theory lthy
haftmann@61669
   192
      then error "Not possible on level of global theory"
haftmann@61669
   193
      else ();
haftmann@61669
   194
  in Local_Theory.subscription end;
haftmann@61669
   195
haftmann@61673
   196
fun gen_sublocale prep_interpretation expression raw_defs raw_eqns lthy =
haftmann@61673
   197
  generic_interpretation_with_defs prep_interpretation Element.witness_proof_eqs
haftmann@61673
   198
    Local_Theory.notes_kind (subscribe_locale_only lthy) expression raw_defs raw_eqns lthy;
haftmann@61669
   199
haftmann@61673
   200
in
haftmann@61669
   201
haftmann@61673
   202
fun global_interpretation expression =
haftmann@61673
   203
  gen_global_interpretation cert_interpretation expression;
haftmann@61673
   204
fun global_sublocale expression =
haftmann@61673
   205
  gen_global_sublocale (K I) cert_interpretation expression;
haftmann@61673
   206
fun global_sublocale_cmd raw_expression =
haftmann@61673
   207
  gen_global_sublocale Locale.check read_interpretation raw_expression;
haftmann@61673
   208
fun sublocale expression =
haftmann@61673
   209
  gen_sublocale cert_interpretation expression;
haftmann@61673
   210
fun sublocale_cmd raw_expression =
haftmann@61673
   211
  gen_sublocale read_interpretation raw_expression;
haftmann@61673
   212
haftmann@61673
   213
end;
haftmann@61673
   214
haftmann@61673
   215
haftmann@61673
   216
(* local-theory-based view *)
haftmann@61673
   217
haftmann@61673
   218
local
haftmann@61673
   219
haftmann@61673
   220
fun gen_permanent_interpretation prep_interpretation expression raw_defs raw_eqns =
haftmann@61673
   221
  Local_Theory.assert_bottom true
haftmann@61673
   222
  #> generic_interpretation_with_defs prep_interpretation Element.witness_proof_eqs
haftmann@61673
   223
    Local_Theory.notes_kind Local_Theory.subscription expression raw_defs raw_eqns
haftmann@61669
   224
haftmann@61669
   225
in
haftmann@61669
   226
haftmann@61673
   227
fun ephemeral_interpretation expression =
haftmann@61673
   228
  generic_interpretation cert_interpretation Element.witness_proof_eqs
haftmann@61673
   229
    Local_Theory.notes_kind Locale.activate_fragment expression;
haftmann@61669
   230
haftmann@61673
   231
fun permanent_interpretation expression =
haftmann@61673
   232
  gen_permanent_interpretation cert_interpretation expression;
haftmann@61673
   233
fun permanent_interpretation_cmd raw_expression =
haftmann@61673
   234
  gen_permanent_interpretation read_interpretation raw_expression;
haftmann@61669
   235
haftmann@61673
   236
end;
haftmann@61673
   237
haftmann@61673
   238
haftmann@61673
   239
(* mixed Isar interface *)
haftmann@61673
   240
haftmann@61673
   241
local
haftmann@61670
   242
haftmann@61673
   243
fun subscribe_or_activate lthy =
haftmann@61673
   244
  if Named_Target.is_theory lthy
haftmann@61673
   245
  then Local_Theory.subscription
haftmann@61673
   246
  else Locale.activate_fragment;
haftmann@61669
   247
haftmann@61673
   248
fun gen_local_theory_interpretation prep_interpretation expression raw_eqns lthy =
haftmann@61673
   249
  generic_interpretation prep_interpretation Element.witness_proof_eqs
haftmann@61673
   250
    Local_Theory.notes_kind (subscribe_or_activate lthy) expression raw_eqns lthy;
haftmann@61669
   251
haftmann@61673
   252
in
haftmann@61673
   253
haftmann@61673
   254
fun interpretation expression =
haftmann@61673
   255
  gen_local_theory_interpretation cert_interpretation expression;
haftmann@61673
   256
fun interpretation_cmd raw_expression =
haftmann@61673
   257
  gen_local_theory_interpretation read_interpretation raw_expression;
haftmann@61669
   258
haftmann@61669
   259
end;
haftmann@61669
   260
haftmann@61669
   261
end;