author  wenzelm 
Sat, 15 Nov 2008 21:31:17 +0100  
changeset 28799  ee65e7d043fc 
parent 26626  c6231d64d264 
child 28814  463c9e9111ae 
permissions  rwrr 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

1 
(* Title: HOL/Tools/datatype_realizer.ML 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

2 
ID: $Id$ 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

3 
Author: Stefan Berghofer, TU Muenchen 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

4 

d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

5 
Porgram extraction from proofs involving datatypes: 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

6 
Realizers for induction and case analysis 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

7 
*) 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

8 

d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

9 
signature DATATYPE_REALIZER = 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

10 
sig 
24699
c6674504103f
datatype interpretators for size and datatype_realizer
haftmann
parents:
23590
diff
changeset

11 
val add_dt_realizers: string list > theory > theory 
c6674504103f
datatype interpretators for size and datatype_realizer
haftmann
parents:
23590
diff
changeset

12 
val setup: theory > theory 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

13 
end; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

14 

d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

15 
structure DatatypeRealizer : DATATYPE_REALIZER = 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

16 
struct 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

17 

d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

18 
open DatatypeAux; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

19 

d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

20 
fun subsets i j = if i <= j then 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

21 
let val is = subsets (i+1) j 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

22 
in map (fn ks => i::ks) is @ is end 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

23 
else [[]]; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

24 

d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

25 
fun forall_intr_prf (t, prf) = 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

26 
let val (a, T) = (case t of Var ((a, _), T) => (a, T)  Free p => p) 
15531  27 
in Abst (a, SOME T, Proofterm.prf_abstract_over t prf) end; 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

28 

d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

29 
fun prf_of thm = 
28799  30 
Reconstruct.reconstruct_proof (Thm.theory_of_thm thm) (Thm.prop_of thm) 
31 
(Proofterm.proof_of (Thm.proof_of thm)); 

13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

32 

d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

33 
fun prf_subst_vars inst = 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

34 
Proofterm.map_proof_terms (subst_vars ([], inst)) I; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

35 

d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

36 
fun is_unit t = snd (strip_type (fastype_of t)) = HOLogic.unitT; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

37 

13725
12404b452034
Changed format of realizers / correctness proofs.
berghofe
parents:
13708
diff
changeset

38 
fun tname_of (Type (s, _)) = s 
12404b452034
Changed format of realizers / correctness proofs.
berghofe
parents:
13708
diff
changeset

39 
 tname_of _ = ""; 
12404b452034
Changed format of realizers / correctness proofs.
berghofe
parents:
13708
diff
changeset

40 

13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

41 
fun mk_realizes T = Const ("realizes", T > HOLogic.boolT > HOLogic.boolT); 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

42 

24699
c6674504103f
datatype interpretators for size and datatype_realizer
haftmann
parents:
23590
diff
changeset

43 
fun make_ind sorts ({descr, rec_names, rec_rewrites, induction, ...} : datatype_info) is thy = 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

44 
let 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

45 
val recTs = get_rec_types descr sorts; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

46 
val pnames = if length descr = 1 then ["P"] 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

47 
else map (fn i => "P" ^ string_of_int i) (1 upto length descr); 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

48 

d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

49 
val rec_result_Ts = map (fn ((i, _), P) => 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

50 
if i mem is then TFree ("'" ^ P, HOLogic.typeS) else HOLogic.unitT) 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

51 
(descr ~~ pnames); 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

52 

d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

53 
fun make_pred i T U r x = 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

54 
if i mem is then 
15570  55 
Free (List.nth (pnames, i), T > U > HOLogic.boolT) $ r $ x 
56 
else Free (List.nth (pnames, i), U > HOLogic.boolT) $ x; 

13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

57 

d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

58 
fun mk_all i s T t = 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

59 
if i mem is then list_all_free ([(s, T)], t) else t; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

60 

15570  61 
val (prems, rec_fns) = split_list (List.concat (snd (foldl_map 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

62 
(fn (j, ((i, (_, _, constrs)), T)) => foldl_map (fn (j, (cname, cargs)) => 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

63 
let 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

64 
val Ts = map (typ_of_dtyp descr sorts) cargs; 
20071
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
19806
diff
changeset

65 
val tnames = Name.variant_list pnames (DatatypeProp.make_tnames Ts); 
15570  66 
val recs = List.filter (is_rec_type o fst o fst) (cargs ~~ tnames ~~ Ts); 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

67 
val frees = tnames ~~ Ts; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

68 

d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

69 
fun mk_prems vs [] = 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

70 
let 
15570  71 
val rT = List.nth (rec_result_Ts, i); 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

72 
val vs' = filter_out is_unit vs; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

73 
val f = mk_Free "f" (map fastype_of vs' > rT) j; 
18929  74 
val f' = Envir.eta_contract (list_abs_free 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

75 
(map dest_Free vs, if i mem is then list_comb (f, vs') 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

76 
else HOLogic.unit)); 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

77 
in (HOLogic.mk_Trueprop (make_pred i rT T (list_comb (f, vs')) 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

78 
(list_comb (Const (cname, Ts > T), map Free frees))), f') 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

79 
end 
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13467
diff
changeset

80 
 mk_prems vs (((dt, s), T) :: ds) = 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

81 
let 
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13467
diff
changeset

82 
val k = body_index dt; 
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13467
diff
changeset

83 
val (Us, U) = strip_type T; 
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13467
diff
changeset

84 
val i = length Us; 
15570  85 
val rT = List.nth (rec_result_Ts, k); 
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13467
diff
changeset

86 
val r = Free ("r" ^ s, Us > rT); 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

87 
val (p, f) = mk_prems (vs @ [r]) ds 
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13467
diff
changeset

88 
in (mk_all k ("r" ^ s) (Us > rT) (Logic.mk_implies 
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13467
diff
changeset

89 
(list_all (map (pair "x") Us, HOLogic.mk_Trueprop 
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13467
diff
changeset

90 
(make_pred k rT U (app_bnds r i) 
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13467
diff
changeset

91 
(app_bnds (Free (s, T)) i))), p)), f) 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

92 
end 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

93 

d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

94 
in (j + 1, 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

95 
apfst (curry list_all_free frees) (mk_prems (map Free frees) recs)) 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

96 
end) (j, constrs)) (1, descr ~~ recTs)))); 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

97 

d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

98 
fun mk_proj j [] t = t 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

99 
 mk_proj j (i :: is) t = if null is then t else 
23577  100 
if (j: int) = i then HOLogic.mk_fst t 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

101 
else mk_proj j is (HOLogic.mk_snd t); 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

102 

d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

103 
val tnames = DatatypeProp.make_tnames recTs; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

104 
val fTs = map fastype_of rec_fns; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

105 
val ps = map (fn ((((i, _), T), U), s) => Abs ("x", T, make_pred i U T 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

106 
(list_comb (Const (s, fTs > T > U), rec_fns) $ Bound 0) (Bound 0))) 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

107 
(descr ~~ recTs ~~ rec_result_Ts ~~ rec_names); 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

108 
val r = if null is then Extraction.nullt else 
15570  109 
foldr1 HOLogic.mk_prod (List.mapPartial (fn (((((i, _), T), U), s), tname) => 
15531  110 
if i mem is then SOME 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

111 
(list_comb (Const (s, fTs > T > U), rec_fns) $ Free (tname, T)) 
15531  112 
else NONE) (descr ~~ recTs ~~ rec_result_Ts ~~ rec_names ~~ tnames)); 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

113 
val concl = HOLogic.mk_Trueprop (foldr1 (HOLogic.mk_binop "op &") 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

114 
(map (fn ((((i, _), T), U), tname) => 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

115 
make_pred i U T (mk_proj i is r) (Free (tname, T))) 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

116 
(descr ~~ recTs ~~ rec_result_Ts ~~ tnames))); 
22578  117 
val cert = cterm_of thy; 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

118 
val inst = map (pairself cert) (map head_of (HOLogic.dest_conj 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

119 
(HOLogic.dest_Trueprop (concl_of induction))) ~~ ps); 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

120 

17959  121 
val thm = OldGoals.simple_prove_goal_cterm (cert (Logic.list_implies (prems, concl))) 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

122 
(fn prems => 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

123 
[rewrite_goals_tac (map mk_meta_eq [fst_conv, snd_conv]), 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

124 
rtac (cterm_instantiate inst induction) 1, 
23590
ad95084a5c63
renamed ObjectLogic.atomize_tac to ObjectLogic.atomize_prems_tac;
wenzelm
parents:
23577
diff
changeset

125 
ALLGOALS ObjectLogic.atomize_prems_tac, 
26359  126 
rewrite_goals_tac (@{thm o_def} :: map mk_meta_eq rec_rewrites), 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

127 
REPEAT ((resolve_tac prems THEN_ALL_NEW (fn i => 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

128 
REPEAT (etac allE i) THEN atac i)) 1)]); 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

129 

21646
c07b5b0e8492
thm/prf: separate official name vs. additional tags;
wenzelm
parents:
20286
diff
changeset

130 
val ind_name = Thm.get_name induction; 
15570  131 
val vs = map (fn i => List.nth (pnames, i)) is; 
18358  132 
val (thm', thy') = thy 
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24711
diff
changeset

133 
> Sign.absolute_path 
26481  134 
> PureThy.store_thm (space_implode "_" (ind_name :: vs @ ["correctness"]), thm) 
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24711
diff
changeset

135 
> Sign.restore_naming thy; 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

136 

20286  137 
val ivs = rev (Term.add_vars (Logic.varify (DatatypeProp.make_ind [descr] sorts)) []); 
22691  138 
val rvs = rev (Thm.fold_terms Term.add_vars thm' []); 
13725
12404b452034
Changed format of realizers / correctness proofs.
berghofe
parents:
13708
diff
changeset

139 
val ivs1 = map Var (filter_out (fn (_, T) => 
12404b452034
Changed format of realizers / correctness proofs.
berghofe
parents:
13708
diff
changeset

140 
tname_of (body_type T) mem ["set", "bool"]) ivs); 
17521  141 
val ivs2 = map (fn (ixn, _) => Var (ixn, valOf (AList.lookup (op =) rvs ixn))) ivs; 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

142 

15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset

143 
val prf = foldr forall_intr_prf 
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset

144 
(foldr (fn ((f, p), prf) => 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

145 
(case head_of (strip_abs_body f) of 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

146 
Free (s, T) => 
19806  147 
let val T' = Logic.varifyT T 
15531  148 
in Abst (s, SOME T', Proofterm.prf_abstract_over 
149 
(Var ((s, 0), T')) (AbsP ("H", SOME p, prf))) 

13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

150 
end 
15531  151 
 _ => AbsP ("H", SOME p, prf))) 
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset

152 
(Proofterm.proof_combP 
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset

153 
(prf_of thm', map PBound (length prems  1 downto 0))) (rec_fns ~~ prems_of thm)) ivs2; 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

154 

15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset

155 
val r' = if null is then r else Logic.varify (foldr (uncurry lambda) 
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset

156 
r (map Logic.unvarify ivs1 @ filter_out is_unit 
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset

157 
(map (head_of o strip_abs_body) rec_fns))); 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

158 

d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

159 
in Extraction.add_realizers_i [(ind_name, (vs, r', prf))] thy' end; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

160 

d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

161 

24699
c6674504103f
datatype interpretators for size and datatype_realizer
haftmann
parents:
23590
diff
changeset

162 
fun make_casedists sorts ({index, descr, case_name, case_rewrites, exhaustion, ...} : datatype_info) thy = 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

163 
let 
19806  164 
val cert = cterm_of thy; 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

165 
val rT = TFree ("'P", HOLogic.typeS); 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

166 
val rT' = TVar (("'P", 0), HOLogic.typeS); 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

167 

d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

168 
fun make_casedist_prem T (cname, cargs) = 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

169 
let 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

170 
val Ts = map (typ_of_dtyp descr sorts) cargs; 
20071
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
19806
diff
changeset

171 
val frees = Name.variant_list ["P", "y"] (DatatypeProp.make_tnames Ts) ~~ Ts; 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

172 
val free_ts = map Free frees; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

173 
val r = Free ("r" ^ NameSpace.base cname, Ts > rT) 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

174 
in (r, list_all_free (frees, Logic.mk_implies (HOLogic.mk_Trueprop 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

175 
(HOLogic.mk_eq (Free ("y", T), list_comb (Const (cname, Ts > T), free_ts))), 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

176 
HOLogic.mk_Trueprop (Free ("P", rT > HOLogic.boolT) $ 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

177 
list_comb (r, free_ts))))) 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

178 
end; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

179 

17521  180 
val SOME (_, _, constrs) = AList.lookup (op =) descr index; 
15570  181 
val T = List.nth (get_rec_types descr sorts, index); 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

182 
val (rs, prems) = split_list (map (make_casedist_prem T) constrs); 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

183 
val r = Const (case_name, map fastype_of rs > T > rT); 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

184 

19806  185 
val y = Var (("y", 0), Logic.legacy_varifyT T); 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

186 
val y' = Free ("y", T); 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

187 

17959  188 
val thm = OldGoals.prove_goalw_cterm [] (cert (Logic.list_implies (prems, 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

189 
HOLogic.mk_Trueprop (Free ("P", rT > HOLogic.boolT) $ 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

190 
list_comb (r, rs @ [y']))))) 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

191 
(fn prems => 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

192 
[rtac (cterm_instantiate [(cert y, cert y')] exhaustion) 1, 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

193 
ALLGOALS (EVERY' 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

194 
[asm_simp_tac (HOL_basic_ss addsimps case_rewrites), 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

195 
resolve_tac prems, asm_simp_tac HOL_basic_ss])]); 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

196 

21646
c07b5b0e8492
thm/prf: separate official name vs. additional tags;
wenzelm
parents:
20286
diff
changeset

197 
val exh_name = Thm.get_name exhaustion; 
18358  198 
val (thm', thy') = thy 
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24711
diff
changeset

199 
> Sign.absolute_path 
26481  200 
> PureThy.store_thm (exh_name ^ "_P_correctness", thm) 
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24711
diff
changeset

201 
> Sign.restore_naming thy; 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

202 

13725
12404b452034
Changed format of realizers / correctness proofs.
berghofe
parents:
13708
diff
changeset

203 
val P = Var (("P", 0), rT' > HOLogic.boolT); 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

204 
val prf = forall_intr_prf (y, forall_intr_prf (P, 
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset

205 
foldr (fn ((p, r), prf) => 
19806  206 
forall_intr_prf (Logic.legacy_varify r, AbsP ("H", SOME (Logic.varify p), 
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset

207 
prf))) (Proofterm.proof_combP (prf_of thm', 
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset

208 
map PBound (length prems  1 downto 0))) (prems ~~ rs))); 
19806  209 
val r' = Logic.legacy_varify (Abs ("y", Logic.legacy_varifyT T, 
13725
12404b452034
Changed format of realizers / correctness proofs.
berghofe
parents:
13708
diff
changeset

210 
list_abs (map dest_Free rs, list_comb (r, 
12404b452034
Changed format of realizers / correctness proofs.
berghofe
parents:
13708
diff
changeset

211 
map Bound ((length rs  1 downto 0) @ [length rs]))))); 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

212 

d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

213 
in Extraction.add_realizers_i 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

214 
[(exh_name, (["P"], r', prf)), 
13725
12404b452034
Changed format of realizers / correctness proofs.
berghofe
parents:
13708
diff
changeset

215 
(exh_name, ([], Extraction.nullt, prf_of exhaustion))] thy' 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

216 
end; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

217 

24699
c6674504103f
datatype interpretators for size and datatype_realizer
haftmann
parents:
23590
diff
changeset

218 
fun add_dt_realizers names thy = 
25223  219 
if ! Proofterm.proofs < 2 then thy 
24699
c6674504103f
datatype interpretators for size and datatype_realizer
haftmann
parents:
23590
diff
changeset

220 
else let 
c6674504103f
datatype interpretators for size and datatype_realizer
haftmann
parents:
23590
diff
changeset

221 
val _ = message "Adding realizers for induction and case analysis ..." 
c6674504103f
datatype interpretators for size and datatype_realizer
haftmann
parents:
23590
diff
changeset

222 
val infos = map (DatatypePackage.the_datatype thy) names; 
c6674504103f
datatype interpretators for size and datatype_realizer
haftmann
parents:
23590
diff
changeset

223 
val info :: _ = infos; 
c6674504103f
datatype interpretators for size and datatype_realizer
haftmann
parents:
23590
diff
changeset

224 
in 
c6674504103f
datatype interpretators for size and datatype_realizer
haftmann
parents:
23590
diff
changeset

225 
thy 
c6674504103f
datatype interpretators for size and datatype_realizer
haftmann
parents:
23590
diff
changeset

226 
> fold_rev (make_ind (#sorts info) info) (subsets 0 (length (#descr info)  1)) 
c6674504103f
datatype interpretators for size and datatype_realizer
haftmann
parents:
23590
diff
changeset

227 
> fold_rev (make_casedists (#sorts info)) infos 
c6674504103f
datatype interpretators for size and datatype_realizer
haftmann
parents:
23590
diff
changeset

228 
end; 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

229 

24711  230 
val setup = DatatypePackage.interpretation add_dt_realizers; 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

231 

d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

232 
end; 