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