src/Pure/Isar/generic_target.ML
author haftmann
Thu, 29 Oct 2020 18:23:28 +0000
changeset 72516 17dc99589a91
parent 72505 974071d873ba
child 73845 bfce186331be
permissions -rw-r--r--
unified Local_Theory.init with Generic_Target.init
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41959
b460124855b8 tuned headers;
wenzelm
parents: 41228
diff changeset
     1
(*  Title:      Pure/Isar/generic_target.ML
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     2
    Author:     Makarius
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     4
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     5
Common target infrastructure.
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     6
*)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     7
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     8
signature GENERIC_TARGET =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     9
sig
60345
592806806494 completely separated canonical class abbreviations from abbreviations stemming from non-canonical morphisms -- these have no shared concept
haftmann
parents: 60344
diff changeset
    10
  (*auxiliary*)
592806806494 completely separated canonical class abbreviations from abbreviations stemming from non-canonical morphisms -- these have no shared concept
haftmann
parents: 60344
diff changeset
    11
  val export_abbrev: Proof.context ->
592806806494 completely separated canonical class abbreviations from abbreviations stemming from non-canonical morphisms -- these have no shared concept
haftmann
parents: 60344
diff changeset
    12
      (term -> term) -> term -> term * ((string * sort) list * (term list * term list))
62764
ff3b8e4079bd more PIDE markup;
wenzelm
parents: 62752
diff changeset
    13
  val check_mixfix: Proof.context -> binding * (string * sort) list -> mixfix -> mixfix
60345
592806806494 completely separated canonical class abbreviations from abbreviations stemming from non-canonical morphisms -- these have no shared concept
haftmann
parents: 60344
diff changeset
    14
  val check_mixfix_global: binding * bool -> mixfix -> mixfix
592806806494 completely separated canonical class abbreviations from abbreviations stemming from non-canonical morphisms -- these have no shared concept
haftmann
parents: 60344
diff changeset
    15
60340
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
    16
  (*background primitives*)
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
    17
  val background_foundation: ((binding * typ) * mixfix) * (binding * term) ->
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
    18
    term list * term list -> local_theory -> (term * thm) * local_theory
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
    19
  val background_declaration: declaration -> local_theory -> local_theory
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
    20
  val background_abbrev: binding * term -> term list -> local_theory -> (term * term) * local_theory
71788
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
    21
  val add_foundation_interpretation: (binding * (term * term list) -> Context.generic -> Context.generic) ->
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
    22
    theory -> theory
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
    23
60340
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
    24
  (*nested local theories primitives*)
67652
11716a084cae clarified signature;
wenzelm
parents: 66337
diff changeset
    25
  val standard_facts: local_theory -> Proof.context -> Attrib.fact list -> Attrib.fact list
11716a084cae clarified signature;
wenzelm
parents: 66337
diff changeset
    26
  val standard_notes: (int * int -> bool) -> string -> Attrib.fact list ->
63269
27d51aa2d711 tuned signature;
wenzelm
parents: 63267
diff changeset
    27
    local_theory -> local_theory
27d51aa2d711 tuned signature;
wenzelm
parents: 63267
diff changeset
    28
  val standard_declaration: (int * int -> bool) ->
27d51aa2d711 tuned signature;
wenzelm
parents: 63267
diff changeset
    29
    (morphism -> Context.generic -> Context.generic) -> local_theory -> local_theory
60340
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
    30
  val standard_const: (int * int -> bool) -> Syntax.mode -> (binding * mixfix) * term ->
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
    31
    local_theory -> local_theory
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
    32
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
    33
  (*lifting target primitives to local theory operations*)
40782
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    34
  val define: (((binding * typ) * mixfix) * (binding * term) ->
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    35
      term list * term list -> local_theory -> (term * thm) * local_theory) ->
46992
eeea81b86b70 refined Local_Theory.define vs. Local_Theory.define_internal, which allows to pass alternative name to the foundational axiom -- expecially important for 'instantiation' or 'overloading', which loose name information due to Long_Name.base_name cooking etc.;
wenzelm
parents: 46916
diff changeset
    36
    bool -> (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
40782
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    37
    (term * (string * thm)) * local_theory
47250
6523a21076a8 clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents: 47249
diff changeset
    38
  val notes:
67652
11716a084cae clarified signature;
wenzelm
parents: 66337
diff changeset
    39
    (string -> Attrib.fact list -> Attrib.fact list -> local_theory -> local_theory) ->
11716a084cae clarified signature;
wenzelm
parents: 66337
diff changeset
    40
    string -> Attrib.fact list -> local_theory -> (string * thm list) list * local_theory
60337
haftmann
parents: 60283
diff changeset
    41
  val abbrev: (Syntax.mode -> binding * mixfix -> term ->
57160
cf00d3c9db43 explicit passing of params
haftmann
parents: 57118
diff changeset
    42
      term list * term list -> local_theory -> local_theory) ->
60337
haftmann
parents: 60283
diff changeset
    43
    Syntax.mode -> (binding * mixfix) * term -> local_theory -> (term * term) * local_theory
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
    44
60341
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
    45
  (*theory target primitives*)
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
    46
  val theory_target_foundation: ((binding * typ) * mixfix) * (binding * term) ->
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
    47
     term list * term list -> local_theory -> (term * thm) * local_theory
67652
11716a084cae clarified signature;
wenzelm
parents: 66337
diff changeset
    48
  val theory_target_notes: string -> Attrib.fact list -> Attrib.fact list ->
11716a084cae clarified signature;
wenzelm
parents: 66337
diff changeset
    49
    local_theory -> local_theory
60341
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
    50
  val theory_target_abbrev: Syntax.mode -> (binding * mixfix) -> term -> term list * term list ->
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
    51
    local_theory -> local_theory
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
    52
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
    53
  (*theory target operations*)
60344
a40369ea3ba5 self-contained formulation of abbrev for named targets
haftmann
parents: 60342
diff changeset
    54
  val theory_abbrev: Syntax.mode -> (binding * mixfix) * term ->
a40369ea3ba5 self-contained formulation of abbrev for named targets
haftmann
parents: 60342
diff changeset
    55
    local_theory -> (term * term) * local_theory
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
    56
  val theory_declaration: declaration -> local_theory -> local_theory
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
    57
60341
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
    58
  (*locale target primitives*)
67652
11716a084cae clarified signature;
wenzelm
parents: 66337
diff changeset
    59
  val locale_target_notes: string -> string -> Attrib.fact list -> Attrib.fact list ->
11716a084cae clarified signature;
wenzelm
parents: 66337
diff changeset
    60
    local_theory -> local_theory
63339
fd9bd5024751 tuned whitespace;
wenzelm
parents: 63269
diff changeset
    61
  val locale_target_abbrev: string -> Syntax.mode ->
fd9bd5024751 tuned whitespace;
wenzelm
parents: 63269
diff changeset
    62
    (binding * mixfix) -> term -> term list * term list -> local_theory -> local_theory
57192
180e955711cf yet another attempt for terminology: foo_target_bar denotes an operation bar operating solely on the target context of target foo, foo_bar denotes a whole stack of operations to accomplish bar for target foo
haftmann
parents: 57191
diff changeset
    63
  val locale_target_declaration: string -> bool -> declaration -> local_theory -> local_theory
60341
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
    64
  val locale_target_const: string -> (morphism -> bool) -> Syntax.mode ->
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
    65
    (binding * mixfix) * term -> local_theory -> local_theory
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
    66
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
    67
  (*locale operations*)
60344
a40369ea3ba5 self-contained formulation of abbrev for named targets
haftmann
parents: 60342
diff changeset
    68
  val locale_abbrev: string -> Syntax.mode -> (binding * mixfix) * term ->
a40369ea3ba5 self-contained formulation of abbrev for named targets
haftmann
parents: 60342
diff changeset
    69
    local_theory -> (term * term) * local_theory
57193
d59c4383cae4 self-contained locale_declaration operation
haftmann
parents: 57192
diff changeset
    70
  val locale_declaration: string -> {syntax: bool, pervasive: bool} -> declaration ->
d59c4383cae4 self-contained locale_declaration operation
haftmann
parents: 57192
diff changeset
    71
    local_theory -> local_theory
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
    72
  val locale_const: string -> Syntax.mode -> (binding * mixfix) * term ->
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
    73
    local_theory -> local_theory
45353
wenzelm
parents: 45352
diff changeset
    74
end
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    75
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    76
structure Generic_Target: GENERIC_TARGET =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    77
struct
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    78
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
    79
(** consts **)
38312
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    80
60345
592806806494 completely separated canonical class abbreviations from abbreviations stemming from non-canonical morphisms -- these have no shared concept
haftmann
parents: 60344
diff changeset
    81
fun export_abbrev lthy preprocess rhs =
60342
df9b153d866b separate function to compute exported abbreviation
haftmann
parents: 60341
diff changeset
    82
  let
df9b153d866b separate function to compute exported abbreviation
haftmann
parents: 60341
diff changeset
    83
    val thy_ctxt = Proof_Context.init_global (Proof_Context.theory_of lthy);
df9b153d866b separate function to compute exported abbreviation
haftmann
parents: 60341
diff changeset
    84
df9b153d866b separate function to compute exported abbreviation
haftmann
parents: 60341
diff changeset
    85
    val rhs' = rhs
df9b153d866b separate function to compute exported abbreviation
haftmann
parents: 60341
diff changeset
    86
      |> Assumption.export_term lthy (Local_Theory.target_of lthy)
df9b153d866b separate function to compute exported abbreviation
haftmann
parents: 60341
diff changeset
    87
      |> preprocess;
63339
fd9bd5024751 tuned whitespace;
wenzelm
parents: 63269
diff changeset
    88
    val term_params =
fd9bd5024751 tuned whitespace;
wenzelm
parents: 63269
diff changeset
    89
      map Free (sort (Variable.fixed_ord lthy o apply2 #1) (Variable.add_fixed lthy rhs' []));
60342
df9b153d866b separate function to compute exported abbreviation
haftmann
parents: 60341
diff changeset
    90
    val u = fold_rev lambda term_params rhs';
df9b153d866b separate function to compute exported abbreviation
haftmann
parents: 60341
diff changeset
    91
    val global_rhs = singleton (Variable.polymorphic thy_ctxt) u;
df9b153d866b separate function to compute exported abbreviation
haftmann
parents: 60341
diff changeset
    92
df9b153d866b separate function to compute exported abbreviation
haftmann
parents: 60341
diff changeset
    93
    val extra_tfrees =
df9b153d866b separate function to compute exported abbreviation
haftmann
parents: 60341
diff changeset
    94
      subtract (op =) (Term.add_tfreesT (Term.fastype_of u) []) (Term.add_tfrees u []);
df9b153d866b separate function to compute exported abbreviation
haftmann
parents: 60341
diff changeset
    95
    val type_params = map (Logic.mk_type o TFree) extra_tfrees;
60345
592806806494 completely separated canonical class abbreviations from abbreviations stemming from non-canonical morphisms -- these have no shared concept
haftmann
parents: 60344
diff changeset
    96
  in (global_rhs, (extra_tfrees, (type_params, term_params))) end;
60342
df9b153d866b separate function to compute exported abbreviation
haftmann
parents: 60341
diff changeset
    97
38312
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    98
fun check_mixfix ctxt (b, extra_tfrees) mx =
62767
d6b0d35b3aed relevant check_mixfix happens further at the bottom, to avoid duplicate reports via Specification.prepare;
wenzelm
parents: 62764
diff changeset
    99
  if null extra_tfrees then mx
d6b0d35b3aed relevant check_mixfix happens further at the bottom, to avoid duplicate reports via Specification.prepare;
wenzelm
parents: 62764
diff changeset
   100
  else
d6b0d35b3aed relevant check_mixfix happens further at the bottom, to avoid duplicate reports via Specification.prepare;
wenzelm
parents: 62764
diff changeset
   101
   (if Context_Position.is_visible ctxt then
d6b0d35b3aed relevant check_mixfix happens further at the bottom, to avoid duplicate reports via Specification.prepare;
wenzelm
parents: 62764
diff changeset
   102
      warning
d6b0d35b3aed relevant check_mixfix happens further at the bottom, to avoid duplicate reports via Specification.prepare;
wenzelm
parents: 62764
diff changeset
   103
        ("Additional type variable(s) in specification of " ^ Binding.print b ^ ": " ^
d6b0d35b3aed relevant check_mixfix happens further at the bottom, to avoid duplicate reports via Specification.prepare;
wenzelm
parents: 62764
diff changeset
   104
          commas (map (Syntax.string_of_typ ctxt o TFree) (sort_by #1 extra_tfrees)) ^
d6b0d35b3aed relevant check_mixfix happens further at the bottom, to avoid duplicate reports via Specification.prepare;
wenzelm
parents: 62764
diff changeset
   105
          (if Mixfix.is_empty mx then ""
d6b0d35b3aed relevant check_mixfix happens further at the bottom, to avoid duplicate reports via Specification.prepare;
wenzelm
parents: 62764
diff changeset
   106
           else "\nDropping mixfix syntax " ^ Pretty.string_of (Mixfix.pretty_mixfix mx)))
d6b0d35b3aed relevant check_mixfix happens further at the bottom, to avoid duplicate reports via Specification.prepare;
wenzelm
parents: 62764
diff changeset
   107
    else (); NoSyn);
38312
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
   108
47291
6a641856a0e9 better drop background syntax if entity depends on parameters;
wenzelm
parents: 47290
diff changeset
   109
fun check_mixfix_global (b, no_params) mx =
62752
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 61701
diff changeset
   110
  if no_params orelse Mixfix.is_empty mx then mx
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 61701
diff changeset
   111
  else
d09d71223e7a more position information for type mixfix;
wenzelm
parents: 61701
diff changeset
   112
    (warning ("Dropping global mixfix syntax: " ^ Binding.print b ^ " " ^
62767
d6b0d35b3aed relevant check_mixfix happens further at the bottom, to avoid duplicate reports via Specification.prepare;
wenzelm
parents: 62764
diff changeset
   113
      Pretty.string_of (Mixfix.pretty_mixfix mx)); NoSyn);
47291
6a641856a0e9 better drop background syntax if entity depends on parameters;
wenzelm
parents: 47290
diff changeset
   114
61701
e89cfc004f18 Refine the supression of abbreviations for morphisms that are not identities.
ballarin
parents: 61261
diff changeset
   115
fun same_const (Const (c, _), Const (c', _)) = c = c'
e89cfc004f18 Refine the supression of abbreviations for morphisms that are not identities.
ballarin
parents: 61261
diff changeset
   116
  | same_const (t $ _, t' $ _) = same_const (t, t')
e89cfc004f18 Refine the supression of abbreviations for morphisms that are not identities.
ballarin
parents: 61261
diff changeset
   117
  | same_const (_, _) = false;
e89cfc004f18 Refine the supression of abbreviations for morphisms that are not identities.
ballarin
parents: 61261
diff changeset
   118
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   119
fun const_decl phi_pred prmode ((b, mx), rhs) phi context =
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   120
  if phi_pred phi then
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   121
    let
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   122
      val b' = Morphism.binding phi b;
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   123
      val rhs' = Morphism.term phi rhs;
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   124
      val same_shape = Term.aconv_untyped (rhs, rhs');
61701
e89cfc004f18 Refine the supression of abbreviations for morphisms that are not identities.
ballarin
parents: 61261
diff changeset
   125
      val same_stem = same_shape orelse same_const (rhs, rhs');
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   126
      val const_alias =
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   127
        if same_shape then
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   128
          (case rhs' of
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   129
            Const (c, T) =>
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   130
              let
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   131
                val thy = Context.theory_of context;
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   132
                val ctxt = Context.proof_of context;
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   133
              in
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   134
                (case Type_Infer_Context.const_type ctxt c of
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   135
                  SOME T' => if Sign.typ_equiv thy (T, T') then SOME c else NONE
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   136
                | NONE => NONE)
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   137
              end
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   138
          | _ => NONE)
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   139
        else NONE;
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   140
    in
60283
c927fa99d045 tuned whitespace;
wenzelm
parents: 59621
diff changeset
   141
      (case const_alias of
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   142
        SOME c =>
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   143
          context
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   144
          |> Context.mapping (Sign.const_alias b' c) (Proof_Context.const_alias b' c)
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   145
          |> Morphism.form (Proof_Context.generic_notation true prmode [(rhs', mx)])
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   146
      | NONE =>
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   147
          context
62764
ff3b8e4079bd more PIDE markup;
wenzelm
parents: 62752
diff changeset
   148
          |> Proof_Context.generic_add_abbrev Print_Mode.internal
ff3b8e4079bd more PIDE markup;
wenzelm
parents: 62752
diff changeset
   149
            (b', Term.close_schematic_term rhs')
61701
e89cfc004f18 Refine the supression of abbreviations for morphisms that are not identities.
ballarin
parents: 61261
diff changeset
   150
          |-> (fn (const as Const (c, _), _) => same_stem ?
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   151
                (Proof_Context.generic_revert_abbrev (#1 prmode) c #>
62764
ff3b8e4079bd more PIDE markup;
wenzelm
parents: 62752
diff changeset
   152
                 same_shape ?
ff3b8e4079bd more PIDE markup;
wenzelm
parents: 62752
diff changeset
   153
                  Morphism.form (Proof_Context.generic_notation true prmode [(const, mx)]))))
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   154
    end
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   155
  else context;
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   156
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   157
60283
c927fa99d045 tuned whitespace;
wenzelm
parents: 59621
diff changeset
   158
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   159
(** background primitives **)
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   160
71788
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   161
structure Foundation_Interpretations = Theory_Data
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   162
(
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   163
  type T = (binding * (term * term list) -> Context.generic -> Context.generic) Inttab.table;
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   164
  val empty = Inttab.empty;
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   165
  val extend = I;
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   166
  val merge = Inttab.merge (K true);
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   167
);
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   168
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   169
fun add_foundation_interpretation f =
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   170
  Foundation_Interpretations.map (Inttab.update_new (serial (), f));
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   171
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   172
fun foundation_interpretation binding_const_params lthy =
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   173
  let
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   174
    val interps = Foundation_Interpretations.get (Proof_Context.theory_of lthy);
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   175
    val interp = Inttab.fold (fn (_, f) => f binding_const_params) interps;
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   176
  in
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   177
    lthy
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   178
    |> Local_Theory.background_theory (Context.theory_map interp)
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   179
    |> Local_Theory.map_contexts (K (Context.proof_map interp))
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   180
  end;
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   181
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   182
fun background_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) lthy =
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   183
  let
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   184
    val params = type_params @ term_params;
71788
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   185
    val target_params = type_params
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   186
      @ take_prefix is_Free (Variable.export_terms lthy (Local_Theory.target_of lthy) term_params);
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   187
    val mx' = check_mixfix_global (b, null params) mx;
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   188
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   189
    val (const, lthy2) = lthy
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   190
      |> Local_Theory.background_theory_result (Sign.declare_const lthy ((b, U), mx'));
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   191
    val lhs = Term.list_comb (const, params);
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   192
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   193
    val ((_, def), lthy3) = lthy2
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   194
      |> Local_Theory.background_theory_result
61261
ddb2da7cb2e4 more explicit Defs.context: use proper name spaces as far as possible;
wenzelm
parents: 60924
diff changeset
   195
        (Thm.add_def (Proof_Context.defs_context lthy2) false false
71788
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   196
          (Thm.def_binding_optional b b_def, Logic.mk_equals (lhs, rhs)))
ca3ac5238c41 hooks for foundational terms: protection of foundational terms during simplification
haftmann
parents: 71178
diff changeset
   197
      ||> foundation_interpretation (b, (const, target_params));
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   198
  in ((lhs, def), lthy3) end;
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   199
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   200
fun background_declaration decl lthy =
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   201
  let
71178
c7d4f2ab40b9 proper theory context, e.g. for Thm.transfer;
wenzelm
parents: 70544
diff changeset
   202
    fun theory_decl context =
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   203
      Local_Theory.standard_form lthy
71178
c7d4f2ab40b9 proper theory context, e.g. for Thm.transfer;
wenzelm
parents: 70544
diff changeset
   204
        (Proof_Context.init_global (Context.theory_of context)) decl context;
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   205
  in Local_Theory.background_theory (Context.theory_map theory_decl) lthy end;
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   206
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   207
fun background_abbrev (b, global_rhs) params =
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   208
  Local_Theory.background_theory_result (Sign.add_abbrev Print_Mode.internal (b, global_rhs))
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58028
diff changeset
   209
  #>> apply2 (fn t => Term.list_comb (Logic.unvarify_global t, params))
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   210
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   211
60283
c927fa99d045 tuned whitespace;
wenzelm
parents: 59621
diff changeset
   212
60340
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
   213
(** nested local theories primitives **)
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
   214
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
   215
fun standard_facts lthy ctxt =
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
   216
  Attrib.transform_facts (Local_Theory.standard_morphism lthy ctxt);
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
   217
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
   218
fun standard_notes pred kind facts lthy =
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
   219
  Local_Theory.map_contexts (fn level => fn ctxt =>
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
   220
    if pred (Local_Theory.level lthy, level)
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
   221
    then Attrib.local_notes kind (standard_facts lthy ctxt facts) ctxt |> snd
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
   222
    else ctxt) lthy;
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
   223
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
   224
fun standard_declaration pred decl lthy =
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
   225
  Local_Theory.map_contexts (fn level => fn ctxt =>
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
   226
    if pred (Local_Theory.level lthy, level)
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
   227
    then Context.proof_map (Local_Theory.standard_form lthy ctxt decl) ctxt
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
   228
    else ctxt) lthy;
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
   229
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
   230
fun standard_const pred prmode ((b, mx), rhs) =
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
   231
  standard_declaration pred (const_decl (K true) prmode ((b, mx), rhs));
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
   232
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
   233
69394731c419 tuned order
haftmann
parents: 60337
diff changeset
   234
(** lifting target primitives to local theory operations **)
38312
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
   235
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   236
(* define *)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   237
46992
eeea81b86b70 refined Local_Theory.define vs. Local_Theory.define_internal, which allows to pass alternative name to the foundational axiom -- expecially important for 'instantiation' or 'overloading', which loose name information due to Long_Name.base_name cooking etc.;
wenzelm
parents: 46916
diff changeset
   238
fun define foundation internal ((b, mx), ((b_def, atts), rhs)) lthy =
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   239
  let
59616
eb59c6968219 tuned -- more explicit use of context;
wenzelm
parents: 59573
diff changeset
   240
    val thy_ctxt = Proof_Context.init_global (Proof_Context.theory_of lthy);
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   241
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   242
    (*term and type parameters*)
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59616
diff changeset
   243
    val ((defs, _), rhs') = Thm.cterm_of lthy rhs
47276
wenzelm
parents: 47272
diff changeset
   244
      |> Local_Defs.export_cterm lthy thy_ctxt ||> Thm.term_of;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   245
47272
ca31cfa509b1 refined define/abbrev: allow extra fixes in aux. context vs. bottom target (NB: export_term expands defined variables, leaving fixed ones);
wenzelm
parents: 47250
diff changeset
   246
    val xs = Variable.add_fixed lthy rhs' [];
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   247
    val T = Term.fastype_of rhs;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   248
    val tfreesT = Term.add_tfreesT T (fold (Term.add_tfreesT o #2) xs []);
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   249
    val extra_tfrees = rev (subtract (op =) tfreesT (Term.add_tfrees rhs []));
38312
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
   250
    val mx' = check_mixfix lthy (b, extra_tfrees) mx;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   251
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   252
    val type_params = map (Logic.mk_type o TFree) extra_tfrees;
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58028
diff changeset
   253
    val term_params = map Free (sort (Variable.fixed_ord lthy o apply2 #1) xs);
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   254
    val params = type_params @ term_params;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   255
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   256
    val U = map Term.fastype_of params ---> T;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   257
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   258
    (*foundation*)
45353
wenzelm
parents: 45352
diff changeset
   259
    val ((lhs', global_def), lthy2) = lthy
wenzelm
parents: 45352
diff changeset
   260
      |> foundation (((b, U), mx'), (b_def, rhs')) (type_params, term_params);
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   261
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   262
    (*local definition*)
63344
c9910404cc8a tuned signature;
wenzelm
parents: 63339
diff changeset
   263
    val ([(lhs, (_, local_def))], lthy3) = lthy2
63346
c8366fb67538 clarified PIDE markup;
wenzelm
parents: 63344
diff changeset
   264
      |> Context_Position.set_visible false
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63346
diff changeset
   265
      |> Local_Defs.define [((b, NoSyn), (Binding.empty_atts, lhs'))]
63346
c8366fb67538 clarified PIDE markup;
wenzelm
parents: 63344
diff changeset
   266
      ||> Context_Position.restore_visible lthy2;
47240
wenzelm
parents: 47238
diff changeset
   267
wenzelm
parents: 47238
diff changeset
   268
    (*result*)
47238
289dcbdd5984 more direct Local_Defs.contract;
wenzelm
parents: 47081
diff changeset
   269
    val def =
47240
wenzelm
parents: 47238
diff changeset
   270
      Thm.transitive local_def global_def
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59616
diff changeset
   271
      |> Local_Defs.contract lthy3 defs (Thm.cterm_of lthy3 (Logic.mk_equals (lhs, rhs)));
38315
fa3f90cf6d9c basic renumbering
haftmann
parents: 38313
diff changeset
   272
    val ([(res_name, [res])], lthy4) = lthy3
47080
wenzelm
parents: 46992
diff changeset
   273
      |> Local_Theory.notes [((if internal then Binding.empty else b_def, atts), [([def], [])])];
38315
fa3f90cf6d9c basic renumbering
haftmann
parents: 38313
diff changeset
   274
  in ((lhs, (res_name, res)), lthy4) end;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   275
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   276
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   277
(* notes *)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   278
47250
6523a21076a8 clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents: 47249
diff changeset
   279
local
6523a21076a8 clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents: 47249
diff changeset
   280
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   281
fun import_export_proof ctxt (name, raw_th) =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   282
  let
59616
eb59c6968219 tuned -- more explicit use of context;
wenzelm
parents: 59573
diff changeset
   283
    val thy_ctxt = Proof_Context.init_global (Proof_Context.theory_of ctxt);
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   284
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   285
    (*export assumes/defines*)
54883
dd04a8b654fc proper context for norm_hhf and derived operations;
wenzelm
parents: 54742
diff changeset
   286
    val th = Goal.norm_result ctxt raw_th;
45407
a83574606719 more specific treatment of defines/assumes -- avoid normalizing defs by themselves (NB: locale specifications and Local_Theory.define may lead to arbitrary mixture);
wenzelm
parents: 45390
diff changeset
   287
    val ((defs, asms), th') = Local_Defs.export ctxt thy_ctxt th;
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 52230
diff changeset
   288
    val asms' = map (rewrite_rule ctxt (Drule.norm_hhf_eqs @ defs)) asms;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   289
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   290
    (*export fixes*)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   291
    val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   292
    val frees = map Free (Thm.fold_terms Term.add_frees th' []);
45352
0b4038361a3a misc tuning;
wenzelm
parents: 45310
diff changeset
   293
    val (th'' :: vs) =
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59616
diff changeset
   294
      (th' :: map (Drule.mk_term o Thm.cterm_of ctxt) (map Logic.mk_type tfrees @ frees))
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   295
      |> Variable.export ctxt thy_ctxt
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   296
      |> Drule.zero_var_indexes_list;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   297
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   298
    (*thm definition*)
70427
973bf3e42e54 clarified signature;
wenzelm
parents: 68853
diff changeset
   299
    val result = Global_Theory.name_thm Global_Theory.official1 name th'';
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   300
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   301
    (*import fixes*)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   302
    val (tvars, vars) =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   303
      chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   304
      |>> map Logic.dest_type;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   305
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   306
    val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
60642
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60345
diff changeset
   307
    val inst =
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60345
diff changeset
   308
      map_filter
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60345
diff changeset
   309
        (fn (Var (xi, T), t) =>
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60345
diff changeset
   310
          SOME ((xi, Term_Subst.instantiateT instT T),
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60345
diff changeset
   311
            Thm.cterm_of ctxt (Term.map_types (Term_Subst.instantiateT instT) t))
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60345
diff changeset
   312
        | _ => NONE) (vars ~~ frees);
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60345
diff changeset
   313
    val result' = Thm.instantiate (map (apsnd (Thm.ctyp_of ctxt)) instT, inst) result;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   314
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   315
    (*import assumes/defines*)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   316
    val result'' =
45407
a83574606719 more specific treatment of defines/assumes -- avoid normalizing defs by themselves (NB: locale specifications and Local_Theory.define may lead to arbitrary mixture);
wenzelm
parents: 45390
diff changeset
   317
      (fold (curry op COMP) asms' result'
a83574606719 more specific treatment of defines/assumes -- avoid normalizing defs by themselves (NB: locale specifications and Local_Theory.define may lead to arbitrary mixture);
wenzelm
parents: 45390
diff changeset
   318
        handle THM _ => raise THM ("Failed to re-import result", 0, result' :: asms'))
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 52230
diff changeset
   319
      |> Local_Defs.contract ctxt defs (Thm.cprop_of th)
54883
dd04a8b654fc proper context for norm_hhf and derived operations;
wenzelm
parents: 54742
diff changeset
   320
      |> Goal.norm_result ctxt
70427
973bf3e42e54 clarified signature;
wenzelm
parents: 68853
diff changeset
   321
      |> Global_Theory.name_thm Global_Theory.unofficial2 name;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   322
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   323
  in (result'', result) end;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   324
70494
41108e3e9ca5 formal position for PThm nodes;
wenzelm
parents: 70427
diff changeset
   325
fun bind_name lthy b =
41108e3e9ca5 formal position for PThm nodes;
wenzelm
parents: 70427
diff changeset
   326
  (Local_Theory.full_name lthy b, Binding.default_pos_of b);
41108e3e9ca5 formal position for PThm nodes;
wenzelm
parents: 70427
diff changeset
   327
70544
16e98f89a29c clarified signature;
wenzelm
parents: 70494
diff changeset
   328
fun map_facts f = map (apsnd (map (apfst (map f))));
16e98f89a29c clarified signature;
wenzelm
parents: 70494
diff changeset
   329
47250
6523a21076a8 clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents: 47249
diff changeset
   330
in
6523a21076a8 clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents: 47249
diff changeset
   331
60341
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
   332
fun notes target_notes kind facts lthy =
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   333
  let
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   334
    val facts' = facts
70494
41108e3e9ca5 formal position for PThm nodes;
wenzelm
parents: 70427
diff changeset
   335
      |> map (fn (a, bs) =>
41108e3e9ca5 formal position for PThm nodes;
wenzelm
parents: 70427
diff changeset
   336
          (a, Global_Theory.burrow_fact (Global_Theory.name_multi (bind_name lthy (fst a))) bs))
70544
16e98f89a29c clarified signature;
wenzelm
parents: 70494
diff changeset
   337
      |> map_facts (import_export_proof lthy);
16e98f89a29c clarified signature;
wenzelm
parents: 70494
diff changeset
   338
    val local_facts = map_facts #1 facts';
16e98f89a29c clarified signature;
wenzelm
parents: 70494
diff changeset
   339
    val global_facts = map_facts #2 facts';
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   340
  in
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   341
    lthy
60341
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
   342
    |> target_notes kind global_facts (Attrib.partial_evaluation lthy local_facts)
47249
c0481c3c2a6c added Attrib.global_notes/local_notes/generic_notes convenience;
wenzelm
parents: 47246
diff changeset
   343
    |> Attrib.local_notes kind local_facts
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   344
  end;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   345
47250
6523a21076a8 clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents: 47249
diff changeset
   346
end;
6523a21076a8 clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents: 47249
diff changeset
   347
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   348
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   349
(* abbrev *)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   350
60341
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
   351
fun abbrev target_abbrev prmode ((b, mx), rhs) lthy =
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   352
  let
60345
592806806494 completely separated canonical class abbreviations from abbreviations stemming from non-canonical morphisms -- these have no shared concept
haftmann
parents: 60344
diff changeset
   353
    val (global_rhs, (extra_tfrees, (type_params, term_params))) = export_abbrev lthy I rhs;
38312
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
   354
    val mx' = check_mixfix lthy (b, extra_tfrees) mx;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   355
  in
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   356
    lthy
60341
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
   357
    |> target_abbrev prmode (b, mx') global_rhs (type_params, term_params)
63346
c8366fb67538 clarified PIDE markup;
wenzelm
parents: 63344
diff changeset
   358
    |> Context_Position.set_visible false
57109
84c1e0453bda tuned variable names
haftmann
parents: 57075
diff changeset
   359
    |> Proof_Context.add_abbrev Print_Mode.internal (b, rhs) |> snd
84c1e0453bda tuned variable names
haftmann
parents: 57075
diff changeset
   360
    |> Local_Defs.fixed_abbrev ((b, NoSyn), rhs)
62992
d2e3b3b159d7 clarified PIDE reports;
wenzelm
parents: 62767
diff changeset
   361
    ||> Context_Position.restore_visible lthy
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   362
  end;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   363
38341
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   364
60283
c927fa99d045 tuned whitespace;
wenzelm
parents: 59621
diff changeset
   365
60341
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
   366
(** theory target primitives **)
38341
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   367
60341
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
   368
fun theory_target_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) =
47284
0e65b6a016dc clarified background_foundation vs. theory_foundation (with const_declaration);
wenzelm
parents: 47283
diff changeset
   369
  background_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params)
57192
180e955711cf yet another attempt for terminology: foo_target_bar denotes an operation bar operating solely on the target context of target foo, foo_bar denotes a whole stack of operations to accomplish bar for target foo
haftmann
parents: 57191
diff changeset
   370
  #-> (fn (lhs, def) => standard_const (op <>) Syntax.mode_default ((b, mx), lhs)
57057
e54713f22a88 compactified level discriminator
haftmann
parents: 56294
diff changeset
   371
    #> pair (lhs, def));
47284
0e65b6a016dc clarified background_foundation vs. theory_foundation (with const_declaration);
wenzelm
parents: 47283
diff changeset
   372
60341
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
   373
fun theory_target_notes kind global_facts local_facts =
57191
haftmann
parents: 57190
diff changeset
   374
  Local_Theory.background_theory (Attrib.global_notes kind global_facts #> snd)
haftmann
parents: 57190
diff changeset
   375
  #> standard_notes (op <>) kind local_facts;
38341
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   376
60341
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
   377
fun theory_target_abbrev prmode (b, mx) global_rhs params =
47286
392c4cd97e5c more uniform theory_abbrev with const_declaration;
wenzelm
parents: 47285
diff changeset
   378
  Local_Theory.background_theory_result
57109
84c1e0453bda tuned variable names
haftmann
parents: 57075
diff changeset
   379
    (Sign.add_abbrev (#1 prmode) (b, global_rhs) #->
47291
6a641856a0e9 better drop background syntax if entity depends on parameters;
wenzelm
parents: 47290
diff changeset
   380
      (fn (lhs, _) =>  (* FIXME type_params!? *)
63339
fd9bd5024751 tuned whitespace;
wenzelm
parents: 63269
diff changeset
   381
        Sign.notation true prmode
fd9bd5024751 tuned whitespace;
wenzelm
parents: 63269
diff changeset
   382
          [(lhs, check_mixfix_global (b, null (snd params)) mx)] #> pair lhs))
62764
ff3b8e4079bd more PIDE markup;
wenzelm
parents: 62752
diff changeset
   383
  #-> (fn lhs =>
ff3b8e4079bd more PIDE markup;
wenzelm
parents: 62752
diff changeset
   384
        standard_const (op <>) prmode
ff3b8e4079bd more PIDE markup;
wenzelm
parents: 62752
diff changeset
   385
          ((b, if null (snd params) then NoSyn else mx),
ff3b8e4079bd more PIDE markup;
wenzelm
parents: 62752
diff changeset
   386
            Term.list_comb (Logic.unvarify_global lhs, snd params)));
38341
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   387
60341
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
   388
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
   389
(** theory operations **)
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
   390
60344
a40369ea3ba5 self-contained formulation of abbrev for named targets
haftmann
parents: 60342
diff changeset
   391
val theory_abbrev = abbrev theory_target_abbrev;
a40369ea3ba5 self-contained formulation of abbrev for named targets
haftmann
parents: 60342
diff changeset
   392
60341
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
   393
fun theory_declaration decl =
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
   394
  background_declaration decl #> standard_declaration (K true) decl;
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
   395
60283
c927fa99d045 tuned whitespace;
wenzelm
parents: 59621
diff changeset
   396
60341
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
   397
(** locale target primitives **)
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   398
60341
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
   399
fun locale_target_notes locale kind global_facts local_facts =
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   400
  Local_Theory.background_theory
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   401
    (Attrib.global_notes kind (Attrib.map_facts (K []) global_facts) #> snd) #>
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   402
  (fn lthy => lthy |>
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   403
    Local_Theory.target (fn ctxt => ctxt |>
67654
49f45fcd335b clarified signature;
wenzelm
parents: 67652
diff changeset
   404
      Locale.add_facts locale kind (standard_facts lthy ctxt local_facts))) #>
57191
haftmann
parents: 57190
diff changeset
   405
  standard_notes (fn (this, other) => other <> 0 andalso this <> other) kind local_facts;
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   406
57192
180e955711cf yet another attempt for terminology: foo_target_bar denotes an operation bar operating solely on the target context of target foo, foo_bar denotes a whole stack of operations to accomplish bar for target foo
haftmann
parents: 57191
diff changeset
   407
fun locale_target_declaration locale syntax decl lthy = lthy
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   408
  |> Local_Theory.target (fn ctxt => ctxt |>
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   409
    Locale.add_declaration locale syntax
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   410
      (Morphism.transform (Local_Theory.standard_morphism lthy ctxt) decl));
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   411
60341
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
   412
fun locale_target_const locale phi_pred prmode ((b, mx), rhs) =
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
   413
  locale_target_declaration locale true (const_decl phi_pred prmode ((b, mx), rhs))
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
   414
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
   415
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
   416
(** locale operations **)
fcbd7f0c52c3 clearly separated target primitives (target_foo) from self-contained target operations (foo)
haftmann
parents: 60340
diff changeset
   417
57193
d59c4383cae4 self-contained locale_declaration operation
haftmann
parents: 57192
diff changeset
   418
fun locale_declaration locale {syntax, pervasive} decl =
d59c4383cae4 self-contained locale_declaration operation
haftmann
parents: 57192
diff changeset
   419
  pervasive ? background_declaration decl
d59c4383cae4 self-contained locale_declaration operation
haftmann
parents: 57192
diff changeset
   420
  #> locale_target_declaration locale syntax decl
d59c4383cae4 self-contained locale_declaration operation
haftmann
parents: 57192
diff changeset
   421
  #> standard_declaration (fn (_, other) => other <> 0) decl;
d59c4383cae4 self-contained locale_declaration operation
haftmann
parents: 57192
diff changeset
   422
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   423
fun locale_const locale prmode ((b, mx), rhs) =
57192
180e955711cf yet another attempt for terminology: foo_target_bar denotes an operation bar operating solely on the target context of target foo, foo_bar denotes a whole stack of operations to accomplish bar for target foo
haftmann
parents: 57191
diff changeset
   424
  locale_target_const locale (K true) prmode ((b, mx), rhs)
180e955711cf yet another attempt for terminology: foo_target_bar denotes an operation bar operating solely on the target context of target foo, foo_bar denotes a whole stack of operations to accomplish bar for target foo
haftmann
parents: 57191
diff changeset
   425
  #> standard_const (fn (this, other) => other <> 0 andalso this <> other) prmode ((b, mx), rhs);
57190
05ad9aae4537 recovered structure of module, which got somehow convoluted due to incremental modifications
haftmann
parents: 57189
diff changeset
   426
60344
a40369ea3ba5 self-contained formulation of abbrev for named targets
haftmann
parents: 60342
diff changeset
   427
a40369ea3ba5 self-contained formulation of abbrev for named targets
haftmann
parents: 60342
diff changeset
   428
(** locale abbreviations **)
a40369ea3ba5 self-contained formulation of abbrev for named targets
haftmann
parents: 60342
diff changeset
   429
a40369ea3ba5 self-contained formulation of abbrev for named targets
haftmann
parents: 60342
diff changeset
   430
fun locale_target_abbrev locale prmode (b, mx) global_rhs params =
a40369ea3ba5 self-contained formulation of abbrev for named targets
haftmann
parents: 60342
diff changeset
   431
  background_abbrev (b, global_rhs) (snd params)
a40369ea3ba5 self-contained formulation of abbrev for named targets
haftmann
parents: 60342
diff changeset
   432
  #-> (fn (lhs, _) => locale_const locale prmode ((b, mx), lhs));
a40369ea3ba5 self-contained formulation of abbrev for named targets
haftmann
parents: 60342
diff changeset
   433
a40369ea3ba5 self-contained formulation of abbrev for named targets
haftmann
parents: 60342
diff changeset
   434
fun locale_abbrev locale = abbrev (locale_target_abbrev locale);
a40369ea3ba5 self-contained formulation of abbrev for named targets
haftmann
parents: 60342
diff changeset
   435
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   436
end;