author | paulson <lp15@cam.ac.uk> |
Sun, 20 May 2018 18:37:34 +0100 | |
changeset 68239 | 0764ee22a4d1 |
parent 67698 | 67caf783b9ee |
child 69062 | 5eda37c06f42 |
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 |
67650 | 20 |
val morphism: string -> |
21 |
{binding: (binding -> binding) list, |
|
22 |
typ: (typ -> typ) list, |
|
23 |
term: (term -> term) list, |
|
24 |
fact: (thm list -> thm list) list} -> morphism |
|
54740 | 25 |
val pretty: morphism -> Pretty.T |
29581 | 26 |
val binding: morphism -> binding -> binding |
21476 | 27 |
val typ: morphism -> typ -> typ |
28 |
val term: morphism -> term -> term |
|
21521 | 29 |
val fact: morphism -> thm list -> thm list |
21476 | 30 |
val thm: morphism -> thm -> thm |
22235 | 31 |
val cterm: morphism -> cterm -> cterm |
67650 | 32 |
val identity: morphism |
33 |
val compose: morphism -> morphism -> morphism |
|
34 |
val transform: morphism -> (morphism -> 'a) -> morphism -> 'a |
|
35 |
val form: (morphism -> 'a) -> 'a |
|
54740 | 36 |
val binding_morphism: string -> (binding -> binding) -> morphism |
37 |
val typ_morphism: string -> (typ -> typ) -> morphism |
|
38 |
val term_morphism: string -> (term -> term) -> morphism |
|
39 |
val fact_morphism: string -> (thm list -> thm list) -> morphism |
|
40 |
val thm_morphism: string -> (thm -> thm) -> morphism |
|
53087 | 41 |
val transfer_morphism: theory -> morphism |
67664 | 42 |
val transfer_morphism': Proof.context -> morphism |
43 |
val transfer_morphism'': Context.generic -> morphism |
|
61064 | 44 |
val trim_context_morphism: morphism |
67698
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
67664
diff
changeset
|
45 |
val instantiate_frees_morphism: |
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
67664
diff
changeset
|
46 |
((string * sort) * ctyp) list * ((string * typ) * cterm) list -> morphism |
67651
6dd41193a72a
more explicit instantiate_morphism (without checks for typ / term component);
wenzelm
parents:
67650
diff
changeset
|
47 |
val instantiate_morphism: |
6dd41193a72a
more explicit instantiate_morphism (without checks for typ / term component);
wenzelm
parents:
67650
diff
changeset
|
48 |
((indexname * sort) * ctyp) list * ((indexname * typ) * cterm) list -> morphism |
21476 | 49 |
end; |
50 |
||
51 |
structure Morphism: MORPHISM = |
|
52 |
struct |
|
53 |
||
54740 | 54 |
(* named functions *) |
55 |
||
56 |
type 'a funs = (string * ('a -> 'a)) list; |
|
57 |
||
58 |
exception MORPHISM of string * exn; |
|
59 |
||
60 |
fun app (name, f) x = f x |
|
62505 | 61 |
handle exn => |
62 |
if Exn.is_interrupt exn then Exn.reraise exn else raise MORPHISM (name, exn); |
|
54740 | 63 |
|
64 |
fun apply fs = fold_rev app fs; |
|
65 |
||
66 |
||
67 |
(* type morphism *) |
|
45289
25e9e7f527b4
slightly more explicit/syntactic modelling of morphisms;
wenzelm
parents:
37216
diff
changeset
|
68 |
|
21476 | 69 |
datatype morphism = Morphism of |
54740 | 70 |
{names: string list, |
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 | 75 |
|
24031 | 76 |
type declaration = morphism -> Context.generic -> Context.generic; |
77 |
||
67650 | 78 |
fun morphism a {binding, typ, term, fact} = |
79 |
Morphism { |
|
80 |
names = if a = "" then [] else [a], |
|
81 |
binding = map (pair a) binding, |
|
82 |
typ = map (pair a) typ, |
|
83 |
term = map (pair a) term, |
|
84 |
fact = map (pair a) fact}; |
|
85 |
||
54740 | 86 |
fun pretty (Morphism {names, ...}) = Pretty.enum ";" "{" "}" (map Pretty.str (rev names)); |
87 |
||
62819
d3ff367a16a0
careful export of type-dependent functions, without losing their special status;
wenzelm
parents:
62663
diff
changeset
|
88 |
val _ = ML_system_pp (fn _ => fn _ => Pretty.to_polyml o pretty); |
62663 | 89 |
|
45289
25e9e7f527b4
slightly more explicit/syntactic modelling of morphisms;
wenzelm
parents:
37216
diff
changeset
|
90 |
fun binding (Morphism {binding, ...}) = apply binding; |
25e9e7f527b4
slightly more explicit/syntactic modelling of morphisms;
wenzelm
parents:
37216
diff
changeset
|
91 |
fun typ (Morphism {typ, ...}) = apply typ; |
25e9e7f527b4
slightly more explicit/syntactic modelling of morphisms;
wenzelm
parents:
37216
diff
changeset
|
92 |
fun term (Morphism {term, ...}) = apply term; |
25e9e7f527b4
slightly more explicit/syntactic modelling of morphisms;
wenzelm
parents:
37216
diff
changeset
|
93 |
fun fact (Morphism {fact, ...}) = apply fact; |
21521 | 94 |
val thm = singleton o fact; |
22235 | 95 |
val cterm = Drule.cterm_rule o thm; |
21476 | 96 |
|
54740 | 97 |
|
67650 | 98 |
(* morphism combinators *) |
21492 | 99 |
|
54740 | 100 |
val identity = morphism "" {binding = [], typ = [], term = [], fact = []}; |
101 |
||
22571
3f00e937d1c9
renamed comp to compose (avoid clash with Alice keywords);
wenzelm
parents:
22235
diff
changeset
|
102 |
fun compose |
54740 | 103 |
(Morphism {names = names1, binding = binding1, typ = typ1, term = term1, fact = fact1}) |
104 |
(Morphism {names = names2, binding = binding2, typ = typ2, term = term2, fact = fact2}) = |
|
105 |
Morphism { |
|
106 |
names = names1 @ names2, |
|
107 |
binding = binding1 @ binding2, |
|
108 |
typ = typ1 @ typ2, |
|
109 |
term = term1 @ term2, |
|
110 |
fact = fact1 @ fact2}; |
|
21476 | 111 |
|
22571
3f00e937d1c9
renamed comp to compose (avoid clash with Alice keywords);
wenzelm
parents:
22235
diff
changeset
|
112 |
fun phi1 $> phi2 = compose phi2 phi1; |
21476 | 113 |
|
22670
c803b2696ada
added Morphism.transform/form (generic non-sense);
wenzelm
parents:
22571
diff
changeset
|
114 |
fun transform phi f = fn psi => f (phi $> psi); |
c803b2696ada
added Morphism.transform/form (generic non-sense);
wenzelm
parents:
22571
diff
changeset
|
115 |
fun form f = f identity; |
c803b2696ada
added Morphism.transform/form (generic non-sense);
wenzelm
parents:
22571
diff
changeset
|
116 |
|
67650 | 117 |
|
118 |
(* concrete morphisms *) |
|
119 |
||
120 |
fun binding_morphism a binding = morphism a {binding = [binding], typ = [], term = [], fact = []}; |
|
121 |
fun typ_morphism a typ = morphism a {binding = [], typ = [typ], term = [], fact = []}; |
|
122 |
fun term_morphism a term = morphism a {binding = [], typ = [], term = [term], fact = []}; |
|
123 |
fun fact_morphism a fact = morphism a {binding = [], typ = [], term = [], fact = [fact]}; |
|
124 |
fun thm_morphism a thm = morphism a {binding = [], typ = [], term = [], fact = [map thm]}; |
|
125 |
||
126 |
val transfer_morphism = thm_morphism "transfer" o Thm.transfer; |
|
67664 | 127 |
val transfer_morphism' = transfer_morphism o Proof_Context.theory_of; |
128 |
val transfer_morphism'' = transfer_morphism o Context.theory_of; |
|
129 |
||
67650 | 130 |
val trim_context_morphism = thm_morphism "trim_context" Thm.trim_context; |
131 |
||
67698
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
67664
diff
changeset
|
132 |
|
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
67664
diff
changeset
|
133 |
(* instantiate *) |
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
67664
diff
changeset
|
134 |
|
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
67664
diff
changeset
|
135 |
fun instantiate_frees_morphism ([], []) = identity |
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
67664
diff
changeset
|
136 |
| instantiate_frees_morphism (cinstT, cinst) = |
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
67664
diff
changeset
|
137 |
let |
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
67664
diff
changeset
|
138 |
val instT = map (apsnd Thm.typ_of) cinstT; |
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
67664
diff
changeset
|
139 |
val inst = map (apsnd Thm.term_of) cinst; |
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
67664
diff
changeset
|
140 |
in |
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
67664
diff
changeset
|
141 |
morphism "instantiate_frees" |
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
67664
diff
changeset
|
142 |
{binding = [], |
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
67664
diff
changeset
|
143 |
typ = if null instT then [] else [Term_Subst.instantiateT_frees instT], |
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
67664
diff
changeset
|
144 |
term = [Term_Subst.instantiate_frees (instT, inst)], |
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
67664
diff
changeset
|
145 |
fact = [map (Thm.instantiate_frees (cinstT, cinst))]} |
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
67664
diff
changeset
|
146 |
end; |
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
67664
diff
changeset
|
147 |
|
67651
6dd41193a72a
more explicit instantiate_morphism (without checks for typ / term component);
wenzelm
parents:
67650
diff
changeset
|
148 |
fun instantiate_morphism ([], []) = identity |
6dd41193a72a
more explicit instantiate_morphism (without checks for typ / term component);
wenzelm
parents:
67650
diff
changeset
|
149 |
| instantiate_morphism (cinstT, cinst) = |
6dd41193a72a
more explicit instantiate_morphism (without checks for typ / term component);
wenzelm
parents:
67650
diff
changeset
|
150 |
let |
6dd41193a72a
more explicit instantiate_morphism (without checks for typ / term component);
wenzelm
parents:
67650
diff
changeset
|
151 |
val instT = map (apsnd Thm.typ_of) cinstT; |
6dd41193a72a
more explicit instantiate_morphism (without checks for typ / term component);
wenzelm
parents:
67650
diff
changeset
|
152 |
val inst = map (apsnd Thm.term_of) cinst; |
6dd41193a72a
more explicit instantiate_morphism (without checks for typ / term component);
wenzelm
parents:
67650
diff
changeset
|
153 |
in |
6dd41193a72a
more explicit instantiate_morphism (without checks for typ / term component);
wenzelm
parents:
67650
diff
changeset
|
154 |
morphism "instantiate" |
6dd41193a72a
more explicit instantiate_morphism (without checks for typ / term component);
wenzelm
parents:
67650
diff
changeset
|
155 |
{binding = [], |
6dd41193a72a
more explicit instantiate_morphism (without checks for typ / term component);
wenzelm
parents:
67650
diff
changeset
|
156 |
typ = if null instT then [] else [Term_Subst.instantiateT instT], |
6dd41193a72a
more explicit instantiate_morphism (without checks for typ / term component);
wenzelm
parents:
67650
diff
changeset
|
157 |
term = [Term_Subst.instantiate (instT, inst)], |
6dd41193a72a
more explicit instantiate_morphism (without checks for typ / term component);
wenzelm
parents:
67650
diff
changeset
|
158 |
fact = [map (Thm.instantiate (cinstT, cinst))]} |
6dd41193a72a
more explicit instantiate_morphism (without checks for typ / term component);
wenzelm
parents:
67650
diff
changeset
|
159 |
end; |
6dd41193a72a
more explicit instantiate_morphism (without checks for typ / term component);
wenzelm
parents:
67650
diff
changeset
|
160 |
|
21476 | 161 |
end; |
162 |
||
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
29605
diff
changeset
|
163 |
structure Basic_Morphism: BASIC_MORPHISM = Morphism; |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
29605
diff
changeset
|
164 |
open Basic_Morphism; |