src/Pure/morphism.ML
author wenzelm
Fri, 21 Apr 2023 18:53:25 +0200
changeset 77902 01d6b2a44df8
parent 74282 c2ee8d993d6a
child 78057 9439ae944a00
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
24031
e94e541346d7 type Morphism.declaration;
wenzelm
parents: 22670
diff changeset
    12
  type declaration = morphism -> Context.generic -> Context.generic
21476
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    13
  val $> : morphism * morphism -> morphism
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    14
end
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    15
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    16
signature MORPHISM =
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    17
sig
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    18
  include BASIC_MORPHISM
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    19
  exception MORPHISM of string * exn
67650
wenzelm
parents: 62819
diff changeset
    20
  val morphism: string ->
wenzelm
parents: 62819
diff changeset
    21
   {binding: (binding -> binding) list,
wenzelm
parents: 62819
diff changeset
    22
    typ: (typ -> typ) list,
wenzelm
parents: 62819
diff changeset
    23
    term: (term -> term) list,
wenzelm
parents: 62819
diff changeset
    24
    fact: (thm list -> thm list) list} -> morphism
77902
01d6b2a44df8 more operations;
wenzelm
parents: 74282
diff changeset
    25
  val is_identity: morphism -> bool
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    26
  val pretty: morphism -> Pretty.T
29581
b3b33e0298eb binding is alias for Binding.T
haftmann
parents: 28965
diff changeset
    27
  val binding: morphism -> binding -> binding
69062
5eda37c06f42 tuned signature;
wenzelm
parents: 67698
diff changeset
    28
  val binding_prefix: morphism -> (string * bool) list
21476
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    29
  val typ: morphism -> typ -> typ
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    30
  val term: morphism -> term -> term
21521
095f4963beed simultaneous fact morphism;
wenzelm
parents: 21492
diff changeset
    31
  val fact: morphism -> thm list -> thm list
21476
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    32
  val thm: morphism -> thm -> thm
22235
6eac7f7c3294 added cterm interface;
wenzelm
parents: 21521
diff changeset
    33
  val cterm: morphism -> cterm -> cterm
67650
wenzelm
parents: 62819
diff changeset
    34
  val identity: morphism
wenzelm
parents: 62819
diff changeset
    35
  val compose: morphism -> morphism -> morphism
wenzelm
parents: 62819
diff changeset
    36
  val transform: morphism -> (morphism -> 'a) -> morphism -> 'a
wenzelm
parents: 62819
diff changeset
    37
  val form: (morphism -> 'a) -> 'a
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    38
  val binding_morphism: string -> (binding -> binding) -> morphism
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    39
  val typ_morphism: string -> (typ -> typ) -> morphism
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    40
  val term_morphism: string -> (term -> term) -> morphism
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    41
  val fact_morphism: string -> (thm list -> thm list) -> morphism
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    42
  val thm_morphism: string -> (thm -> thm) -> morphism
53087
5a1dcda7967c tuned signature;
wenzelm
parents: 45289
diff changeset
    43
  val transfer_morphism: theory -> morphism
67664
ad2b3e330c27 tuned signature;
wenzelm
parents: 67651
diff changeset
    44
  val transfer_morphism': Proof.context -> morphism
ad2b3e330c27 tuned signature;
wenzelm
parents: 67651
diff changeset
    45
  val transfer_morphism'': Context.generic -> morphism
61064
01b23bfb4947 tuned signature;
wenzelm
parents: 54740
diff changeset
    46
  val trim_context_morphism: morphism
74282
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
    47
  val instantiate_frees_morphism: ctyp TFrees.table * cterm Frees.table -> morphism
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
    48
  val instantiate_morphism: ctyp TVars.table * cterm Vars.table -> morphism
21476
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    49
end;
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    50
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    51
structure Morphism: MORPHISM =
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    52
struct
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    53
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    54
(* named functions *)
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    55
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    56
type 'a funs = (string * ('a -> 'a)) list;
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    57
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    58
exception MORPHISM of string * exn;
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    59
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    60
fun app (name, f) x = f x
62505
9e2a65912111 clarified modules;
wenzelm
parents: 61064
diff changeset
    61
  handle exn =>
9e2a65912111 clarified modules;
wenzelm
parents: 61064
diff changeset
    62
    if Exn.is_interrupt exn then Exn.reraise exn else raise MORPHISM (name, exn);
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    63
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    64
fun apply fs = fold_rev app fs;
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    65
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    66
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    67
(* type morphism *)
45289
25e9e7f527b4 slightly more explicit/syntactic modelling of morphisms;
wenzelm
parents: 37216
diff changeset
    68
21476
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    69
datatype morphism = Morphism of
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    70
 {names: string list,
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    71
  binding: binding funs,
45289
25e9e7f527b4 slightly more explicit/syntactic modelling of morphisms;
wenzelm
parents: 37216
diff changeset
    72
  typ: typ funs,
25e9e7f527b4 slightly more explicit/syntactic modelling of morphisms;
wenzelm
parents: 37216
diff changeset
    73
  term: term funs,
25e9e7f527b4 slightly more explicit/syntactic modelling of morphisms;
wenzelm
parents: 37216
diff changeset
    74
  fact: thm list funs};
21476
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
    75
24031
e94e541346d7 type Morphism.declaration;
wenzelm
parents: 22670
diff changeset
    76
type declaration = morphism -> Context.generic -> Context.generic;
e94e541346d7 type Morphism.declaration;
wenzelm
parents: 22670
diff changeset
    77
67650
wenzelm
parents: 62819
diff changeset
    78
fun morphism a {binding, typ, term, fact} =
wenzelm
parents: 62819
diff changeset
    79
  Morphism {
wenzelm
parents: 62819
diff changeset
    80
    names = if a = "" then [] else [a],
wenzelm
parents: 62819
diff changeset
    81
    binding = map (pair a) binding,
wenzelm
parents: 62819
diff changeset
    82
    typ = map (pair a) typ,
wenzelm
parents: 62819
diff changeset
    83
    term = map (pair a) term,
wenzelm
parents: 62819
diff changeset
    84
    fact = map (pair a) fact};
wenzelm
parents: 62819
diff changeset
    85
77902
01d6b2a44df8 more operations;
wenzelm
parents: 74282
diff changeset
    86
(*syntactic test only!*)
01d6b2a44df8 more operations;
wenzelm
parents: 74282
diff changeset
    87
fun is_identity (Morphism {names, binding, typ, term, fact}) =
01d6b2a44df8 more operations;
wenzelm
parents: 74282
diff changeset
    88
  null names andalso null binding andalso null typ andalso null term andalso null fact;
01d6b2a44df8 more operations;
wenzelm
parents: 74282
diff changeset
    89
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    90
fun pretty (Morphism {names, ...}) = Pretty.enum ";" "{" "}" (map Pretty.str (rev names));
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
    91
62819
d3ff367a16a0 careful export of type-dependent functions, without losing their special status;
wenzelm
parents: 62663
diff changeset
    92
val _ = ML_system_pp (fn _ => fn _ => Pretty.to_polyml o pretty);
62663
bea354f6ff21 clarified modules;
wenzelm
parents: 62505
diff changeset
    93
45289
25e9e7f527b4 slightly more explicit/syntactic modelling of morphisms;
wenzelm
parents: 37216
diff changeset
    94
fun binding (Morphism {binding, ...}) = apply binding;
69062
5eda37c06f42 tuned signature;
wenzelm
parents: 67698
diff changeset
    95
fun binding_prefix morph = Binding.name "x" |> binding morph |> Binding.prefix_of;
45289
25e9e7f527b4 slightly more explicit/syntactic modelling of morphisms;
wenzelm
parents: 37216
diff changeset
    96
fun typ (Morphism {typ, ...}) = apply typ;
25e9e7f527b4 slightly more explicit/syntactic modelling of morphisms;
wenzelm
parents: 37216
diff changeset
    97
fun term (Morphism {term, ...}) = apply term;
25e9e7f527b4 slightly more explicit/syntactic modelling of morphisms;
wenzelm
parents: 37216
diff changeset
    98
fun fact (Morphism {fact, ...}) = apply fact;
21521
095f4963beed simultaneous fact morphism;
wenzelm
parents: 21492
diff changeset
    99
val thm = singleton o fact;
22235
6eac7f7c3294 added cterm interface;
wenzelm
parents: 21521
diff changeset
   100
val cterm = Drule.cterm_rule o thm;
21476
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
   101
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
   102
67650
wenzelm
parents: 62819
diff changeset
   103
(* morphism combinators *)
21492
c73faa8e98aa added name/var/typ/term/thm_morphism;
wenzelm
parents: 21476
diff changeset
   104
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
   105
val identity = morphism "" {binding = [], typ = [], term = [], fact = []};
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
   106
22571
3f00e937d1c9 renamed comp to compose (avoid clash with Alice keywords);
wenzelm
parents: 22235
diff changeset
   107
fun compose
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
   108
    (Morphism {names = names1, binding = binding1, typ = typ1, term = term1, fact = fact1})
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
   109
    (Morphism {names = names2, binding = binding2, typ = typ2, term = term2, fact = fact2}) =
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
   110
  Morphism {
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
   111
    names = names1 @ names2,
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
   112
    binding = binding1 @ binding2,
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
   113
    typ = typ1 @ typ2,
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
   114
    term = term1 @ term2,
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 53087
diff changeset
   115
    fact = fact1 @ fact2};
21476
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
   116
22571
3f00e937d1c9 renamed comp to compose (avoid clash with Alice keywords);
wenzelm
parents: 22235
diff changeset
   117
fun phi1 $> phi2 = compose phi2 phi1;
21476
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
   118
22670
c803b2696ada added Morphism.transform/form (generic non-sense);
wenzelm
parents: 22571
diff changeset
   119
fun transform phi f = fn psi => f (phi $> psi);
c803b2696ada added Morphism.transform/form (generic non-sense);
wenzelm
parents: 22571
diff changeset
   120
fun form f = f identity;
c803b2696ada added Morphism.transform/form (generic non-sense);
wenzelm
parents: 22571
diff changeset
   121
67650
wenzelm
parents: 62819
diff changeset
   122
wenzelm
parents: 62819
diff changeset
   123
(* concrete morphisms *)
wenzelm
parents: 62819
diff changeset
   124
wenzelm
parents: 62819
diff changeset
   125
fun binding_morphism a binding = morphism a {binding = [binding], typ = [], term = [], fact = []};
wenzelm
parents: 62819
diff changeset
   126
fun typ_morphism a typ = morphism a {binding = [], typ = [typ], term = [], fact = []};
wenzelm
parents: 62819
diff changeset
   127
fun term_morphism a term = morphism a {binding = [], typ = [], term = [term], fact = []};
wenzelm
parents: 62819
diff changeset
   128
fun fact_morphism a fact = morphism a {binding = [], typ = [], term = [], fact = [fact]};
wenzelm
parents: 62819
diff changeset
   129
fun thm_morphism a thm = morphism a {binding = [], typ = [], term = [], fact = [map thm]};
wenzelm
parents: 62819
diff changeset
   130
70310
c82f59c47aaf clarified transfer_morphism: implicit join_certificate, e.g. relevant for complex cascades of morphisms such as class locale interpretation;
wenzelm
parents: 69062
diff changeset
   131
val transfer_morphism = thm_morphism "transfer" o Thm.join_transfer;
67664
ad2b3e330c27 tuned signature;
wenzelm
parents: 67651
diff changeset
   132
val transfer_morphism' = transfer_morphism o Proof_Context.theory_of;
ad2b3e330c27 tuned signature;
wenzelm
parents: 67651
diff changeset
   133
val transfer_morphism'' = transfer_morphism o Context.theory_of;
ad2b3e330c27 tuned signature;
wenzelm
parents: 67651
diff changeset
   134
67650
wenzelm
parents: 62819
diff changeset
   135
val trim_context_morphism = thm_morphism "trim_context" Thm.trim_context;
wenzelm
parents: 62819
diff changeset
   136
67698
67caf783b9ee explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents: 67664
diff changeset
   137
67caf783b9ee explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents: 67664
diff changeset
   138
(* instantiate *)
67caf783b9ee explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents: 67664
diff changeset
   139
74282
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   140
fun instantiate_frees_morphism (cinstT, cinst) =
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   141
  if TFrees.is_empty cinstT andalso Frees.is_empty cinst then identity
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   142
  else
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   143
    let
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   144
      val instT = TFrees.map (K Thm.typ_of) cinstT;
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   145
      val inst = Frees.map (K Thm.term_of) cinst;
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   146
    in
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   147
      morphism "instantiate_frees"
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   148
        {binding = [],
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   149
         typ =
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   150
          if TFrees.is_empty instT then []
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   151
          else [Term_Subst.instantiateT_frees instT],
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   152
         term = [Term_Subst.instantiate_frees (instT, inst)],
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   153
         fact = [map (Thm.instantiate_frees (cinstT, cinst))]}
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   154
    end;
67698
67caf783b9ee explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents: 67664
diff changeset
   155
74282
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   156
fun instantiate_morphism (cinstT, cinst) =
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   157
  if TVars.is_empty cinstT andalso Vars.is_empty cinst then identity
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   158
  else
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   159
    let
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   160
      val instT = TVars.map (K Thm.typ_of) cinstT;
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   161
      val inst = Vars.map (K Thm.term_of) cinst;
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   162
    in
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   163
      morphism "instantiate"
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   164
        {binding = [],
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   165
         typ =
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   166
          if TVars.is_empty instT then []
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   167
          else [Term_Subst.instantiateT instT],
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   168
         term = [Term_Subst.instantiate (instT, inst)],
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   169
         fact = [map (Thm.instantiate (cinstT, cinst))]}
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74266
diff changeset
   170
    end;
67651
6dd41193a72a more explicit instantiate_morphism (without checks for typ / term component);
wenzelm
parents: 67650
diff changeset
   171
21476
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
   172
end;
4677b7b84247 Abstract morphisms on formal entities.
wenzelm
parents:
diff changeset
   173
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 29605
diff changeset
   174
structure Basic_Morphism: BASIC_MORPHISM = Morphism;
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 29605
diff changeset
   175
open Basic_Morphism;