src/HOLCF/Tools/Domain/domain_constructors.ML
author wenzelm
Fri May 28 18:15:22 2010 +0200 (2010-05-28 ago)
changeset 37165 c2e27ae53c2a
parent 37109 e67760c1b851
child 37744 3daaf23b9ab4
permissions -rw-r--r--
made SML/NJ quite happy;
huffman@35444
     1
(*  Title:      HOLCF/Tools/domain/domain_constructors.ML
huffman@35444
     2
    Author:     Brian Huffman
huffman@35444
     3
huffman@35444
     4
Defines constructor functions for a given domain isomorphism
huffman@35444
     5
and proves related theorems.
huffman@35444
     6
*)
huffman@35444
     7
huffman@35444
     8
signature DOMAIN_CONSTRUCTORS =
huffman@35444
     9
sig
huffman@35444
    10
  val add_domain_constructors :
huffman@35777
    11
      binding
huffman@35481
    12
      -> (binding * (bool * binding option * typ) list * mixfix) list
huffman@35514
    13
      -> Domain_Take_Proofs.iso_info
huffman@35444
    14
      -> theory
huffman@35444
    15
      -> { con_consts : term list,
huffman@35451
    16
           con_betas : thm list,
huffman@35781
    17
           nchotomy : thm,
huffman@35457
    18
           exhaust : thm,
huffman@35781
    19
           compacts : thm list,
huffman@35452
    20
           con_rews : thm list,
huffman@35456
    21
           inverts : thm list,
huffman@35456
    22
           injects : thm list,
huffman@35458
    23
           dist_les : thm list,
huffman@35458
    24
           dist_eqs : thm list,
huffman@35459
    25
           cases : thm list,
huffman@35460
    26
           sel_rews : thm list,
huffman@35462
    27
           dis_rews : thm list,
huffman@37109
    28
           match_rews : thm list
huffman@35453
    29
         } * theory;
huffman@35444
    30
end;
huffman@35444
    31
huffman@35444
    32
huffman@35444
    33
structure Domain_Constructors :> DOMAIN_CONSTRUCTORS =
huffman@35444
    34
struct
huffman@35444
    35
huffman@35475
    36
open HOLCF_Library;
huffman@35561
    37
huffman@35476
    38
infixr 6 ->>;
huffman@35476
    39
infix -->>;
huffman@35561
    40
infix 9 `;
huffman@35444
    41
huffman@35446
    42
(************************** miscellaneous functions ***************************)
huffman@35446
    43
huffman@37078
    44
val simple_ss = HOL_basic_ss addsimps simp_thms;
huffman@35456
    45
huffman@37078
    46
val beta_rules =
huffman@37078
    47
  @{thms beta_cfun cont_id cont_const cont2cont_Rep_CFun cont2cont_LAM'} @
huffman@37078
    48
  @{thms cont2cont_fst cont2cont_snd cont2cont_Pair};
huffman@37078
    49
huffman@37078
    50
val beta_ss = HOL_basic_ss addsimps (simp_thms @ beta_rules);
huffman@35459
    51
huffman@35444
    52
fun define_consts
huffman@35444
    53
    (specs : (binding * term * mixfix) list)
huffman@35444
    54
    (thy : theory)
huffman@35444
    55
    : (term list * thm list) * theory =
huffman@35444
    56
  let
huffman@35456
    57
    fun mk_decl (b, t, mx) = (b, fastype_of t, mx);
huffman@35444
    58
    val decls = map mk_decl specs;
huffman@35444
    59
    val thy = Cont_Consts.add_consts decls thy;
huffman@35444
    60
    fun mk_const (b, T, mx) = Const (Sign.full_name thy b, T);
huffman@35444
    61
    val consts = map mk_const decls;
huffman@35444
    62
    fun mk_def c (b, t, mx) =
huffman@35444
    63
      (Binding.suffix_name "_def" b, Logic.mk_equals (c, t));
huffman@35444
    64
    val defs = map2 mk_def consts specs;
huffman@35444
    65
    val (def_thms, thy) =
huffman@35444
    66
      PureThy.add_defs false (map Thm.no_attributes defs) thy;
huffman@35444
    67
  in
huffman@35444
    68
    ((consts, def_thms), thy)
huffman@35444
    69
  end;
huffman@35444
    70
huffman@35449
    71
fun prove
huffman@35446
    72
    (thy : theory)
huffman@35449
    73
    (defs : thm list)
huffman@35447
    74
    (goal : term)
huffman@35449
    75
    (tacs : {prems: thm list, context: Proof.context} -> tactic list)
huffman@35447
    76
    : thm =
huffman@35449
    77
  let
huffman@35449
    78
    fun tac {prems, context} =
huffman@35449
    79
      rewrite_goals_tac defs THEN
huffman@35449
    80
      EVERY (tacs {prems = map (rewrite_rule defs) prems, context = context})
huffman@35449
    81
  in
huffman@35449
    82
    Goal.prove_global thy [] [] goal tac
huffman@35449
    83
  end;
huffman@35445
    84
huffman@35483
    85
fun get_vars_avoiding
huffman@35483
    86
    (taken : string list)
huffman@35483
    87
    (args : (bool * typ) list)
huffman@35483
    88
    : (term list * term list) =
huffman@35483
    89
  let
huffman@35483
    90
    val Ts = map snd args;
huffman@35483
    91
    val ns = Name.variant_list taken (Datatype_Prop.make_tnames Ts);
huffman@35483
    92
    val vs = map Free (ns ~~ Ts);
huffman@35483
    93
    val nonlazy = map snd (filter_out (fst o fst) (args ~~ vs));
huffman@35483
    94
  in
huffman@35483
    95
    (vs, nonlazy)
huffman@35483
    96
  end;
huffman@35483
    97
huffman@35483
    98
fun get_vars args = get_vars_avoiding [] args;
huffman@35483
    99
huffman@35445
   100
(************** generating beta reduction rules from definitions **************)
huffman@35444
   101
huffman@35445
   102
local
huffman@35445
   103
  fun arglist (Const _ $ Abs (s, T, t)) =
huffman@35445
   104
      let
huffman@35445
   105
        val arg = Free (s, T);
huffman@35445
   106
        val (args, body) = arglist (subst_bound (arg, t));
huffman@35445
   107
      in (arg :: args, body) end
huffman@35445
   108
    | arglist t = ([], t);
huffman@35445
   109
in
huffman@35445
   110
  fun beta_of_def thy def_thm =
huffman@35445
   111
      let
huffman@35445
   112
        val (con, lam) = Logic.dest_equals (concl_of def_thm);
huffman@35445
   113
        val (args, rhs) = arglist lam;
huffman@35446
   114
        val lhs = list_ccomb (con, args);
huffman@35445
   115
        val goal = mk_equals (lhs, rhs);
huffman@35445
   116
        val cs = ContProc.cont_thms lam;
huffman@35445
   117
        val betas = map (fn c => mk_meta_eq (c RS @{thm beta_cfun})) cs;
huffman@35445
   118
      in
huffman@35449
   119
        prove thy (def_thm::betas) goal (K [rtac reflexive_thm 1])
huffman@35445
   120
      end;
huffman@35445
   121
end;
huffman@35444
   122
huffman@35446
   123
(******************************************************************************)
huffman@35453
   124
(************* definitions and theorems for constructor functions *************)
huffman@35453
   125
(******************************************************************************)
huffman@35453
   126
huffman@35453
   127
fun add_constructors
huffman@35454
   128
    (spec : (binding * (bool * typ) list * mixfix) list)
huffman@35453
   129
    (abs_const : term)
huffman@35453
   130
    (iso_locale : thm)
huffman@35453
   131
    (thy : theory)
huffman@35453
   132
    =
huffman@35453
   133
  let
huffman@35453
   134
huffman@35453
   135
    (* get theorems about rep and abs *)
huffman@35453
   136
    val abs_strict = iso_locale RS @{thm iso.abs_strict};
huffman@35453
   137
huffman@35457
   138
    (* get types of type isomorphism *)
huffman@35457
   139
    val (rhsT, lhsT) = dest_cfunT (fastype_of abs_const);
huffman@35457
   140
huffman@35455
   141
    fun vars_of args =
huffman@35455
   142
      let
huffman@35455
   143
        val Ts = map snd args;
huffman@35455
   144
        val ns = Datatype_Prop.make_tnames Ts;
huffman@35455
   145
      in
huffman@35455
   146
        map Free (ns ~~ Ts)
huffman@35455
   147
      end;
huffman@35455
   148
huffman@35453
   149
    (* define constructor functions *)
huffman@35453
   150
    val ((con_consts, con_defs), thy) =
huffman@35453
   151
      let
huffman@35454
   152
        fun one_arg (lazy, T) var = if lazy then mk_up var else var;
huffman@35453
   153
        fun one_con (_,args,_) = mk_stuple (map2 one_arg args (vars_of args));
huffman@35453
   154
        fun mk_abs t = abs_const ` t;
huffman@35453
   155
        val rhss = map mk_abs (mk_sinjects (map one_con spec));
huffman@35453
   156
        fun mk_def (bind, args, mx) rhs =
huffman@35453
   157
          (bind, big_lambdas (vars_of args) rhs, mx);
huffman@35453
   158
      in
huffman@35453
   159
        define_consts (map2 mk_def spec rhss) thy
huffman@35453
   160
      end;
huffman@35453
   161
huffman@35453
   162
    (* prove beta reduction rules for constructors *)
huffman@35453
   163
    val con_betas = map (beta_of_def thy) con_defs;
huffman@35453
   164
huffman@35453
   165
    (* replace bindings with terms in constructor spec *)
huffman@35453
   166
    val spec' : (term * (bool * typ) list) list =
huffman@35454
   167
      let fun one_con con (b, args, mx) = (con, args);
huffman@35453
   168
      in map2 one_con con_consts spec end;
huffman@35453
   169
huffman@35457
   170
    (* prove exhaustiveness of constructors *)
huffman@35457
   171
    local
huffman@35457
   172
      fun arg2typ n (true,  T) = (n+1, mk_upT (TVar (("'a", n), @{sort cpo})))
huffman@35457
   173
        | arg2typ n (false, T) = (n+1, TVar (("'a", n), @{sort pcpo}));
huffman@35457
   174
      fun args2typ n [] = (n, oneT)
huffman@35457
   175
        | args2typ n [arg] = arg2typ n arg
huffman@35457
   176
        | args2typ n (arg::args) =
huffman@35457
   177
          let
huffman@35457
   178
            val (n1, t1) = arg2typ n arg;
huffman@35457
   179
            val (n2, t2) = args2typ n1 args
huffman@35457
   180
          in (n2, mk_sprodT (t1, t2)) end;
huffman@35457
   181
      fun cons2typ n [] = (n, oneT)
huffman@35457
   182
        | cons2typ n [con] = args2typ n (snd con)
huffman@35457
   183
        | cons2typ n (con::cons) =
huffman@35457
   184
          let
huffman@35457
   185
            val (n1, t1) = args2typ n (snd con);
huffman@35457
   186
            val (n2, t2) = cons2typ n1 cons
huffman@35457
   187
          in (n2, mk_ssumT (t1, t2)) end;
huffman@35457
   188
      val ct = ctyp_of thy (snd (cons2typ 1 spec'));
huffman@35457
   189
      val thm1 = instantiate' [SOME ct] [] @{thm exh_start};
huffman@35457
   190
      val thm2 = rewrite_rule (map mk_meta_eq @{thms ex_defined_iffs}) thm1;
huffman@35457
   191
      val thm3 = rewrite_rule [mk_meta_eq @{thm conj_assoc}] thm2;
huffman@35457
   192
huffman@35526
   193
      val y = Free ("y", lhsT);
huffman@35457
   194
      fun one_con (con, args) =
huffman@35457
   195
        let
huffman@35526
   196
          val (vs, nonlazy) = get_vars_avoiding ["y"] args;
huffman@35526
   197
          val eqn = mk_eq (y, list_ccomb (con, vs));
huffman@35457
   198
          val conj = foldr1 mk_conj (eqn :: map mk_defined nonlazy);
huffman@35457
   199
        in Library.foldr mk_ex (vs, conj) end;
huffman@35526
   200
      val goal = mk_trp (foldr1 mk_disj (mk_undef y :: map one_con spec'));
huffman@36241
   201
      (* first rules replace "y = UU \/ P" with "rep$y = UU \/ P" *)
huffman@35457
   202
      val tacs = [
huffman@35457
   203
          rtac (iso_locale RS @{thm iso.casedist_rule}) 1,
huffman@35457
   204
          rewrite_goals_tac [mk_meta_eq (iso_locale RS @{thm iso.iso_swap})],
huffman@35457
   205
          rtac thm3 1];
huffman@35457
   206
    in
huffman@35781
   207
      val nchotomy = prove thy con_betas goal (K tacs);
huffman@35781
   208
      val exhaust =
huffman@35781
   209
          (nchotomy RS @{thm exh_casedist0})
huffman@35457
   210
          |> rewrite_rule @{thms exh_casedists}
huffman@36241
   211
          |> Drule.zero_var_indexes;
huffman@35457
   212
    end;
huffman@35457
   213
huffman@35453
   214
    (* prove compactness rules for constructors *)
huffman@35781
   215
    val compacts =
huffman@35453
   216
      let
huffman@35453
   217
        val rules = @{thms compact_sinl compact_sinr compact_spair
huffman@35453
   218
                           compact_up compact_ONE};
huffman@35453
   219
        val tacs =
huffman@35453
   220
          [rtac (iso_locale RS @{thm iso.compact_abs}) 1,
huffman@35453
   221
           REPEAT (resolve_tac rules 1 ORELSE atac 1)];
huffman@35453
   222
        fun con_compact (con, args) =
huffman@35453
   223
          let
huffman@35455
   224
            val vs = vars_of args;
huffman@35453
   225
            val con_app = list_ccomb (con, vs);
huffman@35453
   226
            val concl = mk_trp (mk_compact con_app);
huffman@35453
   227
            val assms = map (mk_trp o mk_compact) vs;
huffman@35453
   228
            val goal = Logic.list_implies (assms, concl);
huffman@35453
   229
          in
huffman@35453
   230
            prove thy con_betas goal (K tacs)
huffman@35453
   231
          end;
huffman@35453
   232
      in
huffman@35453
   233
        map con_compact spec'
huffman@35453
   234
      end;
huffman@35453
   235
huffman@35453
   236
    (* prove strictness rules for constructors *)
huffman@35453
   237
    local
huffman@35453
   238
      fun con_strict (con, args) = 
huffman@35453
   239
        let
huffman@35453
   240
          val rules = abs_strict :: @{thms con_strict_rules};
huffman@35484
   241
          val (vs, nonlazy) = get_vars args;
huffman@35453
   242
          fun one_strict v' =
huffman@35453
   243
            let
huffman@35456
   244
              val UU = mk_bottom (fastype_of v');
huffman@35453
   245
              val vs' = map (fn v => if v = v' then UU else v) vs;
huffman@35453
   246
              val goal = mk_trp (mk_undef (list_ccomb (con, vs')));
huffman@35453
   247
              val tacs = [simp_tac (HOL_basic_ss addsimps rules) 1];
huffman@35453
   248
            in prove thy con_betas goal (K tacs) end;
huffman@35453
   249
        in map one_strict nonlazy end;
huffman@35453
   250
huffman@35453
   251
      fun con_defin (con, args) =
huffman@35453
   252
        let
huffman@35453
   253
          fun iff_disj (t, []) = HOLogic.mk_not t
huffman@35453
   254
            | iff_disj (t, ts) = mk_eq (t, foldr1 HOLogic.mk_disj ts);
huffman@35484
   255
          val (vs, nonlazy) = get_vars args;
huffman@35453
   256
          val lhs = mk_undef (list_ccomb (con, vs));
huffman@35453
   257
          val rhss = map mk_undef nonlazy;
huffman@35453
   258
          val goal = mk_trp (iff_disj (lhs, rhss));
huffman@35453
   259
          val rule1 = iso_locale RS @{thm iso.abs_defined_iff};
huffman@35453
   260
          val rules = rule1 :: @{thms con_defined_iff_rules};
huffman@35453
   261
          val tacs = [simp_tac (HOL_ss addsimps rules) 1];
huffman@35453
   262
        in prove thy con_betas goal (K tacs) end;
huffman@35453
   263
    in
huffman@35453
   264
      val con_stricts = maps con_strict spec';
huffman@35453
   265
      val con_defins = map con_defin spec';
huffman@35453
   266
      val con_rews = con_stricts @ con_defins;
huffman@35453
   267
    end;
huffman@35453
   268
huffman@35456
   269
    (* prove injectiveness of constructors *)
huffman@35456
   270
    local
huffman@35456
   271
      fun pgterm rel (con, args) =
huffman@35456
   272
        let
huffman@35456
   273
          fun prime (Free (n, T)) = Free (n^"'", T)
huffman@35456
   274
            | prime t             = t;
huffman@35484
   275
          val (xs, nonlazy) = get_vars args;
huffman@35456
   276
          val ys = map prime xs;
huffman@35456
   277
          val lhs = rel (list_ccomb (con, xs), list_ccomb (con, ys));
huffman@35456
   278
          val rhs = foldr1 mk_conj (ListPair.map rel (xs, ys));
huffman@35456
   279
          val concl = mk_trp (mk_eq (lhs, rhs));
huffman@35456
   280
          val zs = case args of [_] => [] | _ => nonlazy;
huffman@35456
   281
          val assms = map (mk_trp o mk_defined) zs;
huffman@35456
   282
          val goal = Logic.list_implies (assms, concl);
huffman@35456
   283
        in prove thy con_betas goal end;
huffman@35456
   284
      val cons' = filter (fn (_, args) => not (null args)) spec';
huffman@35456
   285
    in
huffman@35456
   286
      val inverts =
huffman@35456
   287
        let
huffman@35456
   288
          val abs_below = iso_locale RS @{thm iso.abs_below};
huffman@35456
   289
          val rules1 = abs_below :: @{thms sinl_below sinr_below spair_below up_below};
huffman@35456
   290
          val rules2 = @{thms up_defined spair_defined ONE_defined}
huffman@35456
   291
          val rules = rules1 @ rules2;
huffman@35456
   292
          val tacs = [asm_simp_tac (simple_ss addsimps rules) 1];
huffman@35456
   293
        in map (fn c => pgterm mk_below c (K tacs)) cons' end;
huffman@35456
   294
      val injects =
huffman@35456
   295
        let
huffman@35456
   296
          val abs_eq = iso_locale RS @{thm iso.abs_eq};
huffman@35456
   297
          val rules1 = abs_eq :: @{thms sinl_eq sinr_eq spair_eq up_eq};
huffman@35456
   298
          val rules2 = @{thms up_defined spair_defined ONE_defined}
huffman@35456
   299
          val rules = rules1 @ rules2;
huffman@35456
   300
          val tacs = [asm_simp_tac (simple_ss addsimps rules) 1];
huffman@35456
   301
        in map (fn c => pgterm mk_eq c (K tacs)) cons' end;
huffman@35456
   302
    end;
huffman@35456
   303
huffman@35458
   304
    (* prove distinctness of constructors *)
huffman@35458
   305
    local
huffman@35458
   306
      fun map_dist (f : 'a -> 'a -> 'b) (xs : 'a list) : 'b list =
huffman@35458
   307
        flat (map_index (fn (i, x) => map (f x) (nth_drop i xs)) xs);
huffman@35458
   308
      fun prime (Free (n, T)) = Free (n^"'", T)
huffman@35458
   309
        | prime t             = t;
huffman@35458
   310
      fun iff_disj (t, []) = mk_not t
huffman@35458
   311
        | iff_disj (t, ts) = mk_eq (t, foldr1 mk_disj ts);
huffman@35458
   312
      fun iff_disj2 (t, [], us) = mk_not t
huffman@35458
   313
        | iff_disj2 (t, ts, []) = mk_not t
huffman@35458
   314
        | iff_disj2 (t, ts, us) =
huffman@35458
   315
          mk_eq (t, mk_conj (foldr1 mk_disj ts, foldr1 mk_disj us));
huffman@35458
   316
      fun dist_le (con1, args1) (con2, args2) =
huffman@35458
   317
        let
huffman@35484
   318
          val (vs1, zs1) = get_vars args1;
huffman@35484
   319
          val (vs2, zs2) = get_vars args2 |> pairself (map prime);
huffman@35458
   320
          val lhs = mk_below (list_ccomb (con1, vs1), list_ccomb (con2, vs2));
huffman@35458
   321
          val rhss = map mk_undef zs1;
huffman@35458
   322
          val goal = mk_trp (iff_disj (lhs, rhss));
huffman@35458
   323
          val rule1 = iso_locale RS @{thm iso.abs_below};
huffman@35458
   324
          val rules = rule1 :: @{thms con_below_iff_rules};
huffman@35458
   325
          val tacs = [simp_tac (HOL_ss addsimps rules) 1];
huffman@35458
   326
        in prove thy con_betas goal (K tacs) end;
huffman@35458
   327
      fun dist_eq (con1, args1) (con2, args2) =
huffman@35458
   328
        let
huffman@35484
   329
          val (vs1, zs1) = get_vars args1;
huffman@35484
   330
          val (vs2, zs2) = get_vars args2 |> pairself (map prime);
huffman@35458
   331
          val lhs = mk_eq (list_ccomb (con1, vs1), list_ccomb (con2, vs2));
huffman@35458
   332
          val rhss1 = map mk_undef zs1;
huffman@35458
   333
          val rhss2 = map mk_undef zs2;
huffman@35458
   334
          val goal = mk_trp (iff_disj2 (lhs, rhss1, rhss2));
huffman@35458
   335
          val rule1 = iso_locale RS @{thm iso.abs_eq};
huffman@35458
   336
          val rules = rule1 :: @{thms con_eq_iff_rules};
huffman@35458
   337
          val tacs = [simp_tac (HOL_ss addsimps rules) 1];
huffman@35458
   338
        in prove thy con_betas goal (K tacs) end;
huffman@35458
   339
    in
huffman@35458
   340
      val dist_les = map_dist dist_le spec';
huffman@35458
   341
      val dist_eqs = map_dist dist_eq spec';
huffman@35458
   342
    end;
huffman@35456
   343
huffman@35453
   344
    val result =
huffman@35453
   345
      {
huffman@35453
   346
        con_consts = con_consts,
huffman@35453
   347
        con_betas = con_betas,
huffman@35781
   348
        nchotomy = nchotomy,
huffman@35457
   349
        exhaust = exhaust,
huffman@35781
   350
        compacts = compacts,
huffman@35456
   351
        con_rews = con_rews,
huffman@35456
   352
        inverts = inverts,
huffman@35458
   353
        injects = injects,
huffman@35458
   354
        dist_les = dist_les,
huffman@35458
   355
        dist_eqs = dist_eqs
huffman@35453
   356
      };
huffman@35453
   357
  in
huffman@35453
   358
    (result, thy)
huffman@35453
   359
  end;
huffman@35453
   360
huffman@35453
   361
(******************************************************************************)
huffman@35459
   362
(**************** definition and theorems for case combinator *****************)
huffman@35459
   363
(******************************************************************************)
huffman@35459
   364
huffman@35459
   365
fun add_case_combinator
huffman@35459
   366
    (spec : (term * (bool * typ) list) list)
huffman@35459
   367
    (lhsT : typ)
huffman@35777
   368
    (dbind : binding)
huffman@35459
   369
    (con_betas : thm list)
huffman@35781
   370
    (exhaust : thm)
huffman@35459
   371
    (iso_locale : thm)
huffman@35486
   372
    (rep_const : term)
huffman@35460
   373
    (thy : theory)
huffman@35460
   374
    : ((typ -> term) * thm list) * theory =
huffman@35459
   375
  let
huffman@35459
   376
huffman@35459
   377
    (* prove rep/abs rules *)
huffman@35459
   378
    val rep_strict = iso_locale RS @{thm iso.rep_strict};
huffman@35459
   379
    val abs_inverse = iso_locale RS @{thm iso.abs_iso};
huffman@35459
   380
huffman@35459
   381
    (* calculate function arguments of case combinator *)
huffman@35486
   382
    val tns = map (fst o dest_TFree) (snd (dest_Type lhsT));
huffman@35486
   383
    val resultT = TFree (Name.variant tns "'t", @{sort pcpo});
huffman@35460
   384
    fun fTs T = map (fn (_, args) => map snd args -->> T) spec;
huffman@35459
   385
    val fns = Datatype_Prop.indexify_names (map (K "f") spec);
huffman@35460
   386
    val fs = map Free (fns ~~ fTs resultT);
huffman@35460
   387
    fun caseT T = fTs T -->> (lhsT ->> T);
huffman@35459
   388
huffman@35486
   389
    (* definition of case combinator *)
huffman@35486
   390
    local
huffman@35777
   391
      val case_bind = Binding.suffix_name "_when" dbind;
huffman@35784
   392
      fun lambda_arg (lazy, v) t =
huffman@35784
   393
          (if lazy then mk_fup else I) (big_lambda v t);
huffman@35784
   394
      fun lambda_args []      t = mk_one_when t
huffman@35784
   395
        | lambda_args (x::[]) t = lambda_arg x t
huffman@35784
   396
        | lambda_args (x::xs) t = mk_ssplit (lambda_arg x (lambda_args xs t));
huffman@35486
   397
      fun one_con f (_, args) =
huffman@35486
   398
        let
huffman@35784
   399
          val Ts = map snd args;
huffman@35486
   400
          val ns = Name.variant_list fns (Datatype_Prop.make_tnames Ts);
huffman@35486
   401
          val vs = map Free (ns ~~ Ts);
huffman@35486
   402
        in
huffman@35784
   403
          lambda_args (map fst args ~~ vs) (list_ccomb (f, vs))
huffman@35486
   404
        end;
huffman@35785
   405
      fun mk_sscases [t] = mk_strictify t
huffman@35785
   406
        | mk_sscases ts = foldr1 mk_sscase ts;
huffman@35784
   407
      val body = mk_sscases (map2 one_con fs spec);
huffman@35486
   408
      val rhs = big_lambdas fs (mk_cfcomp (body, rep_const));
huffman@35486
   409
      val ((case_consts, case_defs), thy) =
huffman@35486
   410
          define_consts [(case_bind, rhs, NoSyn)] thy;
huffman@35486
   411
      val case_name = Sign.full_name thy case_bind;
huffman@35486
   412
    in
huffman@35486
   413
      val case_def = hd case_defs;
huffman@35486
   414
      fun case_const T = Const (case_name, caseT T);
huffman@35486
   415
      val case_app = list_ccomb (case_const resultT, fs);
huffman@35486
   416
      val thy = thy;
huffman@35486
   417
    end;
huffman@35459
   418
huffman@35472
   419
    (* define syntax for case combinator *)
huffman@35472
   420
    (* TODO: re-implement case syntax using a parse translation *)
huffman@35472
   421
    local
huffman@35472
   422
      open Syntax
huffman@35472
   423
      fun syntax c = Syntax.mark_const (fst (dest_Const c));
huffman@35472
   424
      fun xconst c = Long_Name.base_name (fst (dest_Const c));
huffman@35472
   425
      fun c_ast authentic con =
huffman@35472
   426
          Constant (if authentic then syntax con else xconst con);
huffman@35485
   427
      fun showint n = string_of_int (n+1);
huffman@35485
   428
      fun expvar n = Variable ("e" ^ showint n);
huffman@35485
   429
      fun argvar n (m, _) = Variable ("a" ^ showint n ^ "_" ^ showint m);
huffman@35485
   430
      fun argvars n args = map_index (argvar n) args;
huffman@35472
   431
      fun app s (l, r) = mk_appl (Constant s) [l, r];
huffman@35472
   432
      val cabs = app "_cabs";
huffman@35472
   433
      val capp = app @{const_syntax Rep_CFun};
huffman@35472
   434
      val capps = Library.foldl capp
huffman@35472
   435
      fun con1 authentic n (con,args) =
huffman@35472
   436
          Library.foldl capp (c_ast authentic con, argvars n args);
huffman@35485
   437
      fun case1 authentic (n, c) =
huffman@35472
   438
          app "_case1" (con1 authentic n c, expvar n);
huffman@35485
   439
      fun arg1 (n, (con,args)) = List.foldr cabs (expvar n) (argvars n args);
huffman@35485
   440
      fun when1 n (m, c) =
huffman@35485
   441
          if n = m then arg1 (n, c) else (Constant @{const_syntax UU});
huffman@35472
   442
      val case_constant = Constant (syntax (case_const dummyT));
huffman@35472
   443
      fun case_trans authentic =
huffman@35472
   444
          ParsePrintRule
huffman@35472
   445
            (app "_case_syntax"
huffman@35472
   446
              (Variable "x",
huffman@35485
   447
               foldr1 (app "_case2") (map_index (case1 authentic) spec)),
huffman@35485
   448
             capp (capps (case_constant, map_index arg1 spec), Variable "x"));
huffman@35485
   449
      fun one_abscon_trans authentic (n, c) =
huffman@35472
   450
          ParsePrintRule
huffman@35472
   451
            (cabs (con1 authentic n c, expvar n),
huffman@35485
   452
             capps (case_constant, map_index (when1 n) spec));
huffman@35472
   453
      fun abscon_trans authentic =
huffman@35485
   454
          map_index (one_abscon_trans authentic) spec;
huffman@35472
   455
      val trans_rules : ast Syntax.trrule list =
huffman@35472
   456
          case_trans false :: case_trans true ::
huffman@35472
   457
          abscon_trans false @ abscon_trans true;
huffman@35472
   458
    in
huffman@35472
   459
      val thy = Sign.add_trrules_i trans_rules thy;
huffman@35472
   460
    end;
huffman@35472
   461
huffman@35459
   462
    (* prove beta reduction rule for case combinator *)
huffman@35459
   463
    val case_beta = beta_of_def thy case_def;
huffman@35459
   464
huffman@35459
   465
    (* prove strictness of case combinator *)
huffman@35459
   466
    val case_strict =
huffman@35459
   467
      let
huffman@35486
   468
        val defs = case_beta :: map mk_meta_eq [rep_strict, @{thm cfcomp2}];
huffman@35486
   469
        val goal = mk_trp (mk_strict case_app);
huffman@35496
   470
        val rules = @{thms sscase1 ssplit1 strictify1 one_when1};
huffman@35496
   471
        val tacs = [resolve_tac rules 1];
huffman@35459
   472
      in prove thy defs goal (K tacs) end;
huffman@35459
   473
        
huffman@35459
   474
    (* prove rewrites for case combinator *)
huffman@35459
   475
    local
huffman@35459
   476
      fun one_case (con, args) f =
huffman@35459
   477
        let
huffman@35483
   478
          val (vs, nonlazy) = get_vars args;
huffman@35459
   479
          val assms = map (mk_trp o mk_defined) nonlazy;
huffman@35459
   480
          val lhs = case_app ` list_ccomb (con, vs);
huffman@35459
   481
          val rhs = list_ccomb (f, vs);
huffman@35459
   482
          val concl = mk_trp (mk_eq (lhs, rhs));
huffman@35459
   483
          val goal = Logic.list_implies (assms, concl);
huffman@35459
   484
          val defs = case_beta :: con_betas;
huffman@35488
   485
          val rules1 = @{thms strictify2 sscase2 sscase3 ssplit2 fup2 ID1};
huffman@35459
   486
          val rules2 = @{thms con_defined_iff_rules};
huffman@35486
   487
          val rules3 = @{thms cfcomp2 one_when2};
huffman@35486
   488
          val rules = abs_inverse :: rules1 @ rules2 @ rules3;
huffman@35459
   489
          val tacs = [asm_simp_tac (beta_ss addsimps rules) 1];
huffman@35459
   490
        in prove thy defs goal (K tacs) end;
huffman@35459
   491
    in
huffman@35459
   492
      val case_apps = map2 one_case spec fs;
huffman@35459
   493
    end
huffman@35459
   494
huffman@35459
   495
  in
huffman@35460
   496
    ((case_const, case_strict :: case_apps), thy)
huffman@35459
   497
  end
huffman@35459
   498
huffman@35459
   499
(******************************************************************************)
huffman@35446
   500
(************** definitions and theorems for selector functions ***************)
huffman@35446
   501
(******************************************************************************)
huffman@35446
   502
huffman@35446
   503
fun add_selectors
huffman@35446
   504
    (spec : (term * (bool * binding option * typ) list) list)
huffman@35446
   505
    (rep_const : term)
huffman@35446
   506
    (abs_inv : thm)
huffman@35446
   507
    (rep_strict : thm)
huffman@35446
   508
    (rep_strict_iff : thm)
huffman@35446
   509
    (con_betas : thm list)
huffman@35446
   510
    (thy : theory)
huffman@35446
   511
    : thm list * theory =
huffman@35446
   512
  let
huffman@35446
   513
huffman@35446
   514
    (* define selector functions *)
huffman@35446
   515
    val ((sel_consts, sel_defs), thy) =
huffman@35446
   516
      let
huffman@35456
   517
        fun rangeT s = snd (dest_cfunT (fastype_of s));
huffman@35446
   518
        fun mk_outl s = mk_cfcomp (from_sinl (dest_ssumT (rangeT s)), s);
huffman@35446
   519
        fun mk_outr s = mk_cfcomp (from_sinr (dest_ssumT (rangeT s)), s);
huffman@35446
   520
        fun mk_sfst s = mk_cfcomp (sfst_const (dest_sprodT (rangeT s)), s);
huffman@35446
   521
        fun mk_ssnd s = mk_cfcomp (ssnd_const (dest_sprodT (rangeT s)), s);
huffman@35446
   522
        fun mk_down s = mk_cfcomp (from_up (dest_upT (rangeT s)), s);
huffman@35446
   523
huffman@35446
   524
        fun sels_of_arg s (lazy, NONE,   T) = []
huffman@35446
   525
          | sels_of_arg s (lazy, SOME b, T) =
huffman@35446
   526
            [(b, if lazy then mk_down s else s, NoSyn)];
huffman@35446
   527
        fun sels_of_args s [] = []
huffman@35446
   528
          | sels_of_args s (v :: []) = sels_of_arg s v
huffman@35446
   529
          | sels_of_args s (v :: vs) =
huffman@35446
   530
            sels_of_arg (mk_sfst s) v @ sels_of_args (mk_ssnd s) vs;
huffman@35446
   531
        fun sels_of_cons s [] = []
huffman@35446
   532
          | sels_of_cons s ((con, args) :: []) = sels_of_args s args
huffman@35446
   533
          | sels_of_cons s ((con, args) :: cs) =
huffman@35446
   534
            sels_of_args (mk_outl s) args @ sels_of_cons (mk_outr s) cs;
huffman@35446
   535
        val sel_eqns : (binding * term * mixfix) list =
huffman@35446
   536
            sels_of_cons rep_const spec;
huffman@35446
   537
      in
huffman@35446
   538
        define_consts sel_eqns thy
huffman@35446
   539
      end
huffman@35446
   540
huffman@35446
   541
    (* replace bindings with terms in constructor spec *)
huffman@35446
   542
    val spec2 : (term * (bool * term option * typ) list) list =
huffman@35446
   543
      let
huffman@35446
   544
        fun prep_arg (lazy, NONE, T) sels = ((lazy, NONE, T), sels)
huffman@35446
   545
          | prep_arg (lazy, SOME _, T) sels =
huffman@35446
   546
            ((lazy, SOME (hd sels), T), tl sels);
huffman@35446
   547
        fun prep_con (con, args) sels =
huffman@35446
   548
            apfst (pair con) (fold_map prep_arg args sels);
huffman@35446
   549
      in
huffman@35446
   550
        fst (fold_map prep_con spec sel_consts)
huffman@35446
   551
      end;
huffman@35446
   552
huffman@35446
   553
    (* prove selector strictness rules *)
huffman@35446
   554
    val sel_stricts : thm list =
huffman@35446
   555
      let
huffman@35449
   556
        val rules = rep_strict :: @{thms sel_strict_rules};
huffman@35449
   557
        val tacs = [simp_tac (HOL_basic_ss addsimps rules) 1];
huffman@35446
   558
        fun sel_strict sel =
huffman@35446
   559
          let
huffman@35446
   560
            val goal = mk_trp (mk_strict sel);
huffman@35446
   561
          in
huffman@35449
   562
            prove thy sel_defs goal (K tacs)
huffman@35446
   563
          end
huffman@35446
   564
      in
huffman@35446
   565
        map sel_strict sel_consts
huffman@35446
   566
      end
huffman@35446
   567
huffman@35446
   568
    (* prove selector application rules *)
huffman@35446
   569
    val sel_apps : thm list =
huffman@35446
   570
      let
huffman@35449
   571
        val defs = con_betas @ sel_defs;
huffman@35456
   572
        val rules = abs_inv :: @{thms sel_app_rules};
huffman@35456
   573
        val tacs = [asm_simp_tac (simple_ss addsimps rules) 1];
wenzelm@37165
   574
        fun sel_apps_of (i, (con, args: (bool * term option * typ) list)) =
huffman@35446
   575
          let
huffman@35446
   576
            val Ts : typ list = map #3 args;
huffman@35446
   577
            val ns : string list = Datatype_Prop.make_tnames Ts;
huffman@35446
   578
            val vs : term list = map Free (ns ~~ Ts);
huffman@35446
   579
            val con_app : term = list_ccomb (con, vs);
huffman@35446
   580
            val vs' : (bool * term) list = map #1 args ~~ vs;
huffman@35446
   581
            fun one_same (n, sel, T) =
huffman@35446
   582
              let
huffman@35446
   583
                val xs = map snd (filter_out fst (nth_drop n vs'));
huffman@35446
   584
                val assms = map (mk_trp o mk_defined) xs;
huffman@35446
   585
                val concl = mk_trp (mk_eq (sel ` con_app, nth vs n));
huffman@35446
   586
                val goal = Logic.list_implies (assms, concl);
huffman@35446
   587
              in
huffman@35449
   588
                prove thy defs goal (K tacs)
huffman@35446
   589
              end;
huffman@35446
   590
            fun one_diff (n, sel, T) =
huffman@35446
   591
              let
huffman@35446
   592
                val goal = mk_trp (mk_eq (sel ` con_app, mk_bottom T));
huffman@35446
   593
              in
huffman@35449
   594
                prove thy defs goal (K tacs)
huffman@35446
   595
              end;
huffman@35446
   596
            fun one_con (j, (_, args')) : thm list =
huffman@35446
   597
              let
huffman@35446
   598
                fun prep (i, (lazy, NONE, T)) = NONE
huffman@35446
   599
                  | prep (i, (lazy, SOME sel, T)) = SOME (i, sel, T);
huffman@35446
   600
                val sels : (int * term * typ) list =
huffman@35446
   601
                  map_filter prep (map_index I args');
huffman@35446
   602
              in
huffman@35446
   603
                if i = j
huffman@35446
   604
                then map one_same sels
huffman@35446
   605
                else map one_diff sels
huffman@35446
   606
              end
huffman@35446
   607
          in
huffman@35446
   608
            flat (map_index one_con spec2)
huffman@35446
   609
          end
huffman@35446
   610
      in
huffman@35446
   611
        flat (map_index sel_apps_of spec2)
huffman@35446
   612
      end
huffman@35446
   613
huffman@35446
   614
  (* prove selector definedness rules *)
huffman@35446
   615
    val sel_defins : thm list =
huffman@35446
   616
      let
huffman@35449
   617
        val rules = rep_strict_iff :: @{thms sel_defined_iff_rules};
huffman@35449
   618
        val tacs = [simp_tac (HOL_basic_ss addsimps rules) 1];
huffman@35446
   619
        fun sel_defin sel =
huffman@35446
   620
          let
huffman@35456
   621
            val (T, U) = dest_cfunT (fastype_of sel);
huffman@35446
   622
            val x = Free ("x", T);
huffman@35446
   623
            val lhs = mk_eq (sel ` x, mk_bottom U);
huffman@35446
   624
            val rhs = mk_eq (x, mk_bottom T);
huffman@35446
   625
            val goal = mk_trp (mk_eq (lhs, rhs));
huffman@35446
   626
          in
huffman@35449
   627
            prove thy sel_defs goal (K tacs)
huffman@35446
   628
          end
huffman@35446
   629
        fun one_arg (false, SOME sel, T) = SOME (sel_defin sel)
huffman@35446
   630
          | one_arg _                    = NONE;
huffman@35446
   631
      in
huffman@35446
   632
        case spec2 of
huffman@35446
   633
          [(con, args)] => map_filter one_arg args
huffman@35446
   634
        | _             => []
huffman@35446
   635
      end;
huffman@35446
   636
huffman@35446
   637
  in
huffman@35446
   638
    (sel_stricts @ sel_defins @ sel_apps, thy)
huffman@35446
   639
  end
huffman@35446
   640
huffman@35446
   641
(******************************************************************************)
huffman@35460
   642
(************ definitions and theorems for discriminator functions ************)
huffman@35460
   643
(******************************************************************************)
huffman@35460
   644
huffman@35460
   645
fun add_discriminators
huffman@35460
   646
    (bindings : binding list)
huffman@35460
   647
    (spec : (term * (bool * typ) list) list)
huffman@35461
   648
    (lhsT : typ)
huffman@35781
   649
    (exhaust : thm)
huffman@35460
   650
    (case_const : typ -> term)
huffman@35461
   651
    (case_rews : thm list)
huffman@35460
   652
    (thy : theory) =
huffman@35460
   653
  let
huffman@35460
   654
huffman@35460
   655
    fun vars_of args =
huffman@35460
   656
      let
huffman@35460
   657
        val Ts = map snd args;
huffman@35460
   658
        val ns = Datatype_Prop.make_tnames Ts;
huffman@35460
   659
      in
huffman@35460
   660
        map Free (ns ~~ Ts)
huffman@35460
   661
      end;
huffman@35460
   662
huffman@35460
   663
    (* define discriminator functions *)
huffman@35460
   664
    local
huffman@35460
   665
      fun dis_fun i (j, (con, args)) =
huffman@35460
   666
        let
huffman@35483
   667
          val (vs, nonlazy) = get_vars args;
huffman@35460
   668
          val tr = if i = j then @{term TT} else @{term FF};
huffman@35460
   669
        in
huffman@35460
   670
          big_lambdas vs tr
huffman@35460
   671
        end;
huffman@35460
   672
      fun dis_eqn (i, bind) : binding * term * mixfix =
huffman@35460
   673
        let
huffman@35460
   674
          val dis_bind = Binding.prefix_name "is_" bind;
huffman@35460
   675
          val rhs = list_ccomb (case_const trT, map_index (dis_fun i) spec);
huffman@35460
   676
        in
huffman@35460
   677
          (dis_bind, rhs, NoSyn)
huffman@35460
   678
        end;
huffman@35460
   679
    in
huffman@35460
   680
      val ((dis_consts, dis_defs), thy) =
huffman@35460
   681
          define_consts (map_index dis_eqn bindings) thy
huffman@35460
   682
    end;
huffman@35460
   683
huffman@35461
   684
    (* prove discriminator strictness rules *)
huffman@35461
   685
    local
huffman@35461
   686
      fun dis_strict dis =
huffman@35461
   687
        let val goal = mk_trp (mk_strict dis);
huffman@35461
   688
        in prove thy dis_defs goal (K [rtac (hd case_rews) 1]) end;
huffman@35461
   689
    in
huffman@35461
   690
      val dis_stricts = map dis_strict dis_consts;
huffman@35461
   691
    end;
huffman@35461
   692
huffman@35461
   693
    (* prove discriminator/constructor rules *)
huffman@35461
   694
    local
huffman@35461
   695
      fun dis_app (i, dis) (j, (con, args)) =
huffman@35461
   696
        let
huffman@35483
   697
          val (vs, nonlazy) = get_vars args;
huffman@35461
   698
          val lhs = dis ` list_ccomb (con, vs);
huffman@35461
   699
          val rhs = if i = j then @{term TT} else @{term FF};
huffman@35461
   700
          val assms = map (mk_trp o mk_defined) nonlazy;
huffman@35461
   701
          val concl = mk_trp (mk_eq (lhs, rhs));
huffman@35461
   702
          val goal = Logic.list_implies (assms, concl);
huffman@35461
   703
          val tacs = [asm_simp_tac (beta_ss addsimps case_rews) 1];
huffman@35461
   704
        in prove thy dis_defs goal (K tacs) end;
huffman@35461
   705
      fun one_dis (i, dis) =
huffman@35461
   706
          map_index (dis_app (i, dis)) spec;
huffman@35461
   707
    in
huffman@35461
   708
      val dis_apps = flat (map_index one_dis dis_consts);
huffman@35461
   709
    end;
huffman@35461
   710
huffman@35461
   711
    (* prove discriminator definedness rules *)
huffman@35461
   712
    local
huffman@35461
   713
      fun dis_defin dis =
huffman@35461
   714
        let
huffman@35461
   715
          val x = Free ("x", lhsT);
huffman@35461
   716
          val simps = dis_apps @ @{thms dist_eq_tr};
huffman@35461
   717
          val tacs =
huffman@35461
   718
            [rtac @{thm iffI} 1,
huffman@35461
   719
             asm_simp_tac (HOL_basic_ss addsimps dis_stricts) 2,
huffman@35781
   720
             rtac exhaust 1, atac 1,
huffman@35461
   721
             DETERM_UNTIL_SOLVED (CHANGED
huffman@35461
   722
               (asm_full_simp_tac (simple_ss addsimps simps) 1))];
huffman@35461
   723
          val goal = mk_trp (mk_eq (mk_undef (dis ` x), mk_undef x));
huffman@35461
   724
        in prove thy [] goal (K tacs) end;
huffman@35461
   725
    in
huffman@35461
   726
      val dis_defins = map dis_defin dis_consts;
huffman@35461
   727
    end;
huffman@35461
   728
huffman@35460
   729
  in
huffman@35461
   730
    (dis_stricts @ dis_defins @ dis_apps, thy)
huffman@35460
   731
  end;
huffman@35460
   732
huffman@35460
   733
(******************************************************************************)
huffman@35462
   734
(*************** definitions and theorems for match combinators ***************)
huffman@35462
   735
(******************************************************************************)
huffman@35462
   736
huffman@35462
   737
fun add_match_combinators
huffman@35462
   738
    (bindings : binding list)
huffman@35462
   739
    (spec : (term * (bool * typ) list) list)
huffman@35462
   740
    (lhsT : typ)
huffman@35781
   741
    (exhaust : thm)
huffman@35462
   742
    (case_const : typ -> term)
huffman@35462
   743
    (case_rews : thm list)
huffman@35462
   744
    (thy : theory) =
huffman@35462
   745
  let
huffman@35462
   746
huffman@35462
   747
    (* get a fresh type variable for the result type *)
huffman@35462
   748
    val resultT : typ =
huffman@35462
   749
      let
huffman@35462
   750
        val ts : string list = map (fst o dest_TFree) (snd (dest_Type lhsT));
huffman@35462
   751
        val t : string = Name.variant ts "'t";
huffman@35462
   752
      in TFree (t, @{sort pcpo}) end;
huffman@35462
   753
huffman@35462
   754
    (* define match combinators *)
huffman@35462
   755
    local
huffman@35462
   756
      val x = Free ("x", lhsT);
huffman@35462
   757
      fun k args = Free ("k", map snd args -->> mk_matchT resultT);
huffman@35462
   758
      val fail = mk_fail resultT;
huffman@35462
   759
      fun mat_fun i (j, (con, args)) =
huffman@35462
   760
        let
huffman@35483
   761
          val (vs, nonlazy) = get_vars_avoiding ["x","k"] args;
huffman@35462
   762
        in
huffman@35462
   763
          if i = j then k args else big_lambdas vs fail
huffman@35462
   764
        end;
huffman@35462
   765
      fun mat_eqn (i, (bind, (con, args))) : binding * term * mixfix =
huffman@35462
   766
        let
huffman@35462
   767
          val mat_bind = Binding.prefix_name "match_" bind;
huffman@35462
   768
          val funs = map_index (mat_fun i) spec
huffman@35462
   769
          val body = list_ccomb (case_const (mk_matchT resultT), funs);
huffman@35462
   770
          val rhs = big_lambda x (big_lambda (k args) (body ` x));
huffman@35462
   771
        in
huffman@35462
   772
          (mat_bind, rhs, NoSyn)
huffman@35462
   773
        end;
huffman@35462
   774
    in
huffman@35462
   775
      val ((match_consts, match_defs), thy) =
huffman@35462
   776
          define_consts (map_index mat_eqn (bindings ~~ spec)) thy
huffman@35462
   777
    end;
huffman@35462
   778
huffman@35463
   779
    (* register match combinators with fixrec package *)
huffman@35463
   780
    local
huffman@35463
   781
      val con_names = map (fst o dest_Const o fst) spec;
huffman@35463
   782
      val mat_names = map (fst o dest_Const) match_consts;
huffman@35463
   783
    in
huffman@35463
   784
      val thy = Fixrec.add_matchers (con_names ~~ mat_names) thy;
huffman@35463
   785
    end;
huffman@35463
   786
huffman@35466
   787
    (* prove strictness of match combinators *)
huffman@35466
   788
    local
huffman@35466
   789
      fun match_strict mat =
huffman@35466
   790
        let
huffman@35466
   791
          val (T, (U, V)) = apsnd dest_cfunT (dest_cfunT (fastype_of mat));
huffman@35466
   792
          val k = Free ("k", U);
huffman@35466
   793
          val goal = mk_trp (mk_eq (mat ` mk_bottom T ` k, mk_bottom V));
huffman@35466
   794
          val tacs = [asm_simp_tac (beta_ss addsimps case_rews) 1];
huffman@35466
   795
        in prove thy match_defs goal (K tacs) end;
huffman@35466
   796
    in
huffman@35466
   797
      val match_stricts = map match_strict match_consts;
huffman@35466
   798
    end;
huffman@35466
   799
huffman@35466
   800
    (* prove match/constructor rules *)
huffman@35466
   801
    local
huffman@35466
   802
      val fail = mk_fail resultT;
huffman@35466
   803
      fun match_app (i, mat) (j, (con, args)) =
huffman@35466
   804
        let
huffman@35483
   805
          val (vs, nonlazy) = get_vars_avoiding ["k"] args;
huffman@35466
   806
          val (_, (kT, _)) = apsnd dest_cfunT (dest_cfunT (fastype_of mat));
huffman@35466
   807
          val k = Free ("k", kT);
huffman@35466
   808
          val lhs = mat ` list_ccomb (con, vs) ` k;
huffman@35466
   809
          val rhs = if i = j then list_ccomb (k, vs) else fail;
huffman@35466
   810
          val assms = map (mk_trp o mk_defined) nonlazy;
huffman@35466
   811
          val concl = mk_trp (mk_eq (lhs, rhs));
huffman@35466
   812
          val goal = Logic.list_implies (assms, concl);
huffman@35466
   813
          val tacs = [asm_simp_tac (beta_ss addsimps case_rews) 1];
huffman@35466
   814
        in prove thy match_defs goal (K tacs) end;
huffman@35466
   815
      fun one_match (i, mat) =
huffman@35466
   816
          map_index (match_app (i, mat)) spec;
huffman@35466
   817
    in
huffman@35466
   818
      val match_apps = flat (map_index one_match match_consts);
huffman@35466
   819
    end;
huffman@35466
   820
huffman@35462
   821
  in
huffman@35462
   822
    (match_stricts @ match_apps, thy)
huffman@35462
   823
  end;
huffman@35462
   824
huffman@35462
   825
(******************************************************************************)
huffman@35450
   826
(******************************* main function ********************************)
huffman@35450
   827
(******************************************************************************)
huffman@35450
   828
huffman@35450
   829
fun add_domain_constructors
huffman@35777
   830
    (dbind : binding)
huffman@35481
   831
    (spec : (binding * (bool * binding option * typ) list * mixfix) list)
huffman@35514
   832
    (iso_info : Domain_Take_Proofs.iso_info)
huffman@35450
   833
    (thy : theory) =
huffman@35450
   834
  let
huffman@35777
   835
    val dname = Binding.name_of dbind;
huffman@35450
   836
huffman@35481
   837
    (* retrieve facts about rep/abs *)
huffman@35481
   838
    val lhsT = #absT iso_info;
huffman@35481
   839
    val {rep_const, abs_const, ...} = iso_info;
huffman@35481
   840
    val abs_iso_thm = #abs_inverse iso_info;
huffman@35481
   841
    val rep_iso_thm = #rep_inverse iso_info;
huffman@35450
   842
    val iso_locale = @{thm iso.intro} OF [abs_iso_thm, rep_iso_thm];
huffman@35450
   843
    val rep_strict = iso_locale RS @{thm iso.rep_strict};
huffman@35450
   844
    val abs_strict = iso_locale RS @{thm iso.abs_strict};
huffman@35450
   845
    val rep_defined_iff = iso_locale RS @{thm iso.rep_defined_iff};
huffman@35450
   846
    val abs_defined_iff = iso_locale RS @{thm iso.abs_defined_iff};
huffman@35450
   847
huffman@35487
   848
    (* qualify constants and theorems with domain name *)
huffman@35487
   849
    val thy = Sign.add_path dname thy;
huffman@35487
   850
huffman@35450
   851
    (* define constructor functions *)
huffman@35457
   852
    val (con_result, thy) =
huffman@35454
   853
      let
huffman@35454
   854
        fun prep_arg (lazy, sel, T) = (lazy, T);
huffman@35454
   855
        fun prep_con (b, args, mx) = (b, map prep_arg args, mx);
huffman@35454
   856
        val con_spec = map prep_con spec;
huffman@35454
   857
      in
huffman@35454
   858
        add_constructors con_spec abs_const iso_locale thy
huffman@35454
   859
      end;
huffman@35781
   860
    val {con_consts, con_betas, exhaust, ...} = con_result;
huffman@35459
   861
huffman@35459
   862
    (* define case combinator *)
huffman@35460
   863
    val ((case_const : typ -> term, cases : thm list), thy) =
huffman@35459
   864
      let
huffman@35459
   865
        fun prep_arg (lazy, sel, T) = (lazy, T);
huffman@35459
   866
        fun prep_con c (b, args, mx) = (c, map prep_arg args);
huffman@35459
   867
        val case_spec = map2 prep_con con_consts spec;
huffman@35459
   868
      in
huffman@35777
   869
        add_case_combinator case_spec lhsT dbind
huffman@35781
   870
          con_betas exhaust iso_locale rep_const thy
huffman@35459
   871
      end;
huffman@35450
   872
huffman@35446
   873
    (* define and prove theorems for selector functions *)
huffman@35446
   874
    val (sel_thms : thm list, thy : theory) =
huffman@35460
   875
      let
huffman@35460
   876
        val sel_spec : (term * (bool * binding option * typ) list) list =
huffman@35460
   877
          map2 (fn con => fn (b, args, mx) => (con, args)) con_consts spec;
huffman@35460
   878
      in
huffman@35460
   879
        add_selectors sel_spec rep_const
huffman@35460
   880
          abs_iso_thm rep_strict rep_defined_iff con_betas thy
huffman@35460
   881
      end;
huffman@35460
   882
huffman@35460
   883
    (* define and prove theorems for discriminator functions *)
huffman@35460
   884
    val (dis_thms : thm list, thy : theory) =
huffman@35460
   885
      let
huffman@35460
   886
        val bindings = map #1 spec;
huffman@35460
   887
        fun prep_arg (lazy, sel, T) = (lazy, T);
huffman@35460
   888
        fun prep_con c (b, args, mx) = (c, map prep_arg args);
huffman@35460
   889
        val dis_spec = map2 prep_con con_consts spec;
huffman@35460
   890
      in
huffman@35461
   891
        add_discriminators bindings dis_spec lhsT
huffman@35781
   892
          exhaust case_const cases thy
huffman@35460
   893
      end
huffman@35446
   894
huffman@35462
   895
    (* define and prove theorems for match combinators *)
huffman@35462
   896
    val (match_thms : thm list, thy : theory) =
huffman@35462
   897
      let
huffman@35462
   898
        val bindings = map #1 spec;
huffman@35462
   899
        fun prep_arg (lazy, sel, T) = (lazy, T);
huffman@35462
   900
        fun prep_con c (b, args, mx) = (c, map prep_arg args);
huffman@35462
   901
        val mat_spec = map2 prep_con con_consts spec;
huffman@35462
   902
      in
huffman@35462
   903
        add_match_combinators bindings mat_spec lhsT
huffman@35781
   904
          exhaust case_const cases thy
huffman@35462
   905
      end
huffman@35462
   906
huffman@35446
   907
    (* restore original signature path *)
huffman@35446
   908
    val thy = Sign.parent_path thy;
huffman@35444
   909
huffman@35444
   910
    val result =
huffman@35444
   911
      { con_consts = con_consts,
huffman@35451
   912
        con_betas = con_betas,
huffman@35781
   913
        nchotomy = #nchotomy con_result,
huffman@35781
   914
        exhaust = exhaust,
huffman@35781
   915
        compacts = #compacts con_result,
huffman@35457
   916
        con_rews = #con_rews con_result,
huffman@35457
   917
        inverts = #inverts con_result,
huffman@35457
   918
        injects = #injects con_result,
huffman@35458
   919
        dist_les = #dist_les con_result,
huffman@35458
   920
        dist_eqs = #dist_eqs con_result,
huffman@35459
   921
        cases = cases,
huffman@35460
   922
        sel_rews = sel_thms,
huffman@35462
   923
        dis_rews = dis_thms,
huffman@37109
   924
        match_rews = match_thms };
huffman@35444
   925
  in
huffman@35444
   926
    (result, thy)
huffman@35444
   927
  end;
huffman@35444
   928
huffman@35444
   929
end;