src/Pure/morphism.ML
author wenzelm
Thu, 06 Jun 2024 12:42:42 +0200
changeset 80267 ea908185a597
parent 78759 461e924cc825
permissions -rw-r--r--
more operations;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21476
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/morphism.ML
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
     3
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
     4
Abstract morphisms on formal entities.
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
     5
*)
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
     6
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
     7
infix 1 $>
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
     8
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
     9
signature BASIC_MORPHISM =
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    10
sig
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    11
  type morphism
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    12
  val $> : morphism * morphism -> morphism
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    13
end
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    14
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    15
signature MORPHISM =
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    16
sig
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    17
  include BASIC_MORPHISM
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    18
  exception MORPHISM of string * exn
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
    19
  val the_theory: theory option -> theory
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
    20
  val set_context: theory -> morphism -> morphism
78063
7c9f290dff55 tuned signature;
wenzelm
parents: 78062
diff changeset
    21
  val set_context': Proof.context -> morphism -> morphism
7c9f290dff55 tuned signature;
wenzelm
parents: 78062
diff changeset
    22
  val set_context'': Context.generic -> morphism -> morphism
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
    23
  val reset_context: morphism -> morphism
67650
wenzelm
parents: 62819
diff changeset
    24
  val morphism: string ->
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
    25
   {binding: (theory option -> binding -> binding) list,
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
    26
    typ: (theory option -> typ -> typ) list,
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
    27
    term: (theory option -> term -> term) list,
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
    28
    fact: (theory option -> thm list -> thm list) list} -> morphism
77902
01d6b2a44df8 more operations;
wenzelm
parents: 74282
diff changeset
    29
  val is_identity: morphism -> bool
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
    30
  val is_empty: morphism -> bool
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    31
  val pretty: morphism -> Pretty.T
29581
b3b33e0298eb binding is alias for Binding.T
haftmann
parents: 28965
diff changeset
    32
  val binding: morphism -> binding -> binding
69062
5eda37c06f42 tuned signature;
wenzelm
parents: 67698
diff changeset
    33
  val binding_prefix: morphism -> (string * bool) list
21476
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    34
  val typ: morphism -> typ -> typ
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    35
  val term: morphism -> term -> term
21521
095f4963beed simultaneous fact morphism;
wenzelm
parents: 21492
diff changeset
    36
  val fact: morphism -> thm list -> thm list
21476
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    37
  val thm: morphism -> thm -> thm
22235
6eac7f7c3294 added cterm interface;
wenzelm
parents: 21521
diff changeset
    38
  val cterm: morphism -> cterm -> cterm
67650
wenzelm
parents: 62819
diff changeset
    39
  val identity: morphism
78060
b6c886b7184f clarified signature;
wenzelm
parents: 78058
diff changeset
    40
  val default: morphism option -> morphism
67650
wenzelm
parents: 62819
diff changeset
    41
  val compose: morphism -> morphism -> morphism
78072
001739cb8d08 clarified signature: more explicit types;
wenzelm
parents: 78064
diff changeset
    42
  type 'a entity
001739cb8d08 clarified signature: more explicit types;
wenzelm
parents: 78064
diff changeset
    43
  val entity: (morphism -> 'a) -> 'a entity
78075
15ab73949713 more careful reset/set_context for stored declarations;
wenzelm
parents: 78072
diff changeset
    44
  val entity_reset_context: 'a entity -> 'a entity
15ab73949713 more careful reset/set_context for stored declarations;
wenzelm
parents: 78072
diff changeset
    45
  val entity_set_context: theory -> 'a entity -> 'a entity
15ab73949713 more careful reset/set_context for stored declarations;
wenzelm
parents: 78072
diff changeset
    46
  val entity_set_context': Proof.context -> 'a entity -> 'a entity
15ab73949713 more careful reset/set_context for stored declarations;
wenzelm
parents: 78072
diff changeset
    47
  val entity_set_context'': Context.generic -> 'a entity -> 'a entity
78072
001739cb8d08 clarified signature: more explicit types;
wenzelm
parents: 78064
diff changeset
    48
  val transform: morphism -> 'a entity -> 'a entity
78078
35a86345de48 clarified signature;
wenzelm
parents: 78075
diff changeset
    49
  val transform_reset_context: morphism -> 'a entity -> 'a entity
78072
001739cb8d08 clarified signature: more explicit types;
wenzelm
parents: 78064
diff changeset
    50
  val form: 'a entity -> 'a
001739cb8d08 clarified signature: more explicit types;
wenzelm
parents: 78064
diff changeset
    51
  val form_entity: (morphism -> 'a) -> 'a
78112
10487f6571bc more operations;
wenzelm
parents: 78089
diff changeset
    52
  val form_context: theory -> (theory -> 'a) entity -> 'a
10487f6571bc more operations;
wenzelm
parents: 78089
diff changeset
    53
  val form_context': Proof.context -> (Proof.context -> 'a) entity -> 'a
10487f6571bc more operations;
wenzelm
parents: 78089
diff changeset
    54
  val form_context'': Context.generic -> (Context.generic -> 'a) entity -> 'a
78085
dd7bb7f99ad5 tuned signature;
wenzelm
parents: 78081
diff changeset
    55
  type declaration = morphism -> Context.generic -> Context.generic
dd7bb7f99ad5 tuned signature;
wenzelm
parents: 78081
diff changeset
    56
  type declaration_entity = (Context.generic -> Context.generic) entity
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    57
  val binding_morphism: string -> (binding -> binding) -> morphism
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
    58
  val typ_morphism': string -> (theory -> typ -> typ) -> morphism
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    59
  val typ_morphism: string -> (typ -> typ) -> morphism
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
    60
  val term_morphism': string -> (theory -> term -> term) -> morphism
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    61
  val term_morphism: string -> (term -> term) -> morphism
78081
40db83793cea more operations;
wenzelm
parents: 78078
diff changeset
    62
  val fact_morphism': string -> (theory -> thm list -> thm list) -> morphism
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    63
  val fact_morphism: string -> (thm list -> thm list) -> morphism
78081
40db83793cea more operations;
wenzelm
parents: 78078
diff changeset
    64
  val thm_morphism': string -> (theory -> thm -> thm) -> morphism
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    65
  val thm_morphism: string -> (thm -> thm) -> morphism
53087
5a1dcda7967c tuned signature;
wenzelm
parents: 45289
diff changeset
    66
  val transfer_morphism: theory -> morphism
67664
ad2b3e330c27 tuned signature;
wenzelm
parents: 67651
diff changeset
    67
  val transfer_morphism': Proof.context -> morphism
ad2b3e330c27 tuned signature;
wenzelm
parents: 67651
diff changeset
    68
  val transfer_morphism'': Context.generic -> morphism
61064
01b23bfb4947 tuned signature;
wenzelm
parents: 54740
diff changeset
    69
  val trim_context_morphism: morphism
78064
4e865c45458b clarified transfer / trim_context on persistent Token.source (e.g. attribute expressions): actually set/reset implicit context;
wenzelm
parents: 78063
diff changeset
    70
  val set_trim_context: theory -> morphism -> morphism
4e865c45458b clarified transfer / trim_context on persistent Token.source (e.g. attribute expressions): actually set/reset implicit context;
wenzelm
parents: 78063
diff changeset
    71
  val set_trim_context': Proof.context -> morphism -> morphism
4e865c45458b clarified transfer / trim_context on persistent Token.source (e.g. attribute expressions): actually set/reset implicit context;
wenzelm
parents: 78063
diff changeset
    72
  val set_trim_context'': Context.generic -> morphism -> morphism
74282
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
    73
  val instantiate_frees_morphism: ctyp TFrees.table * cterm Frees.table -> morphism
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
    74
  val instantiate_morphism: ctyp TVars.table * cterm Vars.table -> morphism
21476
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    75
end;
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    76
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    77
structure Morphism: MORPHISM =
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    78
struct
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    79
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    80
(* named functions *)
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    81
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
    82
type 'a funs = (string * (theory option -> 'a -> 'a)) list;
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    83
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    84
exception MORPHISM of string * exn;
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    85
78759
461e924cc825 proper Exn.capture / Isabelle_Thread.try_catch;
wenzelm
parents: 78112
diff changeset
    86
fun app context (name, f) x =
461e924cc825 proper Exn.capture / Isabelle_Thread.try_catch;
wenzelm
parents: 78112
diff changeset
    87
  Isabelle_Thread.try_catch (fn () => f context x)
461e924cc825 proper Exn.capture / Isabelle_Thread.try_catch;
wenzelm
parents: 78112
diff changeset
    88
    (fn exn => raise MORPHISM (name, exn));
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    89
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
    90
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
    91
(* optional context *)
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
    92
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
    93
fun the_theory (SOME thy) = thy
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
    94
  | the_theory NONE = raise Fail "Morphism lacks theory context";
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
    95
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
    96
fun join_transfer (SOME thy) = Thm.join_transfer thy
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
    97
  | join_transfer NONE = I;
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
    98
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
    99
val join_context = join_options Context.join_certificate_theory;
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
   100
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
   101
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
   102
(* type morphism *)
45289
25e9e7f527b4 slightly more explicit/syntactic modelling of morphisms;
wenzelm
parents: 37216
diff changeset
   103
21476
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
   104
datatype morphism = Morphism of
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   105
 {context: theory option,
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   106
  names: string list,
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
   107
  binding: binding funs,
45289
25e9e7f527b4 slightly more explicit/syntactic modelling of morphisms;
wenzelm
parents: 37216
diff changeset
   108
  typ: typ funs,
25e9e7f527b4 slightly more explicit/syntactic modelling of morphisms;
wenzelm
parents: 37216
diff changeset
   109
  term: term funs,
25e9e7f527b4 slightly more explicit/syntactic modelling of morphisms;
wenzelm
parents: 37216
diff changeset
   110
  fact: thm list funs};
21476
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
   111
78057
9439ae944a00 tuned signature;
wenzelm
parents: 77902
diff changeset
   112
fun rep (Morphism args) = args;
9439ae944a00 tuned signature;
wenzelm
parents: 77902
diff changeset
   113
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   114
fun apply which phi =
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   115
  let val args = rep phi
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   116
  in fold_rev (app (#context args)) (which args) end;
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   117
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   118
fun put_context context (Morphism {context = _, names, binding, typ, term, fact}) =
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   119
  Morphism {context = context, names = names, binding = binding, typ = typ, term = term, fact = fact};
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   120
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   121
val set_context = put_context o SOME;
78063
7c9f290dff55 tuned signature;
wenzelm
parents: 78062
diff changeset
   122
val set_context' = set_context o Proof_Context.theory_of;
7c9f290dff55 tuned signature;
wenzelm
parents: 78062
diff changeset
   123
val set_context'' = set_context o Context.theory_of;
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   124
val reset_context = put_context NONE;
24031
e94e541346d7 type Morphism.declaration;
wenzelm
parents: 22670
diff changeset
   125
67650
wenzelm
parents: 62819
diff changeset
   126
fun morphism a {binding, typ, term, fact} =
wenzelm
parents: 62819
diff changeset
   127
  Morphism {
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   128
    context = NONE,
67650
wenzelm
parents: 62819
diff changeset
   129
    names = if a = "" then [] else [a],
wenzelm
parents: 62819
diff changeset
   130
    binding = map (pair a) binding,
wenzelm
parents: 62819
diff changeset
   131
    typ = map (pair a) typ,
wenzelm
parents: 62819
diff changeset
   132
    term = map (pair a) term,
wenzelm
parents: 62819
diff changeset
   133
    fact = map (pair a) fact};
wenzelm
parents: 62819
diff changeset
   134
77902
01d6b2a44df8 more operations;
wenzelm
parents: 74282
diff changeset
   135
(*syntactic test only!*)
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   136
fun is_identity (Morphism {context = _, names, binding, typ, term, fact}) =
77902
01d6b2a44df8 more operations;
wenzelm
parents: 74282
diff changeset
   137
  null names andalso null binding andalso null typ andalso null term andalso null fact;
01d6b2a44df8 more operations;
wenzelm
parents: 74282
diff changeset
   138
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   139
fun is_empty phi = is_none (#context (rep phi)) andalso is_identity phi;
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   140
78057
9439ae944a00 tuned signature;
wenzelm
parents: 77902
diff changeset
   141
fun pretty phi = Pretty.enum ";" "{" "}" (map Pretty.str (rev (#names (rep phi))));
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
   142
62819
d3ff367a16a0 careful export of type-dependent functions, without losing their special status;
wenzelm
parents: 62663
diff changeset
   143
val _ = ML_system_pp (fn _ => fn _ => Pretty.to_polyml o pretty);
62663
bea354f6ff21 clarified modules;
wenzelm
parents: 62505
diff changeset
   144
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   145
val binding = apply #binding;
69062
5eda37c06f42 tuned signature;
wenzelm
parents: 67698
diff changeset
   146
fun binding_prefix morph = Binding.name "x" |> binding morph |> Binding.prefix_of;
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   147
val typ = apply #typ;
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   148
val term = apply #term;
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   149
fun fact phi = map (join_transfer (#context (rep phi))) #> apply #fact phi;
21521
095f4963beed simultaneous fact morphism;
wenzelm
parents: 21492
diff changeset
   150
val thm = singleton o fact;
22235
6eac7f7c3294 added cterm interface;
wenzelm
parents: 21521
diff changeset
   151
val cterm = Drule.cterm_rule o thm;
21476
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
   152
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
   153
67650
wenzelm
parents: 62819
diff changeset
   154
(* morphism combinators *)
21492
c73faa8e98aa added name/var/typ/term/thm_morphism;
wenzelm
parents: 21476
diff changeset
   155
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
   156
val identity = morphism "" {binding = [], typ = [], term = [], fact = []};
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
   157
78060
b6c886b7184f clarified signature;
wenzelm
parents: 78058
diff changeset
   158
val default = the_default identity;
b6c886b7184f clarified signature;
wenzelm
parents: 78058
diff changeset
   159
78057
9439ae944a00 tuned signature;
wenzelm
parents: 77902
diff changeset
   160
fun compose phi1 phi2 =
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   161
  if is_empty phi1 then phi2
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   162
  else if is_empty phi2 then phi1
78058
wenzelm
parents: 78057
diff changeset
   163
  else
wenzelm
parents: 78057
diff changeset
   164
    let
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   165
      val {context = context1, names = names1, binding = binding1,
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   166
        typ = typ1, term = term1, fact = fact1} = rep phi1;
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   167
      val {context = context2, names = names2, binding = binding2,
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   168
        typ = typ2, term = term2, fact = fact2} = rep phi2;
78058
wenzelm
parents: 78057
diff changeset
   169
    in
wenzelm
parents: 78057
diff changeset
   170
      Morphism {
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   171
        context = join_context (context1, context2),
78058
wenzelm
parents: 78057
diff changeset
   172
        names = names1 @ names2,
wenzelm
parents: 78057
diff changeset
   173
        binding = binding1 @ binding2,
wenzelm
parents: 78057
diff changeset
   174
        typ = typ1 @ typ2,
wenzelm
parents: 78057
diff changeset
   175
        term = term1 @ term2,
wenzelm
parents: 78057
diff changeset
   176
        fact = fact1 @ fact2}
wenzelm
parents: 78057
diff changeset
   177
    end;
21476
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
   178
22571
3f00e937d1c9 renamed comp to compose (avoid clash with Alice keywords);
wenzelm
parents: 22235
diff changeset
   179
fun phi1 $> phi2 = compose phi2 phi1;
21476
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
   180
78072
001739cb8d08 clarified signature: more explicit types;
wenzelm
parents: 78064
diff changeset
   181
001739cb8d08 clarified signature: more explicit types;
wenzelm
parents: 78064
diff changeset
   182
(* abstract entities *)
001739cb8d08 clarified signature: more explicit types;
wenzelm
parents: 78064
diff changeset
   183
78075
15ab73949713 more careful reset/set_context for stored declarations;
wenzelm
parents: 78072
diff changeset
   184
datatype 'a entity = Entity of (morphism -> 'a) * morphism;
15ab73949713 more careful reset/set_context for stored declarations;
wenzelm
parents: 78072
diff changeset
   185
fun entity f = Entity (f, identity);
78072
001739cb8d08 clarified signature: more explicit types;
wenzelm
parents: 78064
diff changeset
   186
78075
15ab73949713 more careful reset/set_context for stored declarations;
wenzelm
parents: 78072
diff changeset
   187
fun entity_morphism g (Entity (f, phi)) = Entity (f, g phi);
15ab73949713 more careful reset/set_context for stored declarations;
wenzelm
parents: 78072
diff changeset
   188
fun entity_reset_context a = entity_morphism reset_context a;
15ab73949713 more careful reset/set_context for stored declarations;
wenzelm
parents: 78072
diff changeset
   189
fun entity_set_context thy a = entity_morphism (set_context thy) a;
15ab73949713 more careful reset/set_context for stored declarations;
wenzelm
parents: 78072
diff changeset
   190
fun entity_set_context' ctxt a = entity_morphism (set_context' ctxt) a;
15ab73949713 more careful reset/set_context for stored declarations;
wenzelm
parents: 78072
diff changeset
   191
fun entity_set_context'' context a = entity_morphism (set_context'' context) a;
15ab73949713 more careful reset/set_context for stored declarations;
wenzelm
parents: 78072
diff changeset
   192
78089
wenzelm
parents: 78085
diff changeset
   193
fun transform phi = entity_morphism (compose phi);
wenzelm
parents: 78085
diff changeset
   194
fun transform_reset_context phi = entity_morphism (reset_context o compose phi);
78078
35a86345de48 clarified signature;
wenzelm
parents: 78075
diff changeset
   195
78075
15ab73949713 more careful reset/set_context for stored declarations;
wenzelm
parents: 78072
diff changeset
   196
fun form (Entity (f, phi)) = f phi;
78072
001739cb8d08 clarified signature: more explicit types;
wenzelm
parents: 78064
diff changeset
   197
fun form_entity f = f identity;
001739cb8d08 clarified signature: more explicit types;
wenzelm
parents: 78064
diff changeset
   198
78112
10487f6571bc more operations;
wenzelm
parents: 78089
diff changeset
   199
fun form_context thy x = form (entity_set_context thy x) thy;
10487f6571bc more operations;
wenzelm
parents: 78089
diff changeset
   200
fun form_context' ctxt x = form (entity_set_context' ctxt x) ctxt;
10487f6571bc more operations;
wenzelm
parents: 78089
diff changeset
   201
fun form_context'' context x = form (entity_set_context'' context x) context;
10487f6571bc more operations;
wenzelm
parents: 78089
diff changeset
   202
78085
dd7bb7f99ad5 tuned signature;
wenzelm
parents: 78081
diff changeset
   203
type declaration = morphism -> Context.generic -> Context.generic;
dd7bb7f99ad5 tuned signature;
wenzelm
parents: 78081
diff changeset
   204
type declaration_entity = (Context.generic -> Context.generic) entity;
22670
c803b2696ada added Morphism.transform/form (generic non-sense);
wenzelm
parents: 22571
diff changeset
   205
67650
wenzelm
parents: 62819
diff changeset
   206
wenzelm
parents: 62819
diff changeset
   207
(* concrete morphisms *)
wenzelm
parents: 62819
diff changeset
   208
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   209
fun binding_morphism a binding = morphism a {binding = [K binding], typ = [], term = [], fact = []};
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   210
fun typ_morphism' a typ = morphism a {binding = [], typ = [typ o the_theory], term = [], fact = []};
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   211
fun typ_morphism a typ = morphism a {binding = [], typ = [K typ], term = [], fact = []};
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   212
fun term_morphism' a term = morphism a {binding = [], typ = [], term = [term o the_theory], fact = []};
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   213
fun term_morphism a term = morphism a {binding = [], typ = [], term = [K term], fact = []};
78081
40db83793cea more operations;
wenzelm
parents: 78078
diff changeset
   214
fun fact_morphism' a fact = morphism a {binding = [], typ = [], term = [], fact = [fact o the_theory]};
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   215
fun fact_morphism a fact = morphism a {binding = [], typ = [], term = [], fact = [K fact]};
78081
40db83793cea more operations;
wenzelm
parents: 78078
diff changeset
   216
fun thm_morphism' a thm = morphism a {binding = [], typ = [], term = [], fact = [map o thm o the_theory]};
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   217
fun thm_morphism a thm = morphism a {binding = [], typ = [], term = [], fact = [K (map thm)]};
67650
wenzelm
parents: 62819
diff changeset
   218
78063
7c9f290dff55 tuned signature;
wenzelm
parents: 78062
diff changeset
   219
fun transfer_morphism thy = fact_morphism "transfer" I |> set_context thy;
67664
ad2b3e330c27 tuned signature;
wenzelm
parents: 67651
diff changeset
   220
val transfer_morphism' = transfer_morphism o Proof_Context.theory_of;
ad2b3e330c27 tuned signature;
wenzelm
parents: 67651
diff changeset
   221
val transfer_morphism'' = transfer_morphism o Context.theory_of;
ad2b3e330c27 tuned signature;
wenzelm
parents: 67651
diff changeset
   222
67650
wenzelm
parents: 62819
diff changeset
   223
val trim_context_morphism = thm_morphism "trim_context" Thm.trim_context;
wenzelm
parents: 62819
diff changeset
   224
78064
4e865c45458b clarified transfer / trim_context on persistent Token.source (e.g. attribute expressions): actually set/reset implicit context;
wenzelm
parents: 78063
diff changeset
   225
fun set_trim_context thy phi = set_context thy phi $> trim_context_morphism;
4e865c45458b clarified transfer / trim_context on persistent Token.source (e.g. attribute expressions): actually set/reset implicit context;
wenzelm
parents: 78063
diff changeset
   226
val set_trim_context' = set_trim_context o Proof_Context.theory_of;
4e865c45458b clarified transfer / trim_context on persistent Token.source (e.g. attribute expressions): actually set/reset implicit context;
wenzelm
parents: 78063
diff changeset
   227
val set_trim_context'' = set_trim_context o Context.theory_of;
4e865c45458b clarified transfer / trim_context on persistent Token.source (e.g. attribute expressions): actually set/reset implicit context;
wenzelm
parents: 78063
diff changeset
   228
67698
67caf783b9ee explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents: 67664
diff changeset
   229
67caf783b9ee explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents: 67664
diff changeset
   230
(* instantiate *)
67caf783b9ee explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents: 67664
diff changeset
   231
74282
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   232
fun instantiate_frees_morphism (cinstT, cinst) =
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   233
  if TFrees.is_empty cinstT andalso Frees.is_empty cinst then identity
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   234
  else
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   235
    let
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   236
      val instT = TFrees.map (K Thm.typ_of) cinstT;
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   237
      val inst = Frees.map (K Thm.term_of) cinst;
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   238
    in
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   239
      morphism "instantiate_frees"
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   240
        {binding = [],
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   241
         typ =
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   242
          if TFrees.is_empty instT then []
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   243
          else [K (Term_Subst.instantiateT_frees instT)],
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   244
         term = [K (Term_Subst.instantiate_frees (instT, inst))],
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   245
         fact = [K (map (Thm.instantiate_frees (cinstT, cinst)))]}
74282
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   246
    end;
67698
67caf783b9ee explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents: 67664
diff changeset
   247
74282
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   248
fun instantiate_morphism (cinstT, cinst) =
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   249
  if TVars.is_empty cinstT andalso Vars.is_empty cinst then identity
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   250
  else
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   251
    let
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   252
      val instT = TVars.map (K Thm.typ_of) cinstT;
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   253
      val inst = Vars.map (K Thm.term_of) cinst;
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   254
    in
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   255
      morphism "instantiate"
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   256
        {binding = [],
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   257
         typ =
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   258
          if TVars.is_empty instT then []
78062
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   259
          else [K (Term_Subst.instantiateT instT)],
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   260
         term = [K (Term_Subst.instantiate (instT, inst))],
edb195122938 support for context within morphism (for background theory);
wenzelm
parents: 78060
diff changeset
   261
         fact = [K (map (Thm.instantiate (cinstT, cinst)))]}
74282
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   262
    end;
67651
6dd41193a72a more explicit instantiate_morphism (without checks for typ / term component);
wenzelm
parents: 67650
diff changeset
   263
21476
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
   264
end;
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
   265
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 29605
diff changeset
   266
structure Basic_Morphism: BASIC_MORPHISM = Morphism;
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 29605
diff changeset
   267
open Basic_Morphism;