src/HOL/Tools/functorial_mappers.ML
author haftmann
Thu, 18 Nov 2010 10:59:42 +0100
changeset 40597 19b449037ace
parent 40594 fae1da97bb5e
child 40611 9eb0a9dd186e
permissions -rw-r--r--
keep variables bound
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40586
fe4f6703c59e fixed typo
haftmann
parents: 40584
diff changeset
     1
(*  Title:      HOL/Tools/functorial_mappers.ML
40582
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
40584
28cc2acbc58a ML signature interface
haftmann
parents: 40583
diff changeset
    12
  val type_mapper: term -> theory -> Proof.state
40582
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    13
  type entry
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    14
  val entries: theory -> entry Symtab.table
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    15
end;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    16
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    17
structure Functorial_Mappers : FUNCTORIAL_MAPPERS =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    18
struct
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    19
40594
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
    20
val concatenateN = "concatenate";
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
    21
val identityN = "identity";
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
    22
40582
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    23
(** functorial mappers and their properties **)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    24
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    25
(* bookkeeping *)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    26
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    27
type entry = { mapper: string, variances: (sort * (bool * bool)) list,
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    28
  concatenate: thm, identity: thm };
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    29
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    30
structure Data = Theory_Data(
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    31
  type T = entry Symtab.table
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    32
  val empty = Symtab.empty
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    33
  val merge = Symtab.merge (K true)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    34
  val extend = I
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    35
);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    36
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    37
val entries = Data.get;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    38
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    39
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    40
(* type analysis *)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    41
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    42
fun find_atomic thy T =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    43
  let
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    44
    val variances_of = Option.map #variances o Symtab.lookup (Data.get thy);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    45
    fun add_variance is_contra T =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    46
      AList.map_default (op =) (T, (false, false))
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    47
        ((if is_contra then apsnd else apfst) (K true));
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    48
    fun analyze' is_contra (_, (co, contra)) T =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    49
      (if co then analyze is_contra T else I)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    50
      #> (if contra then analyze (not is_contra) T else I)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    51
    and analyze is_contra (T as Type (tyco, Ts)) = (case variances_of tyco
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    52
          of NONE => add_variance is_contra T
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    53
           | SOME variances => fold2 (analyze' is_contra) variances Ts)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    54
      | analyze is_contra T = add_variance is_contra T;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    55
  in analyze false T [] end;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    56
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    57
fun construct_mapper thy atomic =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    58
  let
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    59
    val lookup = the o Symtab.lookup (Data.get thy);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    60
    fun constructs is_contra (_, (co, contra)) T T' =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    61
      (if co then [construct is_contra T T'] else [])
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    62
      @ (if contra then [construct (not is_contra) T T'] else [])
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    63
    and construct is_contra (T as Type (tyco, Ts)) (T' as Type (_, Ts')) =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    64
          let
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    65
            val { mapper, variances, ... } = lookup tyco;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    66
            val args = maps (fn (arg_pattern, (T, T')) =>
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    67
              constructs is_contra arg_pattern T T')
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    68
                (variances ~~ (Ts ~~ Ts'));
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    69
            val (U, U') = if is_contra then (T', T) else (T, T');
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    70
          in list_comb (Const (mapper, map fastype_of args ---> U --> U'), args) end
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    71
      | construct is_contra (TFree (v, _)) (TFree _) = atomic (v, is_contra);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    72
  in construct end;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    73
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    74
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    75
(* mapper properties *)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    76
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    77
fun make_concatenate_prop variances (tyco, mapper) =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    78
  let
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    79
    fun invents n k nctxt =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    80
      let
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    81
        val names = Name.invents nctxt n k;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    82
      in (names, fold Name.declare names nctxt) end;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    83
    val (((vs1, vs2), vs3), _) = Name.context
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
      ||>> invents Name.aT (length variances)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    86
      ||>> invents Name.aT (length variances);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    87
    fun mk_Ts vs = map2 (fn v => fn (sort, _) => TFree (v, sort))
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    88
      vs variances;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    89
    val (Ts1, Ts2, Ts3) = (mk_Ts vs1, mk_Ts vs2, mk_Ts vs3);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    90
    fun mk_argT ((T, T'), (_, (co, contra))) =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    91
      (if co then [(T --> T')] else [])
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    92
      @ (if contra then [(T' --> T)] else []);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    93
    val contras = maps (fn (_, (co, contra)) =>
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    94
      (if co then [false] else []) @ (if contra then [true] else [])) variances;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    95
    val Ts21 = maps mk_argT ((Ts2 ~~ Ts1) ~~ variances);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    96
    val Ts32 = maps mk_argT ((Ts3 ~~ Ts2) ~~ variances);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    97
    val ((names21, names32), nctxt) = Name.context
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    98
      |> invents "f" (length Ts21)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
    99
      ||>> invents "f" (length Ts32);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   100
    val T1 = Type (tyco, Ts1);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   101
    val T2 = Type (tyco, Ts2);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   102
    val T3 = Type (tyco, Ts3);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   103
    val x = Free (the_single (Name.invents nctxt "a" 1), T3);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   104
    val (args21, args32) = (names21 ~~ Ts21, names32 ~~ Ts32);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   105
    val args31 = map2 (fn is_contra => fn ((f21, T21), (f32, T32)) =>
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   106
      if not is_contra then
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   107
        Abs ("x", domain_type T32, Free (f21, T21) $ (Free (f32, T32) $ Bound 0))
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   108
      else
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   109
        Abs ("x", domain_type T21, Free (f32, T32) $ (Free (f21, T21) $ Bound 0))
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   110
      ) contras (args21 ~~ args32)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   111
    fun mk_mapper T T' args = list_comb (Const (mapper,
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   112
      map fastype_of args ---> T --> T'), args);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   113
    val lhs = mk_mapper T2 T1 (map Free args21) $
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   114
      (mk_mapper T3 T2 (map Free args32) $ x);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   115
    val rhs = mk_mapper T3 T1 args31 $ x;
40597
19b449037ace keep variables bound
haftmann
parents: 40594
diff changeset
   116
  in (map Free (args21 @ args32) @ [x], (HOLogic.mk_Trueprop o HOLogic.mk_eq) (lhs, rhs)) end;
40582
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   117
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   118
fun make_identity_prop variances (tyco, mapper) =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   119
  let
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   120
    val vs = Name.invents Name.context Name.aT (length variances);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   121
    val Ts = map2 (fn v => fn (sort, _) => TFree (v, sort)) vs variances;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   122
    fun bool_num b = if b then 1 else 0;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   123
    fun mk_argT (T, (_, (co, contra))) =
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   124
      replicate (bool_num co + bool_num contra) (T --> T)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   125
    val Ts' = maps mk_argT (Ts ~~ variances)
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   126
    val T = Type (tyco, Ts);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   127
    val x = Free ("a", T);
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   128
    val lhs = list_comb (Const (mapper, Ts' ---> T --> T),
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   129
      map (fn T => Abs ("x", domain_type T, Bound 0)) Ts') $ x;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   130
  in (x, (HOLogic.mk_Trueprop o HOLogic.mk_eq) (lhs, x)) end;
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   131
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   132
40597
19b449037ace keep variables bound
haftmann
parents: 40594
diff changeset
   133
(* analyzing and registering mappers *)
40582
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   134
40594
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   135
fun consume eq x [] = (false, [])
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   136
  | consume eq x (ys as z :: zs) = if eq (x, z) then (true, zs) else (false, ys);
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   137
40587
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   138
fun split_mapper_typ "fun" T =
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   139
      let
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   140
        val (Ts', T') = strip_type T;
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   141
        val (Ts'', T'') = split_last Ts';
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   142
        val (Ts''', T''') = split_last Ts'';
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   143
      in (Ts''', T''', T'' --> T') end
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   144
  | split_mapper_typ tyco T =
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   145
      let
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   146
        val (Ts', T') = strip_type T;
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   147
        val (Ts'', T'') = split_last Ts';
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   148
      in (Ts'', T'', T') end;
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   149
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   150
fun analyze_variances thy tyco T =
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   151
  let
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   152
    fun bad_typ () = error ("Bad mapper type: " ^ Syntax.string_of_typ_global thy T);
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   153
    val (Ts, T1, T2) = split_mapper_typ tyco T
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   154
      handle List.Empty => bad_typ ();
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   155
    val _ = pairself
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   156
      ((fn tyco' => if tyco' = tyco then () else bad_typ ()) o fst o dest_Type) (T1, T2)
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   157
    val (vs1, vs2) = pairself (map dest_TFree o snd o dest_Type) (T1, T2)
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   158
      handle TYPE _ => bad_typ ();
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   159
    val _ = if has_duplicates (eq_fst (op =)) (vs1 @ vs2)
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   160
      then bad_typ () else ();
40594
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   161
    fun check_variance_pair (var1 as (v1, sort1), var2 as (v2, sort2)) =
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   162
      let
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   163
        val coT = TFree var1 --> TFree var2;
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   164
        val contraT = TFree var2 --> TFree var1;
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   165
        val sort = Sign.inter_sort thy (sort1, sort2);
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   166
      in
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   167
        consume (op =) coT
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   168
        ##>> consume (op =) contraT
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   169
        #>> pair sort
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   170
      end;
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   171
    val (variances, left_variances) = fold_map check_variance_pair (vs1 ~~ vs2) Ts;
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   172
    val _ = if null left_variances then () else bad_typ ();
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   173
  in variances end;
40587
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   174
40583
6fed6f946471 stub for Isar command interface
haftmann
parents: 40582
diff changeset
   175
fun gen_type_mapper prep_term raw_t thy =
6fed6f946471 stub for Isar command interface
haftmann
parents: 40582
diff changeset
   176
  let
40587
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   177
    val (mapper, T) = case prep_term thy raw_t
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   178
     of Const cT => cT
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   179
      | t => error ("No constant: " ^ Syntax.string_of_term_global thy t);
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   180
    val _ = Type.no_tvars T;
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   181
    fun add_tycos (Type (tyco, Ts)) = insert (op =) tyco #> fold add_tycos Ts
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   182
      | add_tycos _ = I;
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   183
    val tycos = add_tycos T [];
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   184
    val tyco = if tycos = ["fun"] then "fun"
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   185
      else case remove (op =) "fun" tycos
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   186
       of [tyco] => tyco
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   187
        | _ => error ("Bad number of type constructors: " ^ Syntax.string_of_typ_global thy T);
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   188
    val variances = analyze_variances thy tyco T;
40597
19b449037ace keep variables bound
haftmann
parents: 40594
diff changeset
   189
    val concatenate_prop = uncurry (fold_rev Logic.all)
40594
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   190
      (make_concatenate_prop variances (tyco, mapper));
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   191
    val identity_prop = uncurry Logic.all
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   192
      (make_identity_prop variances (tyco, mapper));
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   193
    val qualify = Binding.qualify true (Long_Name.base_name mapper) o Binding.name;
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   194
    fun after_qed [single_concatenate, single_identity] lthy =
40587
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   195
      lthy
40594
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   196
      |> Local_Theory.note ((qualify concatenateN, []), single_concatenate)
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   197
      ||>> Local_Theory.note ((qualify identityN, []), single_identity)
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   198
      |-> (fn ((_, [concatenate]), (_, [identity])) =>
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   199
          (Local_Theory.background_theory o Data.map)
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   200
            (Symtab.update (tyco, { mapper = mapper, variances = variances,
fae1da97bb5e infer variances of user-given mapper operation; proper thm storing
haftmann
parents: 40587
diff changeset
   201
              concatenate = concatenate, identity = identity })));
40583
6fed6f946471 stub for Isar command interface
haftmann
parents: 40582
diff changeset
   202
  in
6fed6f946471 stub for Isar command interface
haftmann
parents: 40582
diff changeset
   203
    thy
6fed6f946471 stub for Isar command interface
haftmann
parents: 40582
diff changeset
   204
    |> Named_Target.theory_init
40587
5206d19038c7 emerging Isar command interface
haftmann
parents: 40586
diff changeset
   205
    |> Proof.theorem NONE after_qed (map (fn t => [(t, [])]) [concatenate_prop, identity_prop])
40583
6fed6f946471 stub for Isar command interface
haftmann
parents: 40582
diff changeset
   206
  end
6fed6f946471 stub for Isar command interface
haftmann
parents: 40582
diff changeset
   207
6fed6f946471 stub for Isar command interface
haftmann
parents: 40582
diff changeset
   208
val type_mapper = gen_type_mapper Sign.cert_term;
6fed6f946471 stub for Isar command interface
haftmann
parents: 40582
diff changeset
   209
val type_mapper_cmd = gen_type_mapper Syntax.read_term_global;
6fed6f946471 stub for Isar command interface
haftmann
parents: 40582
diff changeset
   210
6fed6f946471 stub for Isar command interface
haftmann
parents: 40582
diff changeset
   211
val _ =
6fed6f946471 stub for Isar command interface
haftmann
parents: 40582
diff changeset
   212
  Outer_Syntax.command "type_mapper" "register functorial mapper for type with its properties" Keyword.thy_goal
6fed6f946471 stub for Isar command interface
haftmann
parents: 40582
diff changeset
   213
    (Parse.term >> (fn t => Toplevel.print o (Toplevel.theory_to_proof (type_mapper_cmd t))));
6fed6f946471 stub for Isar command interface
haftmann
parents: 40582
diff changeset
   214
40582
968c481aa18c module for functorial mappers
haftmann
parents:
diff changeset
   215
end;