src/HOL/Tools/functorial_mappers.ML
author haftmann
Wed, 17 Nov 2010 11:09:18 +0100
changeset 40582 968c481aa18c
child 40583 6fed6f946471
permissions -rw-r--r--
module for functorial mappers
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40582
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/Tools/functorial-mappers.ML
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
     3
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
     4
Functorial mappers on types.
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
     5
*)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
     6
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
     7
signature FUNCTORIAL_MAPPERS =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
     8
sig
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
     9
  val find_atomic: theory -> typ -> (typ * (bool * bool)) list
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    10
  val construct_mapper: theory -> (string * bool -> term)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    11
    -> bool -> typ -> typ -> term
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    12
  val register: string -> string -> (sort * (bool * bool)) list -> thm -> thm
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    13
    -> theory -> theory
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    14
  type entry
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    15
  val entries: theory -> entry Symtab.table
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    16
end;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    17
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    18
structure Functorial_Mappers : FUNCTORIAL_MAPPERS =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    19
struct
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    20
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    21
(** functorial mappers and their properties **)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    22
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    23
(* bookkeeping *)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    24
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    25
type entry = { mapper: string, variances: (sort * (bool * bool)) list,
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    26
  concatenate: thm, identity: thm };
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    27
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    28
structure Data = Theory_Data(
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    29
  type T = entry Symtab.table
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    30
  val empty = Symtab.empty
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    31
  val merge = Symtab.merge (K true)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    32
  val extend = I
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    33
);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    34
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    35
val entries = Data.get;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    36
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    37
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    38
(* type analysis *)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    39
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    40
fun find_atomic thy T =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    41
  let
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    42
    val variances_of = Option.map #variances o Symtab.lookup (Data.get thy);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    43
    fun add_variance is_contra T =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    44
      AList.map_default (op =) (T, (false, false))
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    45
        ((if is_contra then apsnd else apfst) (K true));
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    46
    fun analyze' is_contra (_, (co, contra)) T =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    47
      (if co then analyze is_contra T else I)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    48
      #> (if contra then analyze (not is_contra) T else I)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    49
    and analyze is_contra (T as Type (tyco, Ts)) = (case variances_of tyco
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    50
          of NONE => add_variance is_contra T
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    51
           | SOME variances => fold2 (analyze' is_contra) variances Ts)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    52
      | analyze is_contra T = add_variance is_contra T;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    53
  in analyze false T [] end;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    54
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    55
fun construct_mapper thy atomic =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    56
  let
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    57
    val lookup = the o Symtab.lookup (Data.get thy);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    58
    fun constructs is_contra (_, (co, contra)) T T' =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    59
      (if co then [construct is_contra T T'] else [])
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    60
      @ (if contra then [construct (not is_contra) T T'] else [])
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    61
    and construct is_contra (T as Type (tyco, Ts)) (T' as Type (_, Ts')) =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    62
          let
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    63
            val { mapper, variances, ... } = lookup tyco;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    64
            val args = maps (fn (arg_pattern, (T, T')) =>
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    65
              constructs is_contra arg_pattern T T')
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    66
                (variances ~~ (Ts ~~ Ts'));
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    67
            val (U, U') = if is_contra then (T', T) else (T, T');
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    68
          in list_comb (Const (mapper, map fastype_of args ---> U --> U'), args) end
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    69
      | construct is_contra (TFree (v, _)) (TFree _) = atomic (v, is_contra);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    70
  in construct end;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    71
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    72
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    73
(* mapper properties *)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    74
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    75
fun make_concatenate_prop variances (tyco, mapper) =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    76
  let
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    77
    fun invents n k nctxt =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    78
      let
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    79
        val names = Name.invents nctxt n k;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    80
      in (names, fold Name.declare names nctxt) end;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    81
    val (((vs1, vs2), vs3), _) = Name.context
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    82
      |> invents Name.aT (length variances)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    83
      ||>> invents Name.aT (length variances)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    84
      ||>> invents Name.aT (length variances);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    85
    fun mk_Ts vs = map2 (fn v => fn (sort, _) => TFree (v, sort))
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    86
      vs variances;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    87
    val (Ts1, Ts2, Ts3) = (mk_Ts vs1, mk_Ts vs2, mk_Ts vs3);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    88
    fun mk_argT ((T, T'), (_, (co, contra))) =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    89
      (if co then [(T --> T')] else [])
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    90
      @ (if contra then [(T' --> T)] else []);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    91
    val contras = maps (fn (_, (co, contra)) =>
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    92
      (if co then [false] else []) @ (if contra then [true] else [])) variances;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    93
    val Ts21 = maps mk_argT ((Ts2 ~~ Ts1) ~~ variances);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    94
    val Ts32 = maps mk_argT ((Ts3 ~~ Ts2) ~~ variances);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    95
    val ((names21, names32), nctxt) = Name.context
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    96
      |> invents "f" (length Ts21)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    97
      ||>> invents "f" (length Ts32);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    98
    val T1 = Type (tyco, Ts1);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    99
    val T2 = Type (tyco, Ts2);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   100
    val T3 = Type (tyco, Ts3);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   101
    val x = Free (the_single (Name.invents nctxt "a" 1), T3);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   102
    val (args21, args32) = (names21 ~~ Ts21, names32 ~~ Ts32);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   103
    val args31 = map2 (fn is_contra => fn ((f21, T21), (f32, T32)) =>
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   104
      if not is_contra then
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   105
        Abs ("x", domain_type T32, Free (f21, T21) $ (Free (f32, T32) $ Bound 0))
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   106
      else
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   107
        Abs ("x", domain_type T21, Free (f32, T32) $ (Free (f21, T21) $ Bound 0))
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   108
      ) contras (args21 ~~ args32)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   109
    fun mk_mapper T T' args = list_comb (Const (mapper,
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   110
      map fastype_of args ---> T --> T'), args);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   111
    val lhs = mk_mapper T2 T1 (map Free args21) $
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   112
      (mk_mapper T3 T2 (map Free args32) $ x);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   113
    val rhs = mk_mapper T3 T1 args31 $ x;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   114
  in (x, (HOLogic.mk_Trueprop o HOLogic.mk_eq) (lhs, rhs)) end;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   115
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   116
fun make_identity_prop variances (tyco, mapper) =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   117
  let
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   118
    val vs = Name.invents Name.context Name.aT (length variances);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   119
    val Ts = map2 (fn v => fn (sort, _) => TFree (v, sort)) vs variances;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   120
    fun bool_num b = if b then 1 else 0;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   121
    fun mk_argT (T, (_, (co, contra))) =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   122
      replicate (bool_num co + bool_num contra) (T --> T)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   123
    val Ts' = maps mk_argT (Ts ~~ variances)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   124
    val T = Type (tyco, Ts);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   125
    val x = Free ("a", T);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   126
    val lhs = list_comb (Const (mapper, Ts' ---> T --> T),
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   127
      map (fn T => Abs ("x", domain_type T, Bound 0)) Ts') $ x;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   128
  in (x, (HOLogic.mk_Trueprop o HOLogic.mk_eq) (lhs, x)) end;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   129
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   130
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   131
(* registering primitive mappers *)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   132
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   133
fun register tyco mapper variances raw_concatenate raw_identity thy =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   134
  let
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   135
    val (_, concatenate_prop) = make_concatenate_prop variances (tyco, mapper);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   136
    val (_, identity_prop) = make_identity_prop variances (tyco, mapper);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   137
    val concatenate = Goal.prove_global thy
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   138
      (Term.add_free_names concatenate_prop []) [] concatenate_prop
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   139
      (K (ALLGOALS (ProofContext.fact_tac [raw_concatenate])));
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   140
    val identity = Goal.prove_global thy
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   141
      (Term.add_free_names identity_prop []) [] identity_prop
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   142
      (K (ALLGOALS (ProofContext.fact_tac [raw_identity])));
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   143
  in
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   144
    thy
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   145
    |> Data.map (Symtab.update (tyco, { mapper = mapper,
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   146
        variances = variances,
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   147
        concatenate = concatenate, identity = identity }))
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   148
  end;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   149
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   150
end;