| author | wenzelm | 
| Mon, 02 Jan 2017 11:42:15 +0100 | |
| changeset 64748 | 155bf8632104 | 
| parent 62819 | d3ff367a16a0 | 
| child 67650 | 5e4f9a0ffea5 | 
| 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 | 
| 20 | val pretty: morphism -> Pretty.T | |
| 29581 | 21 | val binding: morphism -> binding -> binding | 
| 21476 | 22 | val typ: morphism -> typ -> typ | 
| 23 | val term: morphism -> term -> term | |
| 21521 | 24 | val fact: morphism -> thm list -> thm list | 
| 21476 | 25 | val thm: morphism -> thm -> thm | 
| 22235 | 26 | val cterm: morphism -> cterm -> cterm | 
| 54740 | 27 | val morphism: string -> | 
| 28 |    {binding: (binding -> binding) list,
 | |
| 29 | typ: (typ -> typ) list, | |
| 30 | term: (term -> term) list, | |
| 31 | fact: (thm list -> thm list) list} -> morphism | |
| 32 | val binding_morphism: string -> (binding -> binding) -> morphism | |
| 33 | val typ_morphism: string -> (typ -> typ) -> morphism | |
| 34 | val term_morphism: string -> (term -> term) -> morphism | |
| 35 | val fact_morphism: string -> (thm list -> thm list) -> morphism | |
| 36 | val thm_morphism: string -> (thm -> thm) -> morphism | |
| 53087 | 37 | val transfer_morphism: theory -> morphism | 
| 61064 | 38 | val trim_context_morphism: morphism | 
| 21476 | 39 | val identity: morphism | 
| 22571 
3f00e937d1c9
renamed comp to compose (avoid clash with Alice keywords);
 wenzelm parents: 
22235diff
changeset | 40 | val compose: morphism -> morphism -> morphism | 
| 22670 
c803b2696ada
added Morphism.transform/form (generic non-sense);
 wenzelm parents: 
22571diff
changeset | 41 | val transform: morphism -> (morphism -> 'a) -> morphism -> 'a | 
| 
c803b2696ada
added Morphism.transform/form (generic non-sense);
 wenzelm parents: 
22571diff
changeset | 42 | val form: (morphism -> 'a) -> 'a | 
| 21476 | 43 | end; | 
| 44 | ||
| 45 | structure Morphism: MORPHISM = | |
| 46 | struct | |
| 47 | ||
| 54740 | 48 | (* named functions *) | 
| 49 | ||
| 50 | type 'a funs = (string * ('a -> 'a)) list;
 | |
| 51 | ||
| 52 | exception MORPHISM of string * exn; | |
| 53 | ||
| 54 | fun app (name, f) x = f x | |
| 62505 | 55 | handle exn => | 
| 56 | if Exn.is_interrupt exn then Exn.reraise exn else raise MORPHISM (name, exn); | |
| 54740 | 57 | |
| 58 | fun apply fs = fold_rev app fs; | |
| 59 | ||
| 60 | ||
| 61 | (* type morphism *) | |
| 45289 
25e9e7f527b4
slightly more explicit/syntactic modelling of morphisms;
 wenzelm parents: 
37216diff
changeset | 62 | |
| 21476 | 63 | datatype morphism = Morphism of | 
| 54740 | 64 |  {names: string list,
 | 
| 65 | binding: binding funs, | |
| 45289 
25e9e7f527b4
slightly more explicit/syntactic modelling of morphisms;
 wenzelm parents: 
37216diff
changeset | 66 | typ: typ funs, | 
| 
25e9e7f527b4
slightly more explicit/syntactic modelling of morphisms;
 wenzelm parents: 
37216diff
changeset | 67 | term: term funs, | 
| 
25e9e7f527b4
slightly more explicit/syntactic modelling of morphisms;
 wenzelm parents: 
37216diff
changeset | 68 | fact: thm list funs}; | 
| 21476 | 69 | |
| 24031 | 70 | type declaration = morphism -> Context.generic -> Context.generic; | 
| 71 | ||
| 54740 | 72 | fun pretty (Morphism {names, ...}) = Pretty.enum ";" "{" "}" (map Pretty.str (rev names));
 | 
| 73 | ||
| 62819 
d3ff367a16a0
careful export of type-dependent functions, without losing their special status;
 wenzelm parents: 
62663diff
changeset | 74 | val _ = ML_system_pp (fn _ => fn _ => Pretty.to_polyml o pretty); | 
| 62663 | 75 | |
| 45289 
25e9e7f527b4
slightly more explicit/syntactic modelling of morphisms;
 wenzelm parents: 
37216diff
changeset | 76 | fun binding (Morphism {binding, ...}) = apply binding;
 | 
| 
25e9e7f527b4
slightly more explicit/syntactic modelling of morphisms;
 wenzelm parents: 
37216diff
changeset | 77 | fun typ (Morphism {typ, ...}) = apply typ;
 | 
| 
25e9e7f527b4
slightly more explicit/syntactic modelling of morphisms;
 wenzelm parents: 
37216diff
changeset | 78 | fun term (Morphism {term, ...}) = apply term;
 | 
| 
25e9e7f527b4
slightly more explicit/syntactic modelling of morphisms;
 wenzelm parents: 
37216diff
changeset | 79 | fun fact (Morphism {fact, ...}) = apply fact;
 | 
| 21521 | 80 | val thm = singleton o fact; | 
| 22235 | 81 | val cterm = Drule.cterm_rule o thm; | 
| 21476 | 82 | |
| 54740 | 83 | |
| 84 | fun morphism a {binding, typ, term, fact} =
 | |
| 85 |   Morphism {
 | |
| 86 | names = if a = "" then [] else [a], | |
| 87 | binding = map (pair a) binding, | |
| 88 | typ = map (pair a) typ, | |
| 89 | term = map (pair a) term, | |
| 90 | fact = map (pair a) fact}; | |
| 21476 | 91 | |
| 54740 | 92 | fun binding_morphism a binding = morphism a {binding = [binding], typ = [], term = [], fact = []};
 | 
| 93 | fun typ_morphism a typ = morphism a {binding = [], typ = [typ], term = [], fact = []};
 | |
| 94 | fun term_morphism a term = morphism a {binding = [], typ = [], term = [term], fact = []};
 | |
| 95 | fun fact_morphism a fact = morphism a {binding = [], typ = [], term = [], fact = [fact]};
 | |
| 96 | fun thm_morphism a thm = morphism a {binding = [], typ = [], term = [], fact = [map thm]};
 | |
| 97 | val transfer_morphism = thm_morphism "transfer" o Thm.transfer; | |
| 61064 | 98 | val trim_context_morphism = thm_morphism "trim_context" Thm.trim_context; | 
| 21492 | 99 | |
| 54740 | 100 | val identity = morphism "" {binding = [], typ = [], term = [], fact = []};
 | 
| 101 | ||
| 102 | ||
| 103 | (* morphism combinators *) | |
| 21492 | 104 | |
| 22571 
3f00e937d1c9
renamed comp to compose (avoid clash with Alice keywords);
 wenzelm parents: 
22235diff
changeset | 105 | fun compose | 
| 54740 | 106 |     (Morphism {names = names1, binding = binding1, typ = typ1, term = term1, fact = fact1})
 | 
| 107 |     (Morphism {names = names2, binding = binding2, typ = typ2, term = term2, fact = fact2}) =
 | |
| 108 |   Morphism {
 | |
| 109 | names = names1 @ names2, | |
| 110 | binding = binding1 @ binding2, | |
| 111 | typ = typ1 @ typ2, | |
| 112 | term = term1 @ term2, | |
| 113 | fact = fact1 @ fact2}; | |
| 21476 | 114 | |
| 22571 
3f00e937d1c9
renamed comp to compose (avoid clash with Alice keywords);
 wenzelm parents: 
22235diff
changeset | 115 | fun phi1 $> phi2 = compose phi2 phi1; | 
| 21476 | 116 | |
| 22670 
c803b2696ada
added Morphism.transform/form (generic non-sense);
 wenzelm parents: 
22571diff
changeset | 117 | fun transform phi f = fn psi => f (phi $> psi); | 
| 
c803b2696ada
added Morphism.transform/form (generic non-sense);
 wenzelm parents: 
22571diff
changeset | 118 | fun form f = f identity; | 
| 
c803b2696ada
added Morphism.transform/form (generic non-sense);
 wenzelm parents: 
22571diff
changeset | 119 | |
| 21476 | 120 | end; | 
| 121 | ||
| 37216 
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
 wenzelm parents: 
29605diff
changeset | 122 | structure Basic_Morphism: BASIC_MORPHISM = Morphism; | 
| 
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
 wenzelm parents: 
29605diff
changeset | 123 | open Basic_Morphism; |