author  skalberg 
Fri, 04 Mar 2005 15:07:34 +0100  
changeset 15574  b1d1b5bfc464 
parent 15570  8d8c70b41bab 
child 16123  1381e90c2694 
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 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

11 
val add_dt_realizers: (string * sort) list > 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

12 
DatatypeAux.datatype_info list > theory > theory 
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 = 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

30 
let val {sign, prop, der = (_, prf), ...} = rep_thm thm 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

31 
in Reconstruct.reconstruct_proof sign prop prf end; 
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 

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

43 
fun make_ind sorts ({descr, rec_names, rec_rewrites, induction, ...} : datatype_info) (is, thy) = 
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 sg = sign_of thy; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

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

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

48 
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

49 

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

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

51 
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

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

53 

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

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

55 
if i mem is then 
15570  56 
Free (List.nth (pnames, i), T > U > HOLogic.boolT) $ r $ x 
57 
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

58 

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

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

60 
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

61 

15570  62 
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

63 
(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

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

65 
val Ts = map (typ_of_dtyp descr sorts) cargs; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

66 
val tnames = variantlist (DatatypeProp.make_tnames Ts, pnames); 
15570  67 
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

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

69 

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

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

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

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

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

75 
val f' = Pattern.eta_contract (list_abs_free 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

76 
(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

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

78 
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

79 
(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

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

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

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

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

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

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

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

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

89 
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

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

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

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

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

94 

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

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

96 
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

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

98 

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

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

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

101 
if j = i then HOLogic.mk_fst t 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

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

103 

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

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

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

106 
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

107 
(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

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

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

112 
(list_comb (Const (s, fTs > T > U), rec_fns) $ Free (tname, T)) 
15531  113 
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

114 
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

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

116 
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

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

118 
val cert = cterm_of sg; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

119 
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

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

121 

13708  122 
val thm = 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

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

124 
[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

125 
rtac (cterm_instantiate inst induction) 1, 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

126 
ALLGOALS ObjectLogic.atomize_tac, 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

127 
rewrite_goals_tac (o_def :: map mk_meta_eq rec_rewrites), 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

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

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

130 

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

131 
val {path, ...} = Sign.rep_sg sg; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

132 
val ind_name = Thm.name_of_thm induction; 
15570  133 
val vs = map (fn i => List.nth (pnames, i)) is; 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

134 
val (thy', thm') = thy 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

135 
> Theory.absolute_path 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

136 
> PureThy.store_thm 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

137 
((space_implode "_" (ind_name :: vs @ ["correctness"]), thm), []) 
15570  138 
>> Theory.add_path (NameSpace.pack (getOpt (path,[]))); 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

139 

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

140 
val ivs = Drule.vars_of_terms 
12404b452034
Changed format of realizers / correctness proofs.
berghofe
parents:
13708
diff
changeset

141 
[Logic.varify (DatatypeProp.make_ind [descr] sorts)]; 
12404b452034
Changed format of realizers / correctness proofs.
berghofe
parents:
13708
diff
changeset

142 
val rvs = Drule.vars_of_terms [prop_of thm']; 
12404b452034
Changed format of realizers / correctness proofs.
berghofe
parents:
13708
diff
changeset

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

144 
tname_of (body_type T) mem ["set", "bool"]) ivs); 
15570  145 
val ivs2 = map (fn (ixn, _) => Var (ixn, valOf (assoc (rvs, ixn)))) ivs; 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

146 

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

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

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

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

150 
Free (s, T) => 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

151 
let val T' = Type.varifyT T 
15531  152 
in Abst (s, SOME T', Proofterm.prf_abstract_over 
153 
(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

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

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

157 
(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

158 

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

159 
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

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

161 
(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

162 

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

163 
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

164 

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

165 

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

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

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

168 
val sg = sign_of thy; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

169 
val cert = cterm_of sg; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

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

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

172 

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

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

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

175 
val Ts = map (typ_of_dtyp descr sorts) cargs; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

176 
val frees = variantlist 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

177 
(DatatypeProp.make_tnames Ts, ["P", "y"]) ~~ Ts; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

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

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

180 
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

181 
(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

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

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

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

185 

15531  186 
val SOME (_, _, constrs) = assoc (descr, index); 
15570  187 
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

188 
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

189 
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

190 

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

191 
val y = Var (("y", 0), Type.varifyT T); 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

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

193 

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

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

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

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

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

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

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

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

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

202 

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

203 
val {path, ...} = Sign.rep_sg sg; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

204 
val exh_name = Thm.name_of_thm exhaustion; 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

205 
val (thy', thm') = thy 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

206 
> Theory.absolute_path 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

207 
> PureThy.store_thm ((exh_name ^ "_P_correctness", thm), []) 
15570  208 
>> Theory.add_path (NameSpace.pack (getOpt (path,[]))); 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

209 

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

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

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

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

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

215 
map PBound (length prems  1 downto 0))) (prems ~~ rs))); 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

216 
val r' = Logic.varify (Abs ("y", Type.varifyT T, 
13725
12404b452034
Changed format of realizers / correctness proofs.
berghofe
parents:
13708
diff
changeset

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

218 
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

219 

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

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

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

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

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

224 

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

225 

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

226 
fun add_dt_realizers sorts infos thy = if !proofs < 2 then thy else 
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

227 
(message "Adding realizers for induction and case analysis ..."; thy 
15570  228 
> curry (Library.foldr (make_ind sorts (hd infos))) 
13467
d66b526192bf
Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff
changeset

229 
(subsets 0 (length (#descr (hd infos))  1)) 
15570  230 
> curry (Library.foldr (make_casedists sorts)) infos); 
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; 