src/Pure/morphism.ML
author wenzelm
Fri Dec 13 20:20:15 2013 +0100 (2013-12-13)
changeset 54740 91f54d386680
parent 53087 5a1dcda7967c
child 61064 01b23bfb4947
permissions -rw-r--r--
maintain morphism names for diagnostic purposes;
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@21476
    38
  val identity: morphism
wenzelm@22571
    39
  val compose: morphism -> morphism -> morphism
wenzelm@22670
    40
  val transform: morphism -> (morphism -> 'a) -> morphism -> 'a
wenzelm@22670
    41
  val form: (morphism -> 'a) -> 'a
wenzelm@21476
    42
end;
wenzelm@21476
    43
wenzelm@21476
    44
structure Morphism: MORPHISM =
wenzelm@21476
    45
struct
wenzelm@21476
    46
wenzelm@54740
    47
(* named functions *)
wenzelm@54740
    48
wenzelm@54740
    49
type 'a funs = (string * ('a -> 'a)) list;
wenzelm@54740
    50
wenzelm@54740
    51
exception MORPHISM of string * exn;
wenzelm@54740
    52
wenzelm@54740
    53
fun app (name, f) x = f x
wenzelm@54740
    54
  handle exn => if Exn.is_interrupt exn then reraise exn else raise MORPHISM (name, exn);
wenzelm@54740
    55
wenzelm@54740
    56
fun apply fs = fold_rev app fs;
wenzelm@54740
    57
wenzelm@54740
    58
wenzelm@54740
    59
(* type morphism *)
wenzelm@45289
    60
wenzelm@21476
    61
datatype morphism = Morphism of
wenzelm@54740
    62
 {names: string list,
wenzelm@54740
    63
  binding: binding funs,
wenzelm@45289
    64
  typ: typ funs,
wenzelm@45289
    65
  term: term funs,
wenzelm@45289
    66
  fact: thm list funs};
wenzelm@21476
    67
wenzelm@24031
    68
type declaration = morphism -> Context.generic -> Context.generic;
wenzelm@24031
    69
wenzelm@54740
    70
fun pretty (Morphism {names, ...}) = Pretty.enum ";" "{" "}" (map Pretty.str (rev names));
wenzelm@54740
    71
wenzelm@45289
    72
fun binding (Morphism {binding, ...}) = apply binding;
wenzelm@45289
    73
fun typ (Morphism {typ, ...}) = apply typ;
wenzelm@45289
    74
fun term (Morphism {term, ...}) = apply term;
wenzelm@45289
    75
fun fact (Morphism {fact, ...}) = apply fact;
wenzelm@21521
    76
val thm = singleton o fact;
wenzelm@22235
    77
val cterm = Drule.cterm_rule o thm;
wenzelm@21476
    78
wenzelm@54740
    79
wenzelm@54740
    80
fun morphism a {binding, typ, term, fact} =
wenzelm@54740
    81
  Morphism {
wenzelm@54740
    82
    names = if a = "" then [] else [a],
wenzelm@54740
    83
    binding = map (pair a) binding,
wenzelm@54740
    84
    typ = map (pair a) typ,
wenzelm@54740
    85
    term = map (pair a) term,
wenzelm@54740
    86
    fact = map (pair a) fact};
wenzelm@21476
    87
wenzelm@54740
    88
fun binding_morphism a binding = morphism a {binding = [binding], typ = [], term = [], fact = []};
wenzelm@54740
    89
fun typ_morphism a typ = morphism a {binding = [], typ = [typ], term = [], fact = []};
wenzelm@54740
    90
fun term_morphism a term = morphism a {binding = [], typ = [], term = [term], fact = []};
wenzelm@54740
    91
fun fact_morphism a fact = morphism a {binding = [], typ = [], term = [], fact = [fact]};
wenzelm@54740
    92
fun thm_morphism a thm = morphism a {binding = [], typ = [], term = [], fact = [map thm]};
wenzelm@54740
    93
val transfer_morphism = thm_morphism "transfer" o Thm.transfer;
wenzelm@21492
    94
wenzelm@54740
    95
val identity = morphism "" {binding = [], typ = [], term = [], fact = []};
wenzelm@54740
    96
wenzelm@54740
    97
wenzelm@54740
    98
(* morphism combinators *)
wenzelm@21492
    99
wenzelm@22571
   100
fun compose
wenzelm@54740
   101
    (Morphism {names = names1, binding = binding1, typ = typ1, term = term1, fact = fact1})
wenzelm@54740
   102
    (Morphism {names = names2, binding = binding2, typ = typ2, term = term2, fact = fact2}) =
wenzelm@54740
   103
  Morphism {
wenzelm@54740
   104
    names = names1 @ names2,
wenzelm@54740
   105
    binding = binding1 @ binding2,
wenzelm@54740
   106
    typ = typ1 @ typ2,
wenzelm@54740
   107
    term = term1 @ term2,
wenzelm@54740
   108
    fact = fact1 @ fact2};
wenzelm@21476
   109
wenzelm@22571
   110
fun phi1 $> phi2 = compose phi2 phi1;
wenzelm@21476
   111
wenzelm@22670
   112
fun transform phi f = fn psi => f (phi $> psi);
wenzelm@22670
   113
fun form f = f identity;
wenzelm@22670
   114
wenzelm@21476
   115
end;
wenzelm@21476
   116
wenzelm@37216
   117
structure Basic_Morphism: BASIC_MORPHISM = Morphism;
wenzelm@37216
   118
open Basic_Morphism;