src/Pure/morphism.ML
author wenzelm
Tue Sep 26 20:54:40 2017 +0200 (22 months ago)
changeset 66695 91500c024c7f
parent 62819 d3ff367a16a0
child 67650 5e4f9a0ffea5
permissions -rw-r--r--
tuned;
     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
    12   type declaration = morphism -> Context.generic -> Context.generic
    13   val $> : morphism * morphism -> morphism
    14 end
    15 
    16 signature MORPHISM =
    17 sig
    18   include BASIC_MORPHISM
    19   exception MORPHISM of string * exn
    20   val pretty: morphism -> Pretty.T
    21   val binding: morphism -> binding -> binding
    22   val typ: morphism -> typ -> typ
    23   val term: morphism -> term -> term
    24   val fact: morphism -> thm list -> thm list
    25   val thm: morphism -> thm -> thm
    26   val cterm: morphism -> cterm -> cterm
    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
    37   val transfer_morphism: theory -> morphism
    38   val trim_context_morphism: morphism
    39   val identity: morphism
    40   val compose: morphism -> morphism -> morphism
    41   val transform: morphism -> (morphism -> 'a) -> morphism -> 'a
    42   val form: (morphism -> 'a) -> 'a
    43 end;
    44 
    45 structure Morphism: MORPHISM =
    46 struct
    47 
    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
    55   handle exn =>
    56     if Exn.is_interrupt exn then Exn.reraise exn else raise MORPHISM (name, exn);
    57 
    58 fun apply fs = fold_rev app fs;
    59 
    60 
    61 (* type morphism *)
    62 
    63 datatype morphism = Morphism of
    64  {names: string list,
    65   binding: binding funs,
    66   typ: typ funs,
    67   term: term funs,
    68   fact: thm list funs};
    69 
    70 type declaration = morphism -> Context.generic -> Context.generic;
    71 
    72 fun pretty (Morphism {names, ...}) = Pretty.enum ";" "{" "}" (map Pretty.str (rev names));
    73 
    74 val _ = ML_system_pp (fn _ => fn _ => Pretty.to_polyml o pretty);
    75 
    76 fun binding (Morphism {binding, ...}) = apply binding;
    77 fun typ (Morphism {typ, ...}) = apply typ;
    78 fun term (Morphism {term, ...}) = apply term;
    79 fun fact (Morphism {fact, ...}) = apply fact;
    80 val thm = singleton o fact;
    81 val cterm = Drule.cterm_rule o thm;
    82 
    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};
    91 
    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;
    98 val trim_context_morphism = thm_morphism "trim_context" Thm.trim_context;
    99 
   100 val identity = morphism "" {binding = [], typ = [], term = [], fact = []};
   101 
   102 
   103 (* morphism combinators *)
   104 
   105 fun compose
   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};
   114 
   115 fun phi1 $> phi2 = compose phi2 phi1;
   116 
   117 fun transform phi f = fn psi => f (phi $> psi);
   118 fun form f = f identity;
   119 
   120 end;
   121 
   122 structure Basic_Morphism: BASIC_MORPHISM = Morphism;
   123 open Basic_Morphism;