author | wenzelm |
Tue, 16 May 2023 17:08:31 +0200 | |
changeset 78064 | 4e865c45458b |
parent 78063 | 7c9f290dff55 |
child 78072 | 001739cb8d08 |
permissions | -rw-r--r-- |
21476 | 1 |
(* Title: Pure/morphism.ML |
2 |
Author: Makarius |
|
3 |
||
4 |
Abstract morphisms on formal entities. |
|
5 |
*) |
|
6 |
||
7 |
infix 1 $> |
|
8 |
||
9 |
signature BASIC_MORPHISM = |
|
10 |
sig |
|
11 |
type morphism |
|
24031 | 12 |
type declaration = morphism -> Context.generic -> Context.generic |
21476 | 13 |
val $> : morphism * morphism -> morphism |
14 |
end |
|
15 |
||
16 |
signature MORPHISM = |
|
17 |
sig |
|
18 |
include BASIC_MORPHISM |
|
54740 | 19 |
exception MORPHISM of string * exn |
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
20 |
val the_theory: theory option -> theory |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
21 |
val set_context: theory -> morphism -> morphism |
78063 | 22 |
val set_context': Proof.context -> morphism -> morphism |
23 |
val set_context'': Context.generic -> morphism -> morphism |
|
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
24 |
val reset_context: morphism -> morphism |
67650 | 25 |
val morphism: string -> |
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
26 |
{binding: (theory option -> binding -> binding) list, |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
27 |
typ: (theory option -> typ -> typ) list, |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
28 |
term: (theory option -> term -> term) list, |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
29 |
fact: (theory option -> thm list -> thm list) list} -> morphism |
77902 | 30 |
val is_identity: morphism -> bool |
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
31 |
val is_empty: morphism -> bool |
54740 | 32 |
val pretty: morphism -> Pretty.T |
29581 | 33 |
val binding: morphism -> binding -> binding |
69062 | 34 |
val binding_prefix: morphism -> (string * bool) list |
21476 | 35 |
val typ: morphism -> typ -> typ |
36 |
val term: morphism -> term -> term |
|
21521 | 37 |
val fact: morphism -> thm list -> thm list |
21476 | 38 |
val thm: morphism -> thm -> thm |
22235 | 39 |
val cterm: morphism -> cterm -> cterm |
67650 | 40 |
val identity: morphism |
78060 | 41 |
val default: morphism option -> morphism |
67650 | 42 |
val compose: morphism -> morphism -> morphism |
43 |
val transform: morphism -> (morphism -> 'a) -> morphism -> 'a |
|
44 |
val form: (morphism -> 'a) -> 'a |
|
54740 | 45 |
val binding_morphism: string -> (binding -> binding) -> morphism |
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
46 |
val typ_morphism': string -> (theory -> typ -> typ) -> morphism |
54740 | 47 |
val typ_morphism: string -> (typ -> typ) -> morphism |
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
48 |
val term_morphism': string -> (theory -> term -> term) -> morphism |
54740 | 49 |
val term_morphism: string -> (term -> term) -> morphism |
50 |
val fact_morphism: string -> (thm list -> thm list) -> morphism |
|
51 |
val thm_morphism: string -> (thm -> thm) -> morphism |
|
53087 | 52 |
val transfer_morphism: theory -> morphism |
67664 | 53 |
val transfer_morphism': Proof.context -> morphism |
54 |
val transfer_morphism'': Context.generic -> morphism |
|
61064 | 55 |
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
|
56 |
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
|
57 |
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
|
58 |
val set_trim_context'': Context.generic -> morphism -> morphism |
74282 | 59 |
val instantiate_frees_morphism: ctyp TFrees.table * cterm Frees.table -> morphism |
60 |
val instantiate_morphism: ctyp TVars.table * cterm Vars.table -> morphism |
|
21476 | 61 |
end; |
62 |
||
63 |
structure Morphism: MORPHISM = |
|
64 |
struct |
|
65 |
||
54740 | 66 |
(* named functions *) |
67 |
||
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
68 |
type 'a funs = (string * (theory option -> 'a -> 'a)) list; |
54740 | 69 |
|
70 |
exception MORPHISM of string * exn; |
|
71 |
||
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
72 |
fun app context (name, f) x = f context x |
62505 | 73 |
handle exn => |
74 |
if Exn.is_interrupt exn then Exn.reraise exn else raise MORPHISM (name, exn); |
|
54740 | 75 |
|
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
76 |
|
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
77 |
(* optional context *) |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
78 |
|
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
79 |
fun the_theory (SOME thy) = thy |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
80 |
| the_theory NONE = raise Fail "Morphism lacks theory context"; |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
81 |
|
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
82 |
fun join_transfer (SOME thy) = Thm.join_transfer thy |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
83 |
| join_transfer NONE = I; |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
84 |
|
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
85 |
val join_context = join_options Context.join_certificate_theory; |
54740 | 86 |
|
87 |
||
88 |
(* type morphism *) |
|
45289
25e9e7f527b4
slightly more explicit/syntactic modelling of morphisms;
wenzelm
parents:
37216
diff
changeset
|
89 |
|
21476 | 90 |
datatype morphism = Morphism of |
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
91 |
{context: theory option, |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
92 |
names: string list, |
54740 | 93 |
binding: binding funs, |
45289
25e9e7f527b4
slightly more explicit/syntactic modelling of morphisms;
wenzelm
parents:
37216
diff
changeset
|
94 |
typ: typ funs, |
25e9e7f527b4
slightly more explicit/syntactic modelling of morphisms;
wenzelm
parents:
37216
diff
changeset
|
95 |
term: term funs, |
25e9e7f527b4
slightly more explicit/syntactic modelling of morphisms;
wenzelm
parents:
37216
diff
changeset
|
96 |
fact: thm list funs}; |
21476 | 97 |
|
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
98 |
type declaration = morphism -> Context.generic -> Context.generic; |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
99 |
|
78057 | 100 |
fun rep (Morphism args) = args; |
101 |
||
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
102 |
fun apply which phi = |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
103 |
let val args = rep phi |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
104 |
in fold_rev (app (#context args)) (which args) end; |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
105 |
|
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
106 |
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
|
107 |
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
|
108 |
|
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
109 |
val set_context = put_context o SOME; |
78063 | 110 |
val set_context' = set_context o Proof_Context.theory_of; |
111 |
val set_context'' = set_context o Context.theory_of; |
|
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
112 |
val reset_context = put_context NONE; |
24031 | 113 |
|
67650 | 114 |
fun morphism a {binding, typ, term, fact} = |
115 |
Morphism { |
|
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
116 |
context = NONE, |
67650 | 117 |
names = if a = "" then [] else [a], |
118 |
binding = map (pair a) binding, |
|
119 |
typ = map (pair a) typ, |
|
120 |
term = map (pair a) term, |
|
121 |
fact = map (pair a) fact}; |
|
122 |
||
77902 | 123 |
(*syntactic test only!*) |
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
124 |
fun is_identity (Morphism {context = _, names, binding, typ, term, fact}) = |
77902 | 125 |
null names andalso null binding andalso null typ andalso null term andalso null fact; |
126 |
||
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
127 |
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
|
128 |
|
78057 | 129 |
fun pretty phi = Pretty.enum ";" "{" "}" (map Pretty.str (rev (#names (rep phi)))); |
54740 | 130 |
|
62819
d3ff367a16a0
careful export of type-dependent functions, without losing their special status;
wenzelm
parents:
62663
diff
changeset
|
131 |
val _ = ML_system_pp (fn _ => fn _ => Pretty.to_polyml o pretty); |
62663 | 132 |
|
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
133 |
val binding = apply #binding; |
69062 | 134 |
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
|
135 |
val typ = apply #typ; |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
136 |
val term = apply #term; |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
137 |
fun fact phi = map (join_transfer (#context (rep phi))) #> apply #fact phi; |
21521 | 138 |
val thm = singleton o fact; |
22235 | 139 |
val cterm = Drule.cterm_rule o thm; |
21476 | 140 |
|
54740 | 141 |
|
67650 | 142 |
(* morphism combinators *) |
21492 | 143 |
|
54740 | 144 |
val identity = morphism "" {binding = [], typ = [], term = [], fact = []}; |
145 |
||
78060 | 146 |
val default = the_default identity; |
147 |
||
78057 | 148 |
fun compose phi1 phi2 = |
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
149 |
if is_empty phi1 then phi2 |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
150 |
else if is_empty phi2 then phi1 |
78058 | 151 |
else |
152 |
let |
|
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
153 |
val {context = context1, names = names1, binding = binding1, |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
154 |
typ = typ1, term = term1, fact = fact1} = rep phi1; |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
155 |
val {context = context2, names = names2, binding = binding2, |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
156 |
typ = typ2, term = term2, fact = fact2} = rep phi2; |
78058 | 157 |
in |
158 |
Morphism { |
|
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
159 |
context = join_context (context1, context2), |
78058 | 160 |
names = names1 @ names2, |
161 |
binding = binding1 @ binding2, |
|
162 |
typ = typ1 @ typ2, |
|
163 |
term = term1 @ term2, |
|
164 |
fact = fact1 @ fact2} |
|
165 |
end; |
|
21476 | 166 |
|
22571
3f00e937d1c9
renamed comp to compose (avoid clash with Alice keywords);
wenzelm
parents:
22235
diff
changeset
|
167 |
fun phi1 $> phi2 = compose phi2 phi1; |
21476 | 168 |
|
22670
c803b2696ada
added Morphism.transform/form (generic non-sense);
wenzelm
parents:
22571
diff
changeset
|
169 |
fun transform phi f = fn psi => f (phi $> psi); |
c803b2696ada
added Morphism.transform/form (generic non-sense);
wenzelm
parents:
22571
diff
changeset
|
170 |
fun form f = f identity; |
c803b2696ada
added Morphism.transform/form (generic non-sense);
wenzelm
parents:
22571
diff
changeset
|
171 |
|
67650 | 172 |
|
173 |
(* concrete morphisms *) |
|
174 |
||
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
175 |
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
|
176 |
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
|
177 |
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
|
178 |
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
|
179 |
fun term_morphism a term = morphism a {binding = [], typ = [], term = [K term], fact = []}; |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
180 |
fun fact_morphism a fact = morphism a {binding = [], typ = [], term = [], fact = [K fact]}; |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
181 |
fun thm_morphism a thm = morphism a {binding = [], typ = [], term = [], fact = [K (map thm)]}; |
67650 | 182 |
|
78063 | 183 |
fun transfer_morphism thy = fact_morphism "transfer" I |> set_context thy; |
67664 | 184 |
val transfer_morphism' = transfer_morphism o Proof_Context.theory_of; |
185 |
val transfer_morphism'' = transfer_morphism o Context.theory_of; |
|
186 |
||
67650 | 187 |
val trim_context_morphism = thm_morphism "trim_context" Thm.trim_context; |
188 |
||
78064
4e865c45458b
clarified transfer / trim_context on persistent Token.source (e.g. attribute expressions): actually set/reset implicit context;
wenzelm
parents:
78063
diff
changeset
|
189 |
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
|
190 |
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
|
191 |
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
|
192 |
|
67698
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
67664
diff
changeset
|
193 |
|
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
67664
diff
changeset
|
194 |
(* instantiate *) |
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
67664
diff
changeset
|
195 |
|
74282 | 196 |
fun instantiate_frees_morphism (cinstT, cinst) = |
197 |
if TFrees.is_empty cinstT andalso Frees.is_empty cinst then identity |
|
198 |
else |
|
199 |
let |
|
200 |
val instT = TFrees.map (K Thm.typ_of) cinstT; |
|
201 |
val inst = Frees.map (K Thm.term_of) cinst; |
|
202 |
in |
|
203 |
morphism "instantiate_frees" |
|
204 |
{binding = [], |
|
205 |
typ = |
|
206 |
if TFrees.is_empty instT then [] |
|
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
207 |
else [K (Term_Subst.instantiateT_frees instT)], |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
208 |
term = [K (Term_Subst.instantiate_frees (instT, inst))], |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
209 |
fact = [K (map (Thm.instantiate_frees (cinstT, cinst)))]} |
74282 | 210 |
end; |
67698
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
67664
diff
changeset
|
211 |
|
74282 | 212 |
fun instantiate_morphism (cinstT, cinst) = |
213 |
if TVars.is_empty cinstT andalso Vars.is_empty cinst then identity |
|
214 |
else |
|
215 |
let |
|
216 |
val instT = TVars.map (K Thm.typ_of) cinstT; |
|
217 |
val inst = Vars.map (K Thm.term_of) cinst; |
|
218 |
in |
|
219 |
morphism "instantiate" |
|
220 |
{binding = [], |
|
221 |
typ = |
|
222 |
if TVars.is_empty instT then [] |
|
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
223 |
else [K (Term_Subst.instantiateT instT)], |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
224 |
term = [K (Term_Subst.instantiate (instT, inst))], |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78060
diff
changeset
|
225 |
fact = [K (map (Thm.instantiate (cinstT, cinst)))]} |
74282 | 226 |
end; |
67651
6dd41193a72a
more explicit instantiate_morphism (without checks for typ / term component);
wenzelm
parents:
67650
diff
changeset
|
227 |
|
21476 | 228 |
end; |
229 |
||
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
29605
diff
changeset
|
230 |
structure Basic_Morphism: BASIC_MORPHISM = Morphism; |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
29605
diff
changeset
|
231 |
open Basic_Morphism; |