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;
wenzelm@21476
     1
(*  Title:      Pure/morphism.ML
wenzelm@21476
     2
    Author:     Makarius
wenzelm@21476
     3
wenzelm@21476
     4
Abstract morphisms on formal entities.
wenzelm@21476
     5
*)
wenzelm@21476
     6
wenzelm@21476
     7
infix 1 $>
wenzelm@21476
     8
wenzelm@21476
     9
signature BASIC_MORPHISM =
wenzelm@21476
    10
sig
wenzelm@21476
    11
  type morphism
wenzelm@24031
    12
  type declaration = morphism -> Context.generic -> Context.generic
wenzelm@21476
    13
  val $> : morphism * morphism -> morphism
wenzelm@21476
    14
end
wenzelm@21476
    15
wenzelm@21476
    16
signature MORPHISM =
wenzelm@21476
    17
sig
wenzelm@21476
    18
  include BASIC_MORPHISM
wenzelm@54740
    19
  exception MORPHISM of string * exn
wenzelm@54740
    20
  val pretty: morphism -> Pretty.T
haftmann@29581
    21
  val binding: morphism -> binding -> binding
wenzelm@21476
    22
  val typ: morphism -> typ -> typ
wenzelm@21476
    23
  val term: morphism -> term -> term
wenzelm@21521
    24
  val fact: morphism -> thm list -> thm list
wenzelm@21476
    25
  val thm: morphism -> thm -> thm
wenzelm@22235
    26
  val cterm: morphism -> cterm -> cterm
wenzelm@54740
    27
  val morphism: string ->
wenzelm@54740
    28
   {binding: (binding -> binding) list,
wenzelm@54740
    29
    typ: (typ -> typ) list,
wenzelm@54740
    30
    term: (term -> term) list,
wenzelm@54740
    31
    fact: (thm list -> thm list) list} -> morphism
wenzelm@54740
    32
  val binding_morphism: string -> (binding -> binding) -> morphism
wenzelm@54740
    33
  val typ_morphism: string -> (typ -> typ) -> morphism
wenzelm@54740
    34
  val term_morphism: string -> (term -> term) -> morphism
wenzelm@54740
    35
  val fact_morphism: string -> (thm list -> thm list) -> morphism
wenzelm@54740
    36
  val thm_morphism: string -> (thm -> thm) -> morphism
wenzelm@53087
    37
  val transfer_morphism: theory -> morphism
wenzelm@61064
    38
  val trim_context_morphism: morphism
wenzelm@21476
    39
  val identity: morphism
wenzelm@22571
    40
  val compose: morphism -> morphism -> morphism
wenzelm@22670
    41
  val transform: morphism -> (morphism -> 'a) -> morphism -> 'a
wenzelm@22670
    42
  val form: (morphism -> 'a) -> 'a
wenzelm@21476
    43
end;
wenzelm@21476
    44
wenzelm@21476
    45
structure Morphism: MORPHISM =
wenzelm@21476
    46
struct
wenzelm@21476
    47
wenzelm@54740
    48
(* named functions *)
wenzelm@54740
    49
wenzelm@54740
    50
type 'a funs = (string * ('a -> 'a)) list;
wenzelm@54740
    51
wenzelm@54740
    52
exception MORPHISM of string * exn;
wenzelm@54740
    53
wenzelm@54740
    54
fun app (name, f) x = f x
wenzelm@62505
    55
  handle exn =>
wenzelm@62505
    56
    if Exn.is_interrupt exn then Exn.reraise exn else raise MORPHISM (name, exn);
wenzelm@54740
    57
wenzelm@54740
    58
fun apply fs = fold_rev app fs;
wenzelm@54740
    59
wenzelm@54740
    60
wenzelm@54740
    61
(* type morphism *)
wenzelm@45289
    62
wenzelm@21476
    63
datatype morphism = Morphism of
wenzelm@54740
    64
 {names: string list,
wenzelm@54740
    65
  binding: binding funs,
wenzelm@45289
    66
  typ: typ funs,
wenzelm@45289
    67
  term: term funs,
wenzelm@45289
    68
  fact: thm list funs};
wenzelm@21476
    69
wenzelm@24031
    70
type declaration = morphism -> Context.generic -> Context.generic;
wenzelm@24031
    71
wenzelm@54740
    72
fun pretty (Morphism {names, ...}) = Pretty.enum ";" "{" "}" (map Pretty.str (rev names));
wenzelm@54740
    73
wenzelm@62819
    74
val _ = ML_system_pp (fn _ => fn _ => Pretty.to_polyml o pretty);
wenzelm@62663
    75
wenzelm@45289
    76
fun binding (Morphism {binding, ...}) = apply binding;
wenzelm@45289
    77
fun typ (Morphism {typ, ...}) = apply typ;
wenzelm@45289
    78
fun term (Morphism {term, ...}) = apply term;
wenzelm@45289
    79
fun fact (Morphism {fact, ...}) = apply fact;
wenzelm@21521
    80
val thm = singleton o fact;
wenzelm@22235
    81
val cterm = Drule.cterm_rule o thm;
wenzelm@21476
    82
wenzelm@54740
    83
wenzelm@54740
    84
fun morphism a {binding, typ, term, fact} =
wenzelm@54740
    85
  Morphism {
wenzelm@54740
    86
    names = if a = "" then [] else [a],
wenzelm@54740
    87
    binding = map (pair a) binding,
wenzelm@54740
    88
    typ = map (pair a) typ,
wenzelm@54740
    89
    term = map (pair a) term,
wenzelm@54740
    90
    fact = map (pair a) fact};
wenzelm@21476
    91
wenzelm@54740
    92
fun binding_morphism a binding = morphism a {binding = [binding], typ = [], term = [], fact = []};
wenzelm@54740
    93
fun typ_morphism a typ = morphism a {binding = [], typ = [typ], term = [], fact = []};
wenzelm@54740
    94
fun term_morphism a term = morphism a {binding = [], typ = [], term = [term], fact = []};
wenzelm@54740
    95
fun fact_morphism a fact = morphism a {binding = [], typ = [], term = [], fact = [fact]};
wenzelm@54740
    96
fun thm_morphism a thm = morphism a {binding = [], typ = [], term = [], fact = [map thm]};
wenzelm@54740
    97
val transfer_morphism = thm_morphism "transfer" o Thm.transfer;
wenzelm@61064
    98
val trim_context_morphism = thm_morphism "trim_context" Thm.trim_context;
wenzelm@21492
    99
wenzelm@54740
   100
val identity = morphism "" {binding = [], typ = [], term = [], fact = []};
wenzelm@54740
   101
wenzelm@54740
   102
wenzelm@54740
   103
(* morphism combinators *)
wenzelm@21492
   104
wenzelm@22571
   105
fun compose
wenzelm@54740
   106
    (Morphism {names = names1, binding = binding1, typ = typ1, term = term1, fact = fact1})
wenzelm@54740
   107
    (Morphism {names = names2, binding = binding2, typ = typ2, term = term2, fact = fact2}) =
wenzelm@54740
   108
  Morphism {
wenzelm@54740
   109
    names = names1 @ names2,
wenzelm@54740
   110
    binding = binding1 @ binding2,
wenzelm@54740
   111
    typ = typ1 @ typ2,
wenzelm@54740
   112
    term = term1 @ term2,
wenzelm@54740
   113
    fact = fact1 @ fact2};
wenzelm@21476
   114
wenzelm@22571
   115
fun phi1 $> phi2 = compose phi2 phi1;
wenzelm@21476
   116
wenzelm@22670
   117
fun transform phi f = fn psi => f (phi $> psi);
wenzelm@22670
   118
fun form f = f identity;
wenzelm@22670
   119
wenzelm@21476
   120
end;
wenzelm@21476
   121
wenzelm@37216
   122
structure Basic_Morphism: BASIC_MORPHISM = Morphism;
wenzelm@37216
   123
open Basic_Morphism;