src/HOLCF/domain/theorems.ML
author paulson
Fri Feb 16 18:00:47 1996 +0100 (1996-02-16)
changeset 1512 ce37c64244c0
parent 1461 6bcb44e4d6e5
child 1637 b8a8ae2e5de1
permissions -rw-r--r--
Elimination of fully-functorial style.
Type tactic changed to a type abbrevation (from a datatype).
Constructor tactic and function apply deleted.
regensbu@1274
     1
(* theorems.ML
regensbu@1274
     2
   ID:         $Id$
regensbu@1274
     3
   Author : David von Oheimb
regensbu@1274
     4
   Created: 06-Jun-95
regensbu@1274
     5
   Updated: 08-Jun-95 first proof from cterms
regensbu@1274
     6
   Updated: 26-Jun-95 proofs for exhaustion thms
regensbu@1274
     7
   Updated: 27-Jun-95 proofs for discriminators, constructors and selectors
regensbu@1274
     8
   Updated: 06-Jul-95 proofs for distinctness, invertibility and injectivity
regensbu@1274
     9
   Updated: 17-Jul-95 proofs for induction rules
regensbu@1274
    10
   Updated: 19-Jul-95 proof for co-induction rule
regensbu@1274
    11
   Updated: 28-Aug-95 definedness theorems for selectors (completion)
regensbu@1274
    12
   Updated: 05-Sep-95 simultaneous domain equations (main part)
regensbu@1274
    13
   Updated: 11-Sep-95 simultaneous domain equations (coding finished)
regensbu@1274
    14
   Updated: 13-Sep-95 simultaneous domain equations (debugging)
regensbu@1274
    15
   Copyright 1995 TU Muenchen
regensbu@1274
    16
*)
regensbu@1274
    17
regensbu@1274
    18
regensbu@1274
    19
structure Domain_Theorems = struct
regensbu@1274
    20
regensbu@1274
    21
local
regensbu@1274
    22
regensbu@1274
    23
open Domain_Library;
regensbu@1274
    24
infixr 0 ===>;infixr 0 ==>;infix 0 == ; 
regensbu@1274
    25
infix 1 ===; infix 1 ~= ; infix 1 <<; infix 1 ~<<;
regensbu@1274
    26
infix 9 `   ; infix 9 `% ; infix 9 `%%; infixr 9 oo;
regensbu@1274
    27
regensbu@1274
    28
(* ----- general proof facilities ------------------------------------------------- *)
regensbu@1274
    29
regensbu@1274
    30
fun inferT sg pre_tm = #2(Sign.infer_types sg (K None)(K None)[]true([pre_tm],propT));
regensbu@1274
    31
regensbu@1274
    32
(*
regensbu@1274
    33
infix 0 y;
regensbu@1274
    34
val b=0;
regensbu@1274
    35
fun _ y t = by t;
regensbu@1274
    36
fun  g  defs t = let val sg = sign_of thy;
clasohm@1461
    37
                     val ct = Thm.cterm_of sg (inferT sg t);
clasohm@1461
    38
                 in goalw_cterm defs ct end;
regensbu@1274
    39
*)
regensbu@1274
    40
regensbu@1274
    41
fun pg'' thy defs t = let val sg = sign_of thy;
clasohm@1461
    42
                          val ct = Thm.cterm_of sg (inferT sg t);
clasohm@1461
    43
                      in prove_goalw_cterm defs ct end;
regensbu@1274
    44
fun pg'  thy defs t tacsf=pg'' thy defs t (fn []   => tacsf 
clasohm@1461
    45
                                            | prems=> (cut_facts_tac prems 1)::tacsf);
regensbu@1274
    46
regensbu@1274
    47
fun REPEAT_DETERM_UNTIL p tac = 
regensbu@1274
    48
let fun drep st = if p st then Sequence.single st
paulson@1512
    49
                          else (case Sequence.pull(tac st) of
clasohm@1461
    50
                                  None        => Sequence.null
clasohm@1461
    51
                                | Some(st',_) => drep st')
paulson@1512
    52
in drep end;
regensbu@1274
    53
val UNTIL_SOLVED = REPEAT_DETERM_UNTIL (has_fewer_prems 1);
regensbu@1274
    54
regensbu@1274
    55
local val trueI2 = prove_goal HOL.thy "f~=x ==> True" (fn prems => [rtac TrueI 1]) in
regensbu@1274
    56
val kill_neq_tac = dtac trueI2 end;
clasohm@1461
    57
fun case_UU_tac rews i v =      res_inst_tac [("Q",v^"=UU")] classical2 i THEN
clasohm@1461
    58
                                asm_simp_tac (HOLCF_ss addsimps rews) i;
regensbu@1274
    59
regensbu@1274
    60
val chain_tac = REPEAT_DETERM o resolve_tac 
clasohm@1461
    61
                [is_chain_iterate, ch2ch_fappR, ch2ch_fappL];
regensbu@1274
    62
regensbu@1274
    63
(* ----- general proofs ----------------------------------------------------------- *)
regensbu@1274
    64
regensbu@1274
    65
val swap3 = prove_goal HOL.thy "[| Q ==> P; ~P |] ==> ~Q" (fn prems => [
regensbu@1274
    66
                                cut_facts_tac prems 1,
regensbu@1274
    67
                                etac swap 1,
regensbu@1274
    68
                                dtac notnotD 1,
clasohm@1461
    69
                                etac (hd prems) 1]);
regensbu@1274
    70
regensbu@1274
    71
val dist_eqI = prove_goal Porder0.thy "~ x << y ==> x ~= y" (fn prems => [
clasohm@1461
    72
                                cut_facts_tac prems 1,
clasohm@1461
    73
                                etac swap 1,
clasohm@1461
    74
                                dtac notnotD 1,
clasohm@1461
    75
                                asm_simp_tac HOLCF_ss 1]);
regensbu@1274
    76
val cfst_strict  = prove_goal Cprod3.thy "cfst`UU = UU" (fn _ => [
clasohm@1461
    77
                                (simp_tac (HOLCF_ss addsimps [inst_cprod_pcpo2]) 1)]);
regensbu@1274
    78
val csnd_strict  = prove_goal Cprod3.thy "csnd`UU = UU" (fn _ => [
clasohm@1461
    79
                        (simp_tac (HOLCF_ss addsimps [inst_cprod_pcpo2]) 1)]);
regensbu@1274
    80
regensbu@1274
    81
in
regensbu@1274
    82
regensbu@1274
    83
regensbu@1274
    84
fun theorems thy (((dname,_),cons) : eq, eqs :eq list) =
regensbu@1274
    85
let
regensbu@1274
    86
regensbu@1274
    87
val dummy = writeln ("Proving isomorphism properties of domain "^dname^"...");
regensbu@1274
    88
val pg = pg' thy;
regensbu@1274
    89
regensbu@1274
    90
(* ----- getting the axioms and definitions --------------------------------------- *)
regensbu@1274
    91
regensbu@1274
    92
local val ga = get_axiom thy in
regensbu@1274
    93
val ax_abs_iso    = ga (dname^"_abs_iso"   );
regensbu@1274
    94
val ax_rep_iso    = ga (dname^"_rep_iso"   );
regensbu@1274
    95
val ax_when_def   = ga (dname^"_when_def"  );
regensbu@1274
    96
val axs_con_def   = map (fn (con,_) => ga (extern_name con ^"_def")) cons;
regensbu@1274
    97
val axs_dis_def   = map (fn (con,_) => ga (   dis_name con ^"_def")) cons;
regensbu@1274
    98
val axs_sel_def   = flat(map (fn (_,args) => 
clasohm@1461
    99
                    map (fn     arg => ga (sel_of arg      ^"_def")) args) cons);
regensbu@1274
   100
val ax_copy_def   = ga (dname^"_copy_def"  );
regensbu@1274
   101
end; (* local *)
regensbu@1274
   102
regensbu@1274
   103
(* ----- theorems concerning the isomorphism -------------------------------------- *)
regensbu@1274
   104
regensbu@1274
   105
val dc_abs  = %%(dname^"_abs");
regensbu@1274
   106
val dc_rep  = %%(dname^"_rep");
regensbu@1274
   107
val dc_copy = %%(dname^"_copy");
regensbu@1274
   108
val x_name = "x";
regensbu@1274
   109
regensbu@1274
   110
val (rep_strict, abs_strict) = let 
clasohm@1461
   111
               val r = ax_rep_iso RS (ax_abs_iso RS (allI  RSN(2,allI RS iso_strict)))
clasohm@1461
   112
               in (r RS conjunct1, r RS conjunct2) end;
regensbu@1274
   113
val abs_defin' = pg [] ((dc_abs`%x_name === UU) ==> (%x_name === UU)) [
clasohm@1461
   114
                                res_inst_tac [("t",x_name)] (ax_abs_iso RS subst) 1,
clasohm@1461
   115
                                etac ssubst 1,
clasohm@1461
   116
                                rtac rep_strict 1];
regensbu@1274
   117
val rep_defin' = pg [] ((dc_rep`%x_name === UU) ==> (%x_name === UU)) [
clasohm@1461
   118
                                res_inst_tac [("t",x_name)] (ax_rep_iso RS subst) 1,
clasohm@1461
   119
                                etac ssubst 1,
clasohm@1461
   120
                                rtac abs_strict 1];
regensbu@1274
   121
val iso_rews = [ax_abs_iso,ax_rep_iso,abs_strict,rep_strict];
regensbu@1274
   122
regensbu@1274
   123
local 
regensbu@1274
   124
val iso_swap = pg [] (dc_rep`%"x" === %"y" ==> %"x" === dc_abs`%"y") [
clasohm@1461
   125
                                dres_inst_tac [("f",dname^"_abs")] cfun_arg_cong 1,
clasohm@1461
   126
                                etac (ax_rep_iso RS subst) 1];
regensbu@1274
   127
fun exh foldr1 cn quant foldr2 var = let
regensbu@1274
   128
  fun one_con (con,args) = let val vns = map vname args in
regensbu@1274
   129
    foldr quant (vns, foldr2 ((%x_name === con_app2 con (var vns) vns)::
clasohm@1461
   130
                              map (defined o (var vns)) (nonlazy args))) end
regensbu@1274
   131
  in foldr1 ((cn(%x_name===UU))::map one_con cons) end;
regensbu@1274
   132
in
regensbu@1274
   133
val cases = let 
clasohm@1461
   134
            fun common_tac thm = rtac thm 1 THEN contr_tac 1;
clasohm@1461
   135
            fun unit_tac true = common_tac liftE1
clasohm@1461
   136
            |   unit_tac _    = all_tac;
clasohm@1461
   137
            fun prod_tac []          = common_tac oneE
clasohm@1461
   138
            |   prod_tac [arg]       = unit_tac (is_lazy arg)
clasohm@1461
   139
            |   prod_tac (arg::args) = 
clasohm@1461
   140
                                common_tac sprodE THEN
clasohm@1461
   141
                                kill_neq_tac 1 THEN
clasohm@1461
   142
                                unit_tac (is_lazy arg) THEN
clasohm@1461
   143
                                prod_tac args;
clasohm@1461
   144
            fun sum_one_tac p = SELECT_GOAL(EVERY[
clasohm@1461
   145
                                rtac p 1,
clasohm@1461
   146
                                rewrite_goals_tac axs_con_def,
clasohm@1461
   147
                                dtac iso_swap 1,
clasohm@1461
   148
                                simp_tac HOLCF_ss 1,
clasohm@1461
   149
                                UNTIL_SOLVED(fast_tac HOL_cs 1)]) 1;
clasohm@1461
   150
            fun sum_tac [(_,args)]       [p]        = 
clasohm@1461
   151
                                prod_tac args THEN sum_one_tac p
clasohm@1461
   152
            |   sum_tac ((_,args)::cons') (p::prems) = DETERM(
clasohm@1461
   153
                                common_tac ssumE THEN
clasohm@1461
   154
                                kill_neq_tac 1 THEN kill_neq_tac 2 THEN
clasohm@1461
   155
                                prod_tac args THEN sum_one_tac p) THEN
clasohm@1461
   156
                                sum_tac cons' prems
clasohm@1461
   157
            |   sum_tac _ _ = Imposs "theorems:sum_tac";
clasohm@1461
   158
          in pg'' thy [] (exh (fn l => foldr (op ===>) (l,mk_trp(%"P")))
clasohm@1461
   159
                              (fn T => T ==> %"P") mk_All
clasohm@1461
   160
                              (fn l => foldr (op ===>) (map mk_trp l,mk_trp(%"P")))
clasohm@1461
   161
                              bound_arg)
clasohm@1461
   162
                             (fn prems => [
clasohm@1461
   163
                                cut_facts_tac [excluded_middle] 1,
clasohm@1461
   164
                                etac disjE 1,
clasohm@1461
   165
                                rtac (hd prems) 2,
clasohm@1461
   166
                                etac rep_defin' 2,
clasohm@1461
   167
                                if is_one_con_one_arg (not o is_lazy) cons
clasohm@1461
   168
                                then rtac (hd (tl prems)) 1 THEN atac 2 THEN
clasohm@1461
   169
                                     rewrite_goals_tac axs_con_def THEN
clasohm@1461
   170
                                     simp_tac (HOLCF_ss addsimps [ax_rep_iso]) 1
clasohm@1461
   171
                                else sum_tac cons (tl prems)])end;
regensbu@1274
   172
val exhaust = pg [] (mk_trp(exh (foldr' mk_disj) Id mk_ex (foldr' mk_conj) (K %))) [
clasohm@1461
   173
                                rtac cases 1,
clasohm@1461
   174
                                UNTIL_SOLVED(fast_tac HOL_cs 1)];
regensbu@1274
   175
end;
regensbu@1274
   176
regensbu@1274
   177
local 
regensbu@1274
   178
val when_app = foldl (op `) (%%(dname^"_when"), map % (when_funs cons));
regensbu@1274
   179
val when_appl = pg [ax_when_def] (mk_trp(when_app`%x_name===when_body cons 
clasohm@1461
   180
                (fn (_,n) => %(nth_elem(n-1,when_funs cons)))`(dc_rep`%x_name))) [
clasohm@1461
   181
                                simp_tac HOLCF_ss 1];
regensbu@1274
   182
in
regensbu@1274
   183
val when_strict = pg [] ((if is_one_con_one_arg (K true) cons 
clasohm@1461
   184
        then fn t => mk_trp(strict(%"f")) ===> t else Id)(mk_trp(strict when_app))) [
clasohm@1461
   185
                                simp_tac(HOLCF_ss addsimps [when_appl,rep_strict]) 1];
regensbu@1274
   186
val when_apps = let fun one_when n (con,args) = pg axs_con_def
clasohm@1461
   187
                (lift_defined % (nonlazy args, mk_trp(when_app`(con_app con args) ===
clasohm@1461
   188
                 mk_cfapp(%(nth_elem(n,when_funs cons)),map %# args))))[
clasohm@1461
   189
                        asm_simp_tac (HOLCF_ss addsimps [when_appl,ax_abs_iso]) 1];
clasohm@1461
   190
                in mapn one_when 0 cons end;
regensbu@1274
   191
end;
regensbu@1274
   192
val when_rews = when_strict::when_apps;
regensbu@1274
   193
regensbu@1274
   194
(* ----- theorems concerning the constructors, discriminators and selectors ------- *)
regensbu@1274
   195
regensbu@1274
   196
val dis_stricts = map (fn (con,_) => pg axs_dis_def (mk_trp(
clasohm@1461
   197
                        (if is_one_con_one_arg (K true) cons then mk_not else Id)
clasohm@1461
   198
                         (strict(%%(dis_name con))))) [
clasohm@1461
   199
                simp_tac (HOLCF_ss addsimps (if is_one_con_one_arg (K true) cons 
clasohm@1461
   200
                                        then [ax_when_def] else when_rews)) 1]) cons;
regensbu@1274
   201
val dis_apps = let fun one_dis c (con,args)= pg (axs_dis_def)
clasohm@1461
   202
                   (lift_defined % (nonlazy args, (*(if is_one_con_one_arg is_lazy cons
clasohm@1461
   203
                        then curry (lift_defined %#) args else Id)
regensbu@1274
   204
#################*)
clasohm@1461
   205
                        (mk_trp((%%(dis_name c))`(con_app con args) ===
clasohm@1461
   206
                              %%(if con=c then "TT" else "FF"))))) [
clasohm@1461
   207
                                asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
clasohm@1461
   208
        in flat(map (fn (c,_) => map (one_dis c) cons) cons) end;
regensbu@1274
   209
val dis_defins = map (fn (con,args) => pg [] (defined(%x_name)==> 
clasohm@1461
   210
                      defined(%%(dis_name con)`%x_name)) [
clasohm@1461
   211
                                rtac cases 1,
clasohm@1461
   212
                                contr_tac 1,
clasohm@1461
   213
                                UNTIL_SOLVED (CHANGED(asm_simp_tac 
clasohm@1461
   214
                                              (HOLCF_ss addsimps dis_apps) 1))]) cons;
regensbu@1274
   215
val dis_rews = dis_stricts @ dis_defins @ dis_apps;
regensbu@1274
   216
regensbu@1274
   217
val con_stricts = flat(map (fn (con,args) => map (fn vn =>
clasohm@1461
   218
                        pg (axs_con_def) 
clasohm@1461
   219
                           (mk_trp(con_app2 con (fn arg => if vname arg = vn 
clasohm@1461
   220
                                        then UU else %# arg) args === UU))[
clasohm@1461
   221
                                asm_simp_tac (HOLCF_ss addsimps [abs_strict]) 1]
clasohm@1461
   222
                        ) (nonlazy args)) cons);
regensbu@1274
   223
val con_defins = map (fn (con,args) => pg []
clasohm@1461
   224
                        (lift_defined % (nonlazy args,
clasohm@1461
   225
                                mk_trp(defined(con_app con args)))) ([
clasohm@1461
   226
                                rtac swap3 1] @ (if is_one_con_one_arg (K true) cons 
clasohm@1461
   227
                                then [
clasohm@1461
   228
                                  if is_lazy (hd args) then rtac defined_up 2
clasohm@1461
   229
                                                       else atac 2,
clasohm@1461
   230
                                  rtac abs_defin' 1,    
clasohm@1461
   231
                                  asm_full_simp_tac (HOLCF_ss addsimps axs_con_def) 1]
clasohm@1461
   232
                                else [
clasohm@1461
   233
                                  eres_inst_tac [("f",dis_name con)] cfun_arg_cong 1,
clasohm@1461
   234
                                  asm_simp_tac (HOLCF_ss addsimps dis_rews) 1])))cons;
regensbu@1274
   235
val con_rews = con_stricts @ con_defins;
regensbu@1274
   236
regensbu@1274
   237
val sel_stricts = let fun one_sel sel = pg axs_sel_def (mk_trp(strict(%%sel))) [
clasohm@1461
   238
                                simp_tac (HOLCF_ss addsimps when_rews) 1];
regensbu@1274
   239
in flat(map (fn (_,args) => map (fn arg => one_sel (sel_of arg)) args) cons) end;
regensbu@1274
   240
val sel_apps = let fun one_sel c n sel = map (fn (con,args) => 
clasohm@1461
   241
                let val nlas = nonlazy args;
clasohm@1461
   242
                    val vns  = map vname args;
clasohm@1461
   243
                in pg axs_sel_def (lift_defined %
clasohm@1461
   244
                   (filter (fn v => con=c andalso (v<>nth_elem(n,vns))) nlas,
regensbu@1274
   245
   mk_trp((%%sel)`(con_app con args) === (if con=c then %(nth_elem(n,vns)) else UU))))
clasohm@1461
   246
                            ( (if con=c then [] 
clasohm@1461
   247
                               else map(case_UU_tac(when_rews@con_stricts)1) nlas)
clasohm@1461
   248
                             @(if con=c andalso ((nth_elem(n,vns)) mem nlas)
clasohm@1461
   249
                                         then[case_UU_tac (when_rews @ con_stricts) 1 
clasohm@1461
   250
                                                          (nth_elem(n,vns))] else [])
clasohm@1461
   251
                             @ [asm_simp_tac(HOLCF_ss addsimps when_rews)1])end) cons;
regensbu@1274
   252
in flat(map  (fn (c,args) => 
clasohm@1461
   253
        flat(mapn (fn n => fn arg => one_sel c n (sel_of arg)) 0 args)) cons) end;
regensbu@1274
   254
val sel_defins = if length cons = 1 then map (fn arg => pg [] (defined(%x_name) ==> 
clasohm@1461
   255
                        defined(%%(sel_of arg)`%x_name)) [
clasohm@1461
   256
                                rtac cases 1,
clasohm@1461
   257
                                contr_tac 1,
clasohm@1461
   258
                                UNTIL_SOLVED (CHANGED(asm_simp_tac 
clasohm@1461
   259
                                              (HOLCF_ss addsimps sel_apps) 1))]) 
clasohm@1461
   260
                 (filter_out is_lazy (snd(hd cons))) else [];
regensbu@1274
   261
val sel_rews = sel_stricts @ sel_defins @ sel_apps;
regensbu@1274
   262
regensbu@1274
   263
val distincts_le = let
regensbu@1274
   264
    fun dist (con1, args1) (con2, args2) = pg []
clasohm@1461
   265
              (lift_defined % ((nonlazy args1),
clasohm@1461
   266
                             (mk_trp (con_app con1 args1 ~<< con_app con2 args2))))([
clasohm@1461
   267
                        rtac swap3 1,
clasohm@1461
   268
                        eres_inst_tac [("fo5",dis_name con1)] monofun_cfun_arg 1]
clasohm@1461
   269
                      @ map (case_UU_tac (con_stricts @ dis_rews) 1) (nonlazy args2)
clasohm@1461
   270
                      @[asm_simp_tac (HOLCF_ss addsimps dis_rews) 1]);
regensbu@1274
   271
    fun distinct (con1,args1) (con2,args2) =
clasohm@1461
   272
        let val arg1 = (con1, args1);
clasohm@1461
   273
            val arg2 = (con2, (map (fn (arg,vn) => upd_vname (K vn) arg)
clasohm@1461
   274
                              (args2~~variantlist(map vname args2,map vname args1))));
clasohm@1461
   275
        in [dist arg1 arg2, dist arg2 arg1] end;
regensbu@1274
   276
    fun distincts []      = []
regensbu@1274
   277
    |   distincts (c::cs) = (map (distinct c) cs) :: distincts cs;
regensbu@1274
   278
in distincts cons end;
regensbu@1274
   279
val dists_le = flat (flat distincts_le);
regensbu@1274
   280
val dists_eq = let
regensbu@1274
   281
    fun distinct (_,args1) ((_,args2),leqs) = let
clasohm@1461
   282
        val (le1,le2) = (hd leqs, hd(tl leqs));
clasohm@1461
   283
        val (eq1,eq2) = (le1 RS dist_eqI, le2 RS dist_eqI) in
clasohm@1461
   284
        if nonlazy args1 = [] then [eq1, eq1 RS not_sym] else
clasohm@1461
   285
        if nonlazy args2 = [] then [eq2, eq2 RS not_sym] else
clasohm@1461
   286
                                        [eq1, eq2] end;
regensbu@1274
   287
    fun distincts []      = []
regensbu@1274
   288
    |   distincts ((c,leqs)::cs) = flat(map (distinct c) ((map fst cs)~~leqs)) @
clasohm@1461
   289
                                   distincts cs;
regensbu@1274
   290
    in distincts (cons~~distincts_le) end;
regensbu@1274
   291
regensbu@1274
   292
local 
regensbu@1274
   293
  fun pgterm rel con args = let
clasohm@1461
   294
                fun append s = upd_vname(fn v => v^s);
clasohm@1461
   295
                val (largs,rargs) = (args, map (append "'") args);
clasohm@1461
   296
                in pg [] (mk_trp (rel(con_app con largs,con_app con rargs)) ===>
clasohm@1461
   297
                      lift_defined % ((nonlazy largs),lift_defined % ((nonlazy rargs),
clasohm@1461
   298
                            mk_trp (foldr' mk_conj 
clasohm@1461
   299
                                (map rel (map %# largs ~~ map %# rargs)))))) end;
regensbu@1274
   300
  val cons' = filter (fn (_,args) => args<>[]) cons;
regensbu@1274
   301
in
regensbu@1274
   302
val inverts = map (fn (con,args) => 
clasohm@1461
   303
                pgterm (op <<) con args (flat(map (fn arg => [
clasohm@1461
   304
                                TRY(rtac conjI 1),
clasohm@1461
   305
                                dres_inst_tac [("fo5",sel_of arg)] monofun_cfun_arg 1,
clasohm@1461
   306
                                asm_full_simp_tac (HOLCF_ss addsimps sel_apps) 1]
clasohm@1461
   307
                                                      ) args))) cons';
regensbu@1274
   308
val injects = map (fn ((con,args),inv_thm) => 
clasohm@1461
   309
                           pgterm (op ===) con args [
clasohm@1461
   310
                                etac (antisym_less_inverse RS conjE) 1,
clasohm@1461
   311
                                dtac inv_thm 1, REPEAT(atac 1),
clasohm@1461
   312
                                dtac inv_thm 1, REPEAT(atac 1),
clasohm@1461
   313
                                TRY(safe_tac HOL_cs),
clasohm@1461
   314
                                REPEAT(rtac antisym_less 1 ORELSE atac 1)] )
clasohm@1461
   315
                  (cons'~~inverts);
regensbu@1274
   316
end;
regensbu@1274
   317
regensbu@1274
   318
(* ----- theorems concerning one induction step ----------------------------------- *)
regensbu@1274
   319
regensbu@1274
   320
val copy_strict = pg [ax_copy_def] ((if is_one_con_one_arg (K true) cons then fn t =>
clasohm@1461
   321
         mk_trp(strict(cproj (%"f") eqs (rec_of (hd(snd(hd cons)))))) ===> t
clasohm@1461
   322
        else Id) (mk_trp(strict(dc_copy`%"f")))) [
clasohm@1461
   323
                                asm_simp_tac(HOLCF_ss addsimps [abs_strict,rep_strict,
clasohm@1461
   324
                                                        cfst_strict,csnd_strict]) 1];
regensbu@1274
   325
val copy_apps = map (fn (con,args) => pg (ax_copy_def::axs_con_def)
clasohm@1461
   326
                    (lift_defined %# (filter is_nonlazy_rec args,
clasohm@1461
   327
                        mk_trp(dc_copy`%"f"`(con_app con args) ===
clasohm@1461
   328
                           (con_app2 con (app_rec_arg (cproj (%"f") eqs)) args))))
clasohm@1461
   329
                                 (map (case_UU_tac [ax_abs_iso] 1 o vname)
clasohm@1461
   330
                                   (filter(fn a=>not(is_rec a orelse is_lazy a))args)@
clasohm@1461
   331
                                 [asm_simp_tac (HOLCF_ss addsimps [ax_abs_iso]) 1])
clasohm@1461
   332
                )cons;
regensbu@1274
   333
val copy_stricts = map(fn(con,args)=>pg[](mk_trp(dc_copy`UU`(con_app con args) ===UU))
clasohm@1461
   334
             (let val rews = cfst_strict::csnd_strict::copy_strict::copy_apps@con_rews
clasohm@1461
   335
                         in map (case_UU_tac rews 1) (nonlazy args) @ [
clasohm@1461
   336
                             asm_simp_tac (HOLCF_ss addsimps rews) 1] end))
clasohm@1461
   337
                   (filter (fn (_,args)=>exists is_nonlazy_rec args) cons);
regensbu@1274
   338
val copy_rews = copy_strict::copy_apps @ copy_stricts;
regensbu@1274
   339
regensbu@1274
   340
in     (iso_rews, exhaust, cases, when_rews,
clasohm@1461
   341
        con_rews, sel_rews, dis_rews, dists_eq, dists_le, inverts, injects,
clasohm@1461
   342
        copy_rews)
regensbu@1274
   343
end; (* let *)
regensbu@1274
   344
regensbu@1274
   345
regensbu@1274
   346
fun comp_theorems thy (comp_dname, eqs: eq list, casess, con_rews, copy_rews) =
regensbu@1274
   347
let
regensbu@1274
   348
regensbu@1274
   349
val dummy = writeln ("Proving induction properties of domain "^comp_dname^"...");
regensbu@1274
   350
val pg = pg' thy;
regensbu@1274
   351
regensbu@1274
   352
val dnames = map (fst o fst) eqs;
regensbu@1274
   353
val conss  = map  snd        eqs;
regensbu@1274
   354
regensbu@1274
   355
(* ----- getting the composite axiom and definitions ------------------------------ *)
regensbu@1274
   356
regensbu@1274
   357
local val ga = get_axiom thy in
regensbu@1274
   358
val axs_reach      = map (fn dn => ga (dn ^  "_reach"   )) dnames;
regensbu@1274
   359
val axs_take_def   = map (fn dn => ga (dn ^  "_take_def")) dnames;
regensbu@1274
   360
val axs_finite_def = map (fn dn => ga (dn ^"_finite_def")) dnames;
regensbu@1274
   361
val ax_copy2_def   = ga (comp_dname^ "_copy_def");
regensbu@1274
   362
val ax_bisim_def   = ga (comp_dname^"_bisim_def");
regensbu@1274
   363
end; (* local *)
regensbu@1274
   364
regensbu@1274
   365
(* ----- theorems concerning finiteness and induction ----------------------------- *)
regensbu@1274
   366
regensbu@1274
   367
fun dc_take dn = %%(dn^"_take");
regensbu@1274
   368
val x_name = idx_name dnames "x"; 
regensbu@1274
   369
val P_name = idx_name dnames "P";
regensbu@1274
   370
regensbu@1274
   371
local
clasohm@1461
   372
  val iterate_ss = simpset_of "Fix";    
regensbu@1274
   373
  val iterate_Cprod_strict_ss = iterate_ss addsimps [cfst_strict, csnd_strict];
regensbu@1274
   374
  val iterate_Cprod_ss = iterate_ss addsimps [cfst2,csnd2,csplit2];
regensbu@1274
   375
  val copy_con_rews  = copy_rews @ con_rews;
regensbu@1274
   376
  val copy_take_defs = (if length dnames=1 then [] else [ax_copy2_def]) @axs_take_def;
regensbu@1274
   377
  val take_stricts = pg copy_take_defs (mk_trp(foldr' mk_conj (map (fn ((dn,args),_)=>
clasohm@1461
   378
                  (dc_take dn $ %"n")`UU === mk_constrain(Type(dn,args),UU)) eqs)))([
clasohm@1461
   379
                                nat_ind_tac "n" 1,
clasohm@1461
   380
                                simp_tac iterate_ss 1,
clasohm@1461
   381
                                simp_tac iterate_Cprod_strict_ss 1,
clasohm@1461
   382
                                asm_simp_tac iterate_Cprod_ss 1,
clasohm@1461
   383
                                TRY(safe_tac HOL_cs)] @
clasohm@1461
   384
                        map(K(asm_simp_tac (HOL_ss addsimps copy_rews)1))dnames);
regensbu@1274
   385
  val take_stricts' = rewrite_rule copy_take_defs take_stricts;
regensbu@1274
   386
  val take_0s = mapn (fn n => fn dn => pg axs_take_def(mk_trp((dc_take dn $ %%"0")
clasohm@1461
   387
                                                                `%x_name n === UU))[
clasohm@1461
   388
                                simp_tac iterate_Cprod_strict_ss 1]) 1 dnames;
regensbu@1274
   389
  val take_apps = pg copy_take_defs (mk_trp(foldr' mk_conj 
clasohm@1461
   390
            (flat(map (fn ((dn,_),cons) => map (fn (con,args) => foldr mk_all 
clasohm@1461
   391
                (map vname args,(dc_take dn $ (%%"Suc" $ %"n"))`(con_app con args) ===
clasohm@1461
   392
                 con_app2 con (app_rec_arg (fn n=>dc_take (nth_elem(n,dnames))$ %"n"))
clasohm@1461
   393
                              args)) cons) eqs)))) ([
clasohm@1461
   394
                                nat_ind_tac "n" 1,
clasohm@1461
   395
                                simp_tac iterate_Cprod_strict_ss 1,
clasohm@1461
   396
                                simp_tac (HOLCF_ss addsimps copy_con_rews) 1,
clasohm@1461
   397
                                TRY(safe_tac HOL_cs)] @
clasohm@1461
   398
                        (flat(map (fn ((dn,_),cons) => map (fn (con,args) => EVERY (
clasohm@1461
   399
                                asm_full_simp_tac iterate_Cprod_ss 1::
clasohm@1461
   400
                                map (case_UU_tac (take_stricts'::copy_con_rews) 1)
clasohm@1461
   401
                                    (nonlazy args) @[
clasohm@1461
   402
                                asm_full_simp_tac (HOLCF_ss addsimps copy_rews) 1])
clasohm@1461
   403
                        ) cons) eqs)));
regensbu@1274
   404
in
regensbu@1274
   405
val take_rews = atomize take_stricts @ take_0s @ atomize take_apps;
regensbu@1274
   406
end; (* local *)
regensbu@1274
   407
regensbu@1274
   408
val take_lemmas = mapn (fn n => fn(dn,ax_reach) => pg'' thy axs_take_def (mk_All("n",
clasohm@1461
   409
                mk_trp(dc_take dn $ Bound 0 `%(x_name n) === 
clasohm@1461
   410
                       dc_take dn $ Bound 0 `%(x_name n^"'")))
clasohm@1461
   411
           ===> mk_trp(%(x_name n) === %(x_name n^"'"))) (fn prems => [
clasohm@1461
   412
                                res_inst_tac[("t",x_name n    )](ax_reach RS subst) 1,
clasohm@1461
   413
                                res_inst_tac[("t",x_name n^"'")](ax_reach RS subst) 1,
clasohm@1461
   414
                                rtac (fix_def2 RS ssubst) 1,
clasohm@1461
   415
                                REPEAT(CHANGED(rtac (contlub_cfun_arg RS ssubst) 1
clasohm@1461
   416
                                               THEN chain_tac 1)),
clasohm@1461
   417
                                rtac (contlub_cfun_fun RS ssubst) 1,
clasohm@1461
   418
                                rtac (contlub_cfun_fun RS ssubst) 2,
clasohm@1461
   419
                                rtac lub_equal 3,
clasohm@1461
   420
                                chain_tac 1,
clasohm@1461
   421
                                rtac allI 1,
clasohm@1461
   422
                                resolve_tac prems 1])) 1 (dnames~~axs_reach);
regensbu@1274
   423
regensbu@1274
   424
local
regensbu@1274
   425
  fun one_con p (con,args) = foldr mk_All (map vname args,
clasohm@1461
   426
        lift_defined (bound_arg (map vname args)) (nonlazy args,
clasohm@1461
   427
        lift (fn arg => %(P_name (1+rec_of arg)) $ bound_arg args arg)
clasohm@1461
   428
             (filter is_rec args,mk_trp(%p $ con_app2 con (bound_arg args) args))));
regensbu@1274
   429
  fun one_eq ((p,cons),concl) = (mk_trp(%p $ UU) ===> 
clasohm@1461
   430
                           foldr (op ===>) (map (one_con p) cons,concl));
regensbu@1274
   431
  fun ind_term concf = foldr one_eq (mapn (fn n => fn x => (P_name n, x)) 1 conss,
clasohm@1461
   432
        mk_trp(foldr' mk_conj (mapn (fn n => concf (P_name n,x_name n)) 1 dnames)));
regensbu@1274
   433
  val take_ss = HOL_ss addsimps take_rews;
regensbu@1274
   434
  fun ind_tacs tacsf thms1 thms2 prems = TRY(safe_tac HOL_cs)::
clasohm@1461
   435
                                flat (mapn (fn n => fn (thm1,thm2) => 
clasohm@1461
   436
                                  tacsf (n,prems) (thm1,thm2) @ 
clasohm@1461
   437
                                  flat (map (fn cons =>
clasohm@1461
   438
                                    (resolve_tac prems 1 ::
clasohm@1461
   439
                                     flat (map (fn (_,args) => 
clasohm@1461
   440
                                       resolve_tac prems 1::
clasohm@1461
   441
                                       map (K(atac 1)) (nonlazy args) @
clasohm@1461
   442
                                       map (K(atac 1)) (filter is_rec args))
clasohm@1461
   443
                                     cons)))
clasohm@1461
   444
                                   conss))
clasohm@1461
   445
                                0 (thms1~~thms2));
regensbu@1274
   446
  local 
regensbu@1274
   447
    fun all_rec_to ns lazy_rec (n,cons) = forall (exists (fn arg => 
clasohm@1461
   448
                  is_rec arg andalso not(rec_of arg mem ns) andalso
clasohm@1461
   449
                  ((rec_of arg =  n andalso not(lazy_rec orelse is_lazy arg)) orelse 
clasohm@1461
   450
                    rec_of arg <> n andalso all_rec_to (rec_of arg::ns) 
clasohm@1461
   451
                      (lazy_rec orelse is_lazy arg) (n, (nth_elem(rec_of arg,conss))))
clasohm@1461
   452
                  ) o snd) cons;
regensbu@1274
   453
    fun warn (n,cons) = if all_rec_to [] false (n,cons) then (writeln 
clasohm@1461
   454
                           ("WARNING: domain "^nth_elem(n,dnames)^" is empty!"); true)
clasohm@1461
   455
                        else false;
regensbu@1274
   456
    fun lazy_rec_to ns lazy_rec (n,cons) = exists (exists (fn arg => 
clasohm@1461
   457
                  is_rec arg andalso not(rec_of arg mem ns) andalso
clasohm@1461
   458
                  ((rec_of arg =  n andalso (lazy_rec orelse is_lazy arg)) orelse 
clasohm@1461
   459
                    rec_of arg <> n andalso lazy_rec_to (rec_of arg::ns)
clasohm@1461
   460
                     (lazy_rec orelse is_lazy arg) (n, (nth_elem(rec_of arg,conss))))
clasohm@1461
   461
                 ) o snd) cons;
regensbu@1274
   462
  in val is_emptys = map warn (mapn (fn n => fn (_,cons) => (n,cons)) 0 eqs);
regensbu@1274
   463
     val is_finite = forall (not o lazy_rec_to [] false) 
clasohm@1461
   464
                            (mapn (fn n => fn (_,cons) => (n,cons)) 0 eqs)
regensbu@1274
   465
  end;
regensbu@1274
   466
in
regensbu@1274
   467
val finite_ind = pg'' thy [] (ind_term (fn (P,x) => fn dn => 
clasohm@1461
   468
                          mk_all(x,%P $ (dc_take dn $ %"n" `Bound 0)))) (fn prems=> [
clasohm@1461
   469
                                nat_ind_tac "n" 1,
clasohm@1461
   470
                                simp_tac (take_ss addsimps prems) 1,
clasohm@1461
   471
                                TRY(safe_tac HOL_cs)]
clasohm@1461
   472
                                @ flat(mapn (fn n => fn (cons,cases) => [
clasohm@1461
   473
                                 res_inst_tac [("x",x_name n)] cases 1,
clasohm@1461
   474
                                 asm_simp_tac (take_ss addsimps prems) 1]
clasohm@1461
   475
                                 @ flat(map (fn (con,args) => 
clasohm@1461
   476
                                  asm_simp_tac take_ss 1 ::
clasohm@1461
   477
                                  map (fn arg =>
clasohm@1461
   478
                                   case_UU_tac (prems@con_rews) 1 (
clasohm@1461
   479
                                   nth_elem(rec_of arg,dnames)^"_take n1`"^vname arg))
clasohm@1461
   480
                                  (filter is_nonlazy_rec args) @ [
clasohm@1461
   481
                                  resolve_tac prems 1] @
clasohm@1461
   482
                                  map (K (atac 1))      (nonlazy args) @
clasohm@1461
   483
                                  map (K (etac spec 1)) (filter is_rec args)) 
clasohm@1461
   484
                                 cons))
clasohm@1461
   485
                                1 (conss~~casess)));
regensbu@1274
   486
regensbu@1274
   487
val (finites,ind) = if is_finite then
regensbu@1274
   488
let 
regensbu@1274
   489
  fun take_enough dn = mk_ex ("n",dc_take dn $ Bound 0 ` %"x" === %"x");
regensbu@1274
   490
  val finite_lemmas1a = map (fn dn => pg [] (mk_trp(defined (%"x")) ===> 
clasohm@1461
   491
        mk_trp(mk_disj(mk_all("n",dc_take dn $ Bound 0 ` %"x" === UU),
clasohm@1461
   492
        take_enough dn)) ===> mk_trp(take_enough dn)) [
clasohm@1461
   493
                                etac disjE 1,
clasohm@1461
   494
                                etac notE 1,
clasohm@1461
   495
                                resolve_tac take_lemmas 1,
clasohm@1461
   496
                                asm_simp_tac take_ss 1,
clasohm@1461
   497
                                atac 1]) dnames;
regensbu@1274
   498
  val finite_lemma1b = pg [] (mk_trp (mk_all("n",foldr' mk_conj (mapn 
clasohm@1461
   499
        (fn n => fn ((dn,args),_) => mk_constrainall(x_name n,Type(dn,args),
clasohm@1461
   500
         mk_disj(dc_take dn $ Bound 1 ` Bound 0 === UU,
clasohm@1461
   501
                 dc_take dn $ Bound 1 ` Bound 0 === Bound 0))) 1 eqs)))) ([
clasohm@1461
   502
                                rtac allI 1,
clasohm@1461
   503
                                nat_ind_tac "n" 1,
clasohm@1461
   504
                                simp_tac take_ss 1,
clasohm@1461
   505
                                TRY(safe_tac(empty_cs addSEs[conjE] addSIs[conjI]))] @
clasohm@1461
   506
                                flat(mapn (fn n => fn (cons,cases) => [
clasohm@1461
   507
                                  simp_tac take_ss 1,
clasohm@1461
   508
                                  rtac allI 1,
clasohm@1461
   509
                                  res_inst_tac [("x",x_name n)] cases 1,
clasohm@1461
   510
                                  asm_simp_tac take_ss 1] @ 
clasohm@1461
   511
                                  flat(map (fn (con,args) => 
clasohm@1461
   512
                                    asm_simp_tac take_ss 1 ::
clasohm@1461
   513
                                    flat(map (fn arg => [
clasohm@1461
   514
                                      eres_inst_tac [("x",vname arg)] all_dupE 1,
clasohm@1461
   515
                                      etac disjE 1,
clasohm@1461
   516
                                      asm_simp_tac (HOL_ss addsimps con_rews) 1,
clasohm@1461
   517
                                      asm_simp_tac take_ss 1])
clasohm@1461
   518
                                    (filter is_nonlazy_rec args)))
clasohm@1461
   519
                                  cons))
clasohm@1461
   520
                                1 (conss~~casess))) handle ERROR => raise ERROR;
regensbu@1274
   521
  val all_finite=map (fn(dn,l1b)=>pg axs_finite_def (mk_trp(%%(dn^"_finite") $ %"x"))[
clasohm@1461
   522
                                case_UU_tac take_rews 1 "x",
clasohm@1461
   523
                                eresolve_tac finite_lemmas1a 1,
clasohm@1461
   524
                                step_tac HOL_cs 1,
clasohm@1461
   525
                                step_tac HOL_cs 1,
clasohm@1461
   526
                                cut_facts_tac [l1b] 1,
clasohm@1461
   527
                                fast_tac HOL_cs 1]) (dnames~~atomize finite_lemma1b);
regensbu@1274
   528
in
regensbu@1274
   529
(all_finite,
regensbu@1274
   530
 pg'' thy [] (ind_term (fn (P,x) => fn dn => %P $ %x))
clasohm@1461
   531
                               (ind_tacs (fn _ => fn (all_fin,finite_ind) => [
clasohm@1461
   532
                                rtac (rewrite_rule axs_finite_def all_fin RS exE) 1,
clasohm@1461
   533
                                etac subst 1,
clasohm@1461
   534
                                rtac finite_ind 1]) all_finite (atomize finite_ind))
regensbu@1274
   535
) end (* let *) else
regensbu@1274
   536
(mapn (fn n => fn dn => read_instantiate_sg (sign_of thy) 
clasohm@1461
   537
                    [("P",dn^"_finite "^x_name n)] excluded_middle) 1 dnames,
regensbu@1274
   538
 pg'' thy [] (foldr (op ===>) (mapn (fn n =>K(mk_trp(%%"adm" $ %(P_name n))))1
clasohm@1461
   539
                                       dnames,ind_term (fn(P,x)=>fn dn=> %P $ %x)))
clasohm@1461
   540
                               (ind_tacs (fn (n,prems) => fn (ax_reach,finite_ind) =>[
clasohm@1461
   541
                                rtac (ax_reach RS subst) 1,
clasohm@1461
   542
                                res_inst_tac [("x",x_name n)] spec 1,
clasohm@1461
   543
                                rtac wfix_ind 1,
clasohm@1461
   544
                                rtac adm_impl_admw 1,
clasohm@1461
   545
                                resolve_tac adm_thms 1,
clasohm@1461
   546
                                rtac adm_subst 1,
clasohm@1461
   547
                                cont_tacR 1,
clasohm@1461
   548
                                resolve_tac prems 1,
clasohm@1461
   549
                                strip_tac 1,
clasohm@1461
   550
                                rtac(rewrite_rule axs_take_def finite_ind) 1])
clasohm@1461
   551
                                 axs_reach (atomize finite_ind))
regensbu@1274
   552
)
regensbu@1274
   553
end; (* local *)
regensbu@1274
   554
regensbu@1274
   555
local
regensbu@1274
   556
  val xs = mapn (fn n => K (x_name n)) 1 dnames;
regensbu@1274
   557
  fun bnd_arg n i = Bound(2*(length dnames - n)-i-1);
regensbu@1274
   558
  val take_ss = HOL_ss addsimps take_rews;
regensbu@1274
   559
  val sproj   = bin_branchr (fn s => "fst("^s^")") (fn s => "snd("^s^")");
regensbu@1274
   560
  val coind_lemma = pg [ax_bisim_def] (mk_trp(mk_imp(%%(comp_dname^"_bisim") $ %"R",
clasohm@1461
   561
                foldr (fn (x,t)=> mk_all(x,mk_all(x^"'",t))) (xs,
clasohm@1461
   562
                  foldr mk_imp (mapn (fn n => K(proj (%"R") dnames n $ 
clasohm@1461
   563
                                      bnd_arg n 0 $ bnd_arg n 1)) 0 dnames,
clasohm@1461
   564
                    foldr' mk_conj (mapn (fn n => fn dn => 
clasohm@1461
   565
                                (dc_take dn $ %"n" `bnd_arg n 0 === 
clasohm@1461
   566
                                (dc_take dn $ %"n" `bnd_arg n 1))) 0 dnames)))))) ([
clasohm@1461
   567
                                rtac impI 1,
clasohm@1461
   568
                                nat_ind_tac "n" 1,
clasohm@1461
   569
                                simp_tac take_ss 1,
clasohm@1461
   570
                                safe_tac HOL_cs] @
clasohm@1461
   571
                                flat(mapn (fn n => fn x => [
clasohm@1461
   572
                                  etac allE 1, etac allE 1, 
clasohm@1461
   573
                                  eres_inst_tac [("P1",sproj "R" dnames n^
clasohm@1461
   574
                                                  " "^x^" "^x^"'")](mp RS disjE) 1,
clasohm@1461
   575
                                  TRY(safe_tac HOL_cs),
clasohm@1461
   576
                                  REPEAT(CHANGED(asm_simp_tac take_ss 1))]) 
clasohm@1461
   577
                                0 xs));
regensbu@1274
   578
in
regensbu@1274
   579
val coind = pg [] (mk_trp(%%(comp_dname^"_bisim") $ %"R") ===>
clasohm@1461
   580
                foldr (op ===>) (mapn (fn n => fn x => 
clasohm@1461
   581
                        mk_trp(proj (%"R") dnames n $ %x $ %(x^"'"))) 0 xs,
clasohm@1461
   582
                        mk_trp(foldr' mk_conj (map (fn x => %x === %(x^"'")) xs)))) ([
clasohm@1461
   583
                                TRY(safe_tac HOL_cs)] @
clasohm@1461
   584
                                flat(map (fn take_lemma => [
clasohm@1461
   585
                                  rtac take_lemma 1,
clasohm@1461
   586
                                  cut_facts_tac [coind_lemma] 1,
clasohm@1461
   587
                                  fast_tac HOL_cs 1])
clasohm@1461
   588
                                take_lemmas));
regensbu@1274
   589
end; (* local *)
regensbu@1274
   590
regensbu@1274
   591
regensbu@1274
   592
in (take_rews, take_lemmas, finites, finite_ind, ind, coind)
regensbu@1274
   593
regensbu@1274
   594
end; (* let *)
regensbu@1274
   595
end; (* local *)
regensbu@1274
   596
end; (* struct *)