author  berghofe 
Wed, 11 Jul 2007 11:43:31 +0200  
changeset 23764  15f81c5d5330 
child 23849  2a0e24c74593 
permissions  rwrr 
23764
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

1 
(* Title: HOL/Tools/inductive_set_package.ML 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

2 
ID: $Id$ 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

3 
Author: Stefan Berghofer, TU Muenchen 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

4 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

5 
Wrapper for defining inductive sets using package for inductive predicates, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

6 
including infrastructure for converting between predicates and sets. 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

7 
*) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

8 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

9 
signature INDUCTIVE_SET_PACKAGE = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

10 
sig 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

11 
val to_set_att: thm list > attribute 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

12 
val to_pred_att: thm list > attribute 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

13 
val pred_set_conv_att: attribute 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

14 
val add_inductive_i: bool > bstring > bool > bool > bool > 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

15 
(string * typ option * mixfix) list > 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

16 
(string * typ option) list > ((bstring * Attrib.src list) * term) list > thm list > 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

17 
local_theory > InductivePackage.inductive_result * local_theory 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

18 
val add_inductive: bool > bool > (string * string option * mixfix) list > 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

19 
(string * string option * mixfix) list > 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

20 
((bstring * Attrib.src list) * string) list > (thmref * Attrib.src list) list > 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

21 
local_theory > InductivePackage.inductive_result * local_theory 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

22 
val setup: theory > theory 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

23 
end; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

24 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

25 
structure InductiveSetPackage: INDUCTIVE_SET_PACKAGE = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

26 
struct 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

27 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

28 
val note_theorem = LocalTheory.note Thm.theoremK; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

29 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

30 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

31 
(**** simplify {(x1, ..., xn). (x1, ..., xn) : S} to S ****) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

32 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

33 
val subset_antisym = thm "subset_antisym"; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

34 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

35 
val collect_mem_simproc = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

36 
Simplifier.simproc (theory "Set") "Collect_mem" ["Collect t"] (fn thy => fn ss => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

37 
fn S as Const ("Collect", Type ("fun", [_, T])) $ t => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

38 
let val (u, Ts, ps) = HOLogic.strip_split t 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

39 
in case u of 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

40 
(c as Const ("op :", _)) $ q $ S' => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

41 
(case try (HOLogic.dest_tuple' ps) q of 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

42 
NONE => NONE 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

43 
 SOME ts => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

44 
if not (loose_bvar (S', 0)) andalso 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

45 
ts = map Bound (length ps downto 0) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

46 
then 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

47 
let val simp = full_simp_tac (Simplifier.inherit_context ss 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

48 
(HOL_basic_ss addsimps [split_paired_all, split_conv])) 1 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

49 
in 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

50 
SOME (Goal.prove (Simplifier.the_context ss) [] [] 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

51 
(Const ("==", T > T > propT) $ S $ S') 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

52 
(K (EVERY 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

53 
[rtac eq_reflection 1, rtac subset_antisym 1, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

54 
rtac subsetI 1, dtac CollectD 1, simp, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

55 
rtac subsetI 1, rtac CollectI 1, simp]))) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

56 
end 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

57 
else NONE) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

58 
 _ => NONE 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

59 
end 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

60 
 _ => NONE); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

61 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

62 
(***********************************************************************************) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

63 
(* simplifies (%x y. (x, y) : S & P x y) to (%x y. (x, y) : S Int {(x, y). P x y}) *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

64 
(* and (%x y. (x, y) : S  P x y) to (%x y. (x, y) : S Un {(x, y). P x y}) *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

65 
(* used for converting "strong" (co)induction rules *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

66 
(***********************************************************************************) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

67 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

68 
val strong_ind_simproc = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

69 
Simplifier.simproc HOL.thy "strong_ind" ["t"] (fn thy => fn ss => fn t => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

70 
let 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

71 
val xs = strip_abs_vars t; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

72 
fun close t = fold (fn x => fn u => all (fastype_of x) $ lambda x u) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

73 
(term_vars t) t; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

74 
fun mkop "op &" T x = SOME (Const ("op Int", T > T > T), x) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

75 
 mkop "op " T x = SOME (Const ("op Un", T > T > T), x) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

76 
 mkop _ _ _ = NONE; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

77 
fun mk_collect p T t = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

78 
let val U = HOLogic.dest_setT T 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

79 
in HOLogic.Collect_const U $ 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

80 
HOLogic.ap_split' (HOLogic.prod_factors p) U HOLogic.boolT t 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

81 
end; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

82 
fun decomp (Const (s, _) $ ((m as Const ("op :", 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

83 
Type (_, [_, Type (_, [T, _])]))) $ p $ S) $ u) = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

84 
mkop s T (m, p, S, mk_collect p T (head_of u)) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

85 
 decomp (Const (s, _) $ u $ ((m as Const ("op :", 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

86 
Type (_, [_, Type (_, [T, _])]))) $ p $ S)) = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

87 
mkop s T (m, p, mk_collect p T (head_of u), S) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

88 
 decomp _ = NONE; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

89 
val simp = full_simp_tac (Simplifier.inherit_context ss 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

90 
(HOL_basic_ss addsimps [mem_Collect_eq, split_conv])) 1; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

91 
in 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

92 
if null xs then NONE 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

93 
else case decomp (strip_abs_body t) of 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

94 
NONE => NONE 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

95 
 SOME (bop, (m, p, S, S')) => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

96 
SOME (mk_meta_eq (Goal.prove (Simplifier.the_context ss) [] [] 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

97 
(close (HOLogic.mk_Trueprop (HOLogic.mk_eq 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

98 
(t, list_abs (xs, m $ p $ (bop $ S $ S')))))) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

99 
(K (EVERY 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

100 
[REPEAT (rtac ext 1), rtac iffI 1, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

101 
EVERY [etac conjE 1, rtac IntI 1, simp, simp, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

102 
etac IntE 1, rtac conjI 1, simp, simp] ORELSE 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

103 
EVERY [etac disjE 1, rtac UnI1 1, simp, rtac UnI2 1, simp, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

104 
etac UnE 1, rtac disjI1 1, simp, rtac disjI2 1, simp]])))) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

105 
handle ERROR _ => NONE 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

106 
end); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

107 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

108 
(* only eta contract terms occurring as arguments of functions satisfying p *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

109 
fun eta_contract p = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

110 
let 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

111 
fun eta b (Abs (a, T, body)) = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

112 
(case eta b body of 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

113 
body' as (f $ Bound 0) => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

114 
if loose_bvar1 (f, 0) orelse not b then Abs (a, T, body') 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

115 
else incr_boundvars ~1 f 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

116 
 body' => Abs (a, T, body')) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

117 
 eta b (t $ u) = eta b t $ eta (p (head_of t)) u 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

118 
 eta b t = t 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

119 
in eta false end; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

120 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

121 
fun eta_contract_thm p = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

122 
Conv.fconv_rule (Conv.then_conv (Thm.beta_conversion true, fn ct => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

123 
Thm.transitive (Thm.eta_conversion ct) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

124 
(Thm.symmetric (Thm.eta_conversion 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

125 
(cterm_of (theory_of_cterm ct) (eta_contract p (term_of ct))))))); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

126 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

127 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

128 
(***********************************************************) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

129 
(* rules for converting between predicate and set notation *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

130 
(* *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

131 
(* rules for converting predicates to sets have the form *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

132 
(* P (%x y. (x, y) : s) = (%x y. (x, y) : S s) *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

133 
(* *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

134 
(* rules for converting sets to predicates have the form *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

135 
(* S {(x, y). p x y} = {(x, y). P p x y} *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

136 
(* *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

137 
(* where s and p are parameters *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

138 
(***********************************************************) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

139 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

140 
structure PredSetConvData = GenericDataFun 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

141 
( 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

142 
type T = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

143 
{(* rules for converting predicates to sets *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

144 
to_set_simps: thm list, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

145 
(* rules for converting sets to predicates *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

146 
to_pred_simps: thm list, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

147 
(* arities of functions of type t set => ... => u set *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

148 
set_arities: (typ * (int list list option list * int list list option)) list Symtab.table, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

149 
(* arities of functions of type (t => ... => bool) => u => ... => bool *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

150 
pred_arities: (typ * (int list list option list * int list list option)) list Symtab.table}; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

151 
val empty = {to_set_simps = [], to_pred_simps = [], 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

152 
set_arities = Symtab.empty, pred_arities = Symtab.empty}; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

153 
val extend = I; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

154 
fun merge _ 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

155 
({to_set_simps = to_set_simps1, to_pred_simps = to_pred_simps1, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

156 
set_arities = set_arities1, pred_arities = pred_arities1}, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

157 
{to_set_simps = to_set_simps2, to_pred_simps = to_pred_simps2, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

158 
set_arities = set_arities2, pred_arities = pred_arities2}) = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

159 
{to_set_simps = Drule.merge_rules (to_set_simps1, to_set_simps2), 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

160 
to_pred_simps = Drule.merge_rules (to_pred_simps1, to_pred_simps2), 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

161 
set_arities = Symtab.merge_list op = (set_arities1, set_arities2), 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

162 
pred_arities = Symtab.merge_list op = (pred_arities1, pred_arities2)}; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

163 
); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

164 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

165 
fun name_type_of (Free p) = SOME p 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

166 
 name_type_of (Const p) = SOME p 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

167 
 name_type_of _ = NONE; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

168 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

169 
fun map_type f (Free (s, T)) = Free (s, f T) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

170 
 map_type f (Var (ixn, T)) = Var (ixn, f T) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

171 
 map_type f _ = error "map_type"; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

172 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

173 
fun find_most_specific is_inst f eq xs T = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

174 
find_first (fn U => is_inst (T, f U) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

175 
andalso forall (fn U' => eq (f U, f U') orelse not 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

176 
(is_inst (T, f U') andalso is_inst (f U', f U))) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

177 
xs) xs; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

178 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

179 
fun lookup_arity thy arities (s, T) = case Symtab.lookup arities s of 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

180 
NONE => NONE 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

181 
 SOME xs => find_most_specific (Sign.typ_instance thy) fst (op =) xs T; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

182 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

183 
fun lookup_rule thy f rules = find_most_specific 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

184 
(swap #> Pattern.matches thy) (f #> fst) (op aconv) rules; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

185 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

186 
fun infer_arities thy arities (optf, t) fs = case strip_comb t of 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

187 
(Abs (s, T, u), []) => infer_arities thy arities (NONE, u) fs 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

188 
 (Abs _, _) => infer_arities thy arities (NONE, Envir.beta_norm t) fs 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

189 
 (u, ts) => (case Option.map (lookup_arity thy arities) (name_type_of u) of 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

190 
SOME (SOME (_, (arity, _))) => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

191 
(fold (infer_arities thy arities) (arity ~~ List.take (ts, length arity)) fs 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

192 
handle Subscript => error "infer_arities: bad term") 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

193 
 _ => fold (infer_arities thy arities) (map (pair NONE) ts) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

194 
(case optf of 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

195 
NONE => fs 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

196 
 SOME f => AList.update op = (u, the_default f 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

197 
(Option.map (curry op inter f) (AList.lookup op = fs u))) fs)); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

198 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

199 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

200 
(**************************************************************) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

201 
(* derive the to_pred equation from the to_set equation *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

202 
(* *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

203 
(* 1. instantiate each set parameter with {(x, y). p x y} *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

204 
(* 2. apply %P. {(x, y). P x y} to both sides of the equation *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

205 
(* 3. simplify *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

206 
(**************************************************************) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

207 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

208 
fun mk_to_pred_inst thy fs = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

209 
map (fn (x, ps) => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

210 
let 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

211 
val U = HOLogic.dest_setT (fastype_of x); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

212 
val x' = map_type (K (HOLogic.prodT_factors' ps U > HOLogic.boolT)) x 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

213 
in 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

214 
(cterm_of thy x, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

215 
cterm_of thy (HOLogic.Collect_const U $ 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

216 
HOLogic.ap_split' ps U HOLogic.boolT x')) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

217 
end) fs; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

218 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

219 
fun mk_to_pred_eq p fs optfs' T thm = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

220 
let 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

221 
val thy = theory_of_thm thm; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

222 
val insts = mk_to_pred_inst thy fs; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

223 
val thm' = Thm.instantiate ([], insts) thm; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

224 
val thm'' = (case optfs' of 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

225 
NONE => thm' RS sym 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

226 
 SOME fs' => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

227 
let 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

228 
val U = HOLogic.dest_setT (body_type T); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

229 
val Ts = HOLogic.prodT_factors' fs' U; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

230 
(* FIXME: should cterm_instantiate increment indexes? *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

231 
val arg_cong' = Thm.incr_indexes (Thm.maxidx_of thm + 1) arg_cong; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

232 
val (arg_cong_f, _) = arg_cong' > cprop_of > Drule.strip_imp_concl > 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

233 
Thm.dest_comb > snd > Drule.strip_comb > snd > hd > Thm.dest_comb 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

234 
in 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

235 
thm' RS (Drule.cterm_instantiate [(arg_cong_f, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

236 
cterm_of thy (Abs ("P", Ts > HOLogic.boolT, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

237 
HOLogic.Collect_const U $ HOLogic.ap_split' fs' U 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

238 
HOLogic.boolT (Bound 0))))] arg_cong' RS sym) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

239 
end) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

240 
in 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

241 
Simplifier.simplify (HOL_basic_ss addsimps [mem_Collect_eq, split_conv] 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

242 
addsimprocs [collect_mem_simproc]) thm'' > 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

243 
zero_var_indexes > eta_contract_thm (equal p) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

244 
end; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

245 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

246 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

247 
(**** declare rules for converting predicates to sets ****) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

248 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

249 
fun add ctxt thm {to_set_simps, to_pred_simps, set_arities, pred_arities} = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

250 
case prop_of thm of 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

251 
Const ("Trueprop", _) $ (Const ("op =", Type (_, [T, _])) $ lhs $ rhs) => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

252 
(case body_type T of 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

253 
Type ("bool", []) => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

254 
let 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

255 
val thy = Context.theory_of ctxt; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

256 
fun factors_of t fs = case strip_abs_body t of 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

257 
Const ("op :", _) $ u $ S => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

258 
if is_Free S orelse is_Var S then 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

259 
let val ps = HOLogic.prod_factors u 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

260 
in (SOME ps, (S, ps) :: fs) end 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

261 
else (NONE, fs) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

262 
 _ => (NONE, fs); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

263 
val (h, ts) = strip_comb lhs 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

264 
val (pfs, fs) = fold_map factors_of ts []; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

265 
val ((h', ts'), fs') = (case rhs of 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

266 
Abs _ => (case strip_abs_body rhs of 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

267 
Const ("op :", _) $ u $ S => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

268 
(strip_comb S, SOME (HOLogic.prod_factors u)) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

269 
 _ => error "member symbol on righthand side expected") 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

270 
 _ => (strip_comb rhs, NONE)) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

271 
in 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

272 
case (name_type_of h, name_type_of h') of 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

273 
(SOME (s, T), SOME (s', T')) => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

274 
(case Symtab.lookup set_arities s' of 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

275 
NONE => () 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

276 
 SOME xs => if exists (fn (U, _) => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

277 
Sign.typ_instance thy (T', U) andalso 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

278 
Sign.typ_instance thy (U, T')) xs 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

279 
then 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

280 
error ("Clash of conversion rules for operator " ^ s') 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

281 
else (); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

282 
{to_set_simps = thm :: to_set_simps, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

283 
to_pred_simps = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

284 
mk_to_pred_eq h fs fs' T' thm :: to_pred_simps, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

285 
set_arities = Symtab.insert_list op = (s', 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

286 
(T', (map (AList.lookup op = fs) ts', fs'))) set_arities, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

287 
pred_arities = Symtab.insert_list op = (s, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

288 
(T, (pfs, fs'))) pred_arities}) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

289 
 _ => error "set / predicate constant expected" 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

290 
end 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

291 
 _ => error "equation between predicates expected") 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

292 
 _ => error "equation expected"; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

293 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

294 
val pred_set_conv_att = Thm.declaration_attribute 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

295 
(fn thm => fn ctxt => PredSetConvData.map (add ctxt thm) ctxt); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

296 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

297 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

298 
(**** convert theorem in set notation to predicate notation ****) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

299 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

300 
fun is_pred tab t = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

301 
case Option.map (Symtab.lookup tab o fst) (name_type_of t) of 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

302 
SOME (SOME _) => true  _ => false; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

303 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

304 
fun to_pred_simproc rules = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

305 
let val rules' = map mk_meta_eq rules 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

306 
in 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

307 
Simplifier.simproc HOL.thy "to_pred" ["t"] 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

308 
(fn thy => K (lookup_rule thy (prop_of #> Logic.dest_equals) rules')) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

309 
end; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

310 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

311 
fun to_pred_proc thy rules t = case lookup_rule thy I rules t of 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

312 
NONE => NONE 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

313 
 SOME (lhs, rhs) => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

314 
SOME (Envir.subst_vars 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

315 
(Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty)) rhs); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

316 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

317 
fun to_pred thms ctxt thm = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

318 
let 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

319 
val thy = Context.theory_of ctxt; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

320 
val {to_pred_simps, set_arities, pred_arities, ...} = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

321 
fold (add ctxt) thms (PredSetConvData.get ctxt); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

322 
val fs = filter (is_Var o fst) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

323 
(infer_arities thy set_arities (NONE, prop_of thm) []); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

324 
(* instantiate each set parameter with {(x, y). p x y} *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

325 
val insts = mk_to_pred_inst thy fs 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

326 
in 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

327 
thm > 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

328 
Thm.instantiate ([], insts) > 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

329 
Simplifier.full_simplify (HOL_basic_ss addsimprocs 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

330 
[to_pred_simproc (mem_Collect_eq :: split_conv :: to_pred_simps)]) > 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

331 
eta_contract_thm (is_pred pred_arities) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

332 
end; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

333 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

334 
val to_pred_att = Thm.rule_attribute o to_pred; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

335 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

336 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

337 
(**** convert theorem in predicate notation to set notation ****) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

338 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

339 
fun to_set thms ctxt thm = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

340 
let 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

341 
val thy = Context.theory_of ctxt; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

342 
val {to_set_simps, pred_arities, ...} = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

343 
fold (add ctxt) thms (PredSetConvData.get ctxt); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

344 
val fs = filter (is_Var o fst) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

345 
(infer_arities thy pred_arities (NONE, prop_of thm) []); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

346 
(* instantiate each predicate parameter with %x y. (x, y) : s *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

347 
val insts = map (fn (x, ps) => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

348 
let 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

349 
val Ts = binder_types (fastype_of x); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

350 
val T = HOLogic.mk_tupleT ps Ts; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

351 
val x' = map_type (K (HOLogic.mk_setT T)) x 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

352 
in 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

353 
(cterm_of thy x, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

354 
cterm_of thy (list_abs (map (pair "x") Ts, HOLogic.mk_mem 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

355 
(HOLogic.mk_tuple' ps T (map Bound (length ps downto 0)), x')))) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

356 
end) fs 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

357 
in 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

358 
Simplifier.full_simplify (HOL_basic_ss addsimps to_set_simps 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

359 
addsimprocs [strong_ind_simproc]) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

360 
(Thm.instantiate ([], insts) thm) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

361 
end; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

362 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

363 
val to_set_att = Thm.rule_attribute o to_set; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

364 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

365 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

366 
(**** preprocessor for code generator ****) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

367 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

368 
fun codegen_preproc thy = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

369 
let 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

370 
val {to_pred_simps, set_arities, pred_arities, ...} = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

371 
PredSetConvData.get (Context.Theory thy); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

372 
fun preproc thm = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

373 
if exists_Const (fn (s, _) => case Symtab.lookup set_arities s of 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

374 
NONE => false 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

375 
 SOME arities => exists (fn (_, (xs, _)) => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

376 
forall is_none xs) arities) (prop_of thm) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

377 
then 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

378 
thm > 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

379 
Simplifier.full_simplify (HOL_basic_ss addsimprocs 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

380 
[to_pred_simproc (mem_Collect_eq :: split_conv :: to_pred_simps)]) > 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

381 
eta_contract_thm (is_pred pred_arities) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

382 
else thm 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

383 
in map preproc end; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

384 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

385 
fun code_ind_att optmod = to_pred_att [] #> InductiveCodegen.add optmod NONE; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

386 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

387 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

388 
(**** definition of inductive sets ****) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

389 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

390 
fun add_ind_set_def verbose alt_name coind no_elim no_ind cs 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

391 
intros monos params cnames_syn ctxt = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

392 
let 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

393 
val thy = ProofContext.theory_of ctxt; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

394 
val {set_arities, pred_arities, to_pred_simps, ...} = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

395 
PredSetConvData.get (Context.Proof ctxt); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

396 
fun infer (Abs (_, _, t)) = infer t 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

397 
 infer (Const ("op :", _) $ t $ u) = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

398 
infer_arities thy set_arities (SOME (HOLogic.prod_factors t), u) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

399 
 infer (t $ u) = infer t #> infer u 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

400 
 infer _ = I; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

401 
val new_arities = filter_out 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

402 
(fn (x as Free (_, Type ("fun", _)), _) => x mem params 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

403 
 _ => false) (fold (snd #> infer) intros []); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

404 
val params' = map (fn x => (case AList.lookup op = new_arities x of 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

405 
SOME fs => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

406 
let 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

407 
val T = HOLogic.dest_setT (fastype_of x); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

408 
val Ts = HOLogic.prodT_factors' fs T; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

409 
val x' = map_type (K (Ts > HOLogic.boolT)) x 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

410 
in 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

411 
(x, (x', 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

412 
(HOLogic.Collect_const T $ 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

413 
HOLogic.ap_split' fs T HOLogic.boolT x', 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

414 
list_abs (map (pair "x") Ts, HOLogic.mk_mem 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

415 
(HOLogic.mk_tuple' fs T (map Bound (length fs downto 0)), 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

416 
x))))) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

417 
end 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

418 
 NONE => (x, (x, (x, x))))) params; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

419 
val (params1, (params2, params3)) = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

420 
params' > map snd > split_list > split_list; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

421 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

422 
(* equations for converting sets to predicates *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

423 
val ((cs', cs_info), eqns) = cs > map (fn c as Free (s, T) => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

424 
let 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

425 
val fs = the_default [] (AList.lookup op = new_arities c); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

426 
val U = HOLogic.dest_setT (body_type T); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

427 
val Ts = HOLogic.prodT_factors' fs U; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

428 
val c' = Free (s ^ "p", 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

429 
map fastype_of params1 @ Ts > HOLogic.boolT) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

430 
in 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

431 
((c', (fs, U, Ts)), 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

432 
(list_comb (c, params2), 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

433 
HOLogic.Collect_const U $ HOLogic.ap_split' fs U HOLogic.boolT 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

434 
(list_comb (c', params1)))) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

435 
end) > split_list >> split_list; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

436 
val eqns' = eqns @ 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

437 
map (prop_of #> HOLogic.dest_Trueprop #> HOLogic.dest_eq) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

438 
(mem_Collect_eq :: split_conv :: to_pred_simps); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

439 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

440 
(* predicate version of the introduction rules *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

441 
val intros' = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

442 
map (fn (name_atts, t) => (name_atts, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

443 
t > 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

444 
map_aterms (fn u => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

445 
(case AList.lookup op = params' u of 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

446 
SOME (_, (u', _)) => u' 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

447 
 NONE => u)) > 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

448 
Pattern.rewrite_term thy [] [to_pred_proc thy eqns'] > 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

449 
eta_contract (member op = cs' orf is_pred pred_arities))) intros; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

450 
val cnames_syn' = map (fn (s, _) => (s ^ "p", NoSyn)) cnames_syn; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

451 
val monos' = map (to_pred [] (Context.Proof ctxt)) monos; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

452 
val ({preds, intrs, elims, raw_induct, ...}, ctxt1) = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

453 
InductivePackage.add_ind_def verbose "" coind 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

454 
no_elim no_ind cs' intros' monos' params1 cnames_syn' ctxt; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

455 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

456 
(* define inductive sets using previously defined predicates *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

457 
val (defs, ctxt2) = LocalTheory.defs Thm.internalK 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

458 
(map (fn ((c_syn, (fs, U, _)), p) => (c_syn, (("", []), 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

459 
fold_rev lambda params (HOLogic.Collect_const U $ 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

460 
HOLogic.ap_split' fs U HOLogic.boolT (list_comb (p, params3)))))) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

461 
(cnames_syn ~~ cs_info ~~ preds)) ctxt1; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

462 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

463 
(* prove theorems for converting predicate to set notation *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

464 
val ctxt3 = fold 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

465 
(fn (((p, c as Free (s, _)), (fs, U, Ts)), (_, (_, def))) => fn ctxt => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

466 
let val conv_thm = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

467 
Goal.prove ctxt (map (fst o dest_Free) params) [] 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

468 
(HOLogic.mk_Trueprop (HOLogic.mk_eq 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

469 
(list_comb (p, params3), 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

470 
list_abs (map (pair "x") Ts, HOLogic.mk_mem 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

471 
(HOLogic.mk_tuple' fs U (map Bound (length fs downto 0)), 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

472 
list_comb (c, params)))))) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

473 
(K (REPEAT (rtac ext 1) THEN simp_tac (HOL_basic_ss addsimps 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

474 
[def, mem_Collect_eq, split_conv]) 1)) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

475 
in 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

476 
ctxt > note_theorem ((s ^ "p_" ^ s ^ "_eq", 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

477 
[Attrib.internal (K pred_set_conv_att)]), 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

478 
[conv_thm]) > snd 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

479 
end) (preds ~~ cs ~~ cs_info ~~ defs) ctxt2; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

480 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

481 
(* convert theorems to set notation *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

482 
val rec_name = if alt_name = "" then 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

483 
space_implode "_" (map fst cnames_syn) else alt_name; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

484 
val cnames = map (Sign.full_name (ProofContext.theory_of ctxt3) o #1) cnames_syn; (* FIXME *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

485 
val (intr_names, intr_atts) = split_list (map fst intros); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

486 
val raw_induct' = to_set [] (Context.Proof ctxt3) raw_induct; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

487 
val (intrs', elims', induct, ctxt4) = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

488 
InductivePackage.declare_rules rec_name coind no_ind cnames 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

489 
(map (to_set [] (Context.Proof ctxt3)) intrs) intr_names intr_atts 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

490 
(map (fn th => (to_set [] (Context.Proof ctxt3) th, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

491 
map fst (fst (RuleCases.get th)))) elims) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

492 
raw_induct' ctxt3 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

493 
in 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

494 
({intrs = intrs', elims = elims', induct = induct, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

495 
raw_induct = raw_induct', preds = map fst defs}, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

496 
ctxt4) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

497 
end; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

498 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

499 
val add_inductive_i = InductivePackage.gen_add_inductive_i add_ind_set_def; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

500 
val add_inductive = InductivePackage.gen_add_inductive add_ind_set_def; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

501 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

502 
val mono_add_att = to_pred_att [] #> InductivePackage.mono_add; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

503 
val mono_del_att = to_pred_att [] #> InductivePackage.mono_del; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

504 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

505 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

506 
(** package setup **) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

507 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

508 
(* setup theory *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

509 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

510 
val setup = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

511 
Attrib.add_attributes 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

512 
[("pred_set_conv", Attrib.no_args pred_set_conv_att, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

513 
"declare rules for converting between predicate and set notation"), 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

514 
("to_set", Attrib.syntax (Attrib.thms >> to_set_att), 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

515 
"convert rule to set notation"), 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

516 
("to_pred", Attrib.syntax (Attrib.thms >> to_pred_att), 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

517 
"convert rule to predicate notation")] #> 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

518 
Codegen.add_attribute "ind_set" 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

519 
(Scan.option (Args.$$$ "target"  Args.colon  Args.name) >> code_ind_att) #> 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

520 
Codegen.add_preprocessor codegen_preproc #> 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

521 
Attrib.add_attributes [("mono_set", Attrib.add_del_args mono_add_att mono_del_att, 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

522 
"declaration of monotonicity rule for set operators")] #> 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

523 
Context.theory_map (Simplifier.map_ss (fn ss => 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

524 
ss addsimprocs [collect_mem_simproc])); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

525 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

526 
(* outer syntax *) 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

527 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

528 
local structure P = OuterParse and K = OuterKeyword in 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

529 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

530 
val ind_set_decl = InductivePackage.gen_ind_decl add_ind_set_def; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

531 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

532 
val inductive_setP = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

533 
OuterSyntax.command "inductive_set" "define inductive sets" K.thy_decl (ind_set_decl false); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

534 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

535 
val coinductive_setP = 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

536 
OuterSyntax.command "coinductive_set" "define coinductive sets" K.thy_decl (ind_set_decl true); 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

537 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

538 
val _ = OuterSyntax.add_parsers [inductive_setP, coinductive_setP]; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

539 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

540 
end; 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

541 

15f81c5d5330
New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff
changeset

542 
end; 