author  wenzelm 
Fri, 02 Oct 2009 22:15:08 +0200  
changeset 32861  105f40051387 
parent 32795  a0f38d8d633a 
child 32872  019201eb7e07 
permissions  rwrr 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

1 
(* Title: Tools/code/code_preproc.ML 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

2 
Author: Florian Haftmann, TU Muenchen 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

3 

31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

4 
Preprocessing code equations into a wellsorted system 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

5 
in a graph with explicit dependencies. 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

6 
*) 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

7 

31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

8 
signature CODE_PREPROC = 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

9 
sig 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

10 
val map_pre: (simpset > simpset) > theory > theory 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

11 
val map_post: (simpset > simpset) > theory > theory 
32072  12 
val add_unfold: thm > theory > theory 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

13 
val add_functrans: string * (theory > (thm * bool) list > (thm * bool) list option) > theory > theory 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

14 
val del_functrans: string > theory > theory 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

15 
val simple_functrans: (theory > thm list > thm list option) 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

16 
> theory > (thm * bool) list > (thm * bool) list option 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

17 
val print_codeproc: theory > unit 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

18 

30947  19 
type code_algebra 
20 
type code_graph 

21 
val eqns: code_graph > string > (thm * bool) list 

22 
val typ: code_graph > string > (string * sort) list * typ 

23 
val all: code_graph > string list 

24 
val pretty: theory > code_graph > Pretty.T 

25 
val obtain: theory > string list > term list > code_algebra * code_graph 

32123
8bac9ee4b28d
integrated add_triv_classes into evaluation stack
haftmann
parents:
32072
diff
changeset

26 
val eval_conv: theory 
30947  27 
> (code_algebra > code_graph > (string * sort) list > term > cterm > thm) > cterm > thm 
32123
8bac9ee4b28d
integrated add_triv_classes into evaluation stack
haftmann
parents:
32072
diff
changeset

28 
val eval: theory > ((term > term) > 'a > 'a) 
30947  29 
> (code_algebra > code_graph > (string * sort) list > term > 'a) > term > 'a 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

30 

80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

31 
val setup: theory > theory 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

32 
end 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

33 

31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

34 
structure Code_Preproc : CODE_PREPROC = 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

35 
struct 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

36 

31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

37 
(** preprocessor administration **) 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

38 

80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

39 
(* theory data *) 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

40 

80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

41 
datatype thmproc = Thmproc of { 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

42 
pre: simpset, 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

43 
post: simpset, 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

44 
functrans: (string * (serial * (theory > (thm * bool) list > (thm * bool) list option))) list 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

45 
}; 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

46 

31599  47 
fun make_thmproc ((pre, post), functrans) = 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

48 
Thmproc { pre = pre, post = post, functrans = functrans }; 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

49 
fun map_thmproc f (Thmproc { pre, post, functrans }) = 
31599  50 
make_thmproc (f ((pre, post), functrans)); 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

51 
fun merge_thmproc (Thmproc { pre = pre1, post = post1, functrans = functrans1 }, 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

52 
Thmproc { pre = pre2, post = post2, functrans = functrans2 }) = 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

53 
let 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

54 
val pre = Simplifier.merge_ss (pre1, pre2); 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

55 
val post = Simplifier.merge_ss (post1, post2); 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

56 
val functrans = AList.merge (op =) (eq_fst (op =)) (functrans1, functrans2); 
31599  57 
in make_thmproc ((pre, post), functrans) end; 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

58 

80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

59 
structure Code_Preproc_Data = TheoryDataFun 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

60 
( 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

61 
type T = thmproc; 
31599  62 
val empty = make_thmproc ((Simplifier.empty_ss, Simplifier.empty_ss), []); 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

63 
fun copy spec = spec; 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

64 
val extend = copy; 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

65 
fun merge pp = merge_thmproc; 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

66 
); 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

67 

80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

68 
fun the_thmproc thy = case Code_Preproc_Data.get thy 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

69 
of Thmproc x => x; 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

70 

80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

71 
fun delete_force msg key xs = 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

72 
if AList.defined (op =) xs key then AList.delete (op =) key xs 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

73 
else error ("No such " ^ msg ^ ": " ^ quote key); 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

74 

32544  75 
fun map_data f = Code.purge_data 
76 
#> (Code_Preproc_Data.map o map_thmproc) f; 

31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

77 

32072  78 
val map_pre_post = map_data o apfst; 
79 
val map_pre = map_pre_post o apfst; 

80 
val map_post = map_pre_post o apsnd; 

31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

81 

32072  82 
val add_unfold = map_pre o MetaSimplifier.add_simp; 
83 
val del_unfold = map_pre o MetaSimplifier.del_simp; 

31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

84 
val add_post = map_post o MetaSimplifier.add_simp; 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

85 
val del_post = map_post o MetaSimplifier.del_simp; 
32072  86 

87 
fun add_unfold_post raw_thm thy = 

88 
let 

89 
val thm = LocalDefs.meta_rewrite_rule (ProofContext.init thy) raw_thm; 

90 
val thm_sym = Thm.symmetric thm; 

91 
in 

92 
thy > map_pre_post (fn (pre, post) => 

93 
(pre > MetaSimplifier.add_simp thm, post > MetaSimplifier.del_simp thm_sym)) 

94 
end; 

95 

31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

96 
fun add_functrans (name, f) = (map_data o apsnd) 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

97 
(AList.update (op =) (name, (serial (), f))); 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

98 

80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

99 
fun del_functrans name = (map_data o apsnd) 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

100 
(delete_force "function transformer" name); 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

101 

80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

102 

80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

103 
(* post and preprocessing *) 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

104 

80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

105 
fun apply_functrans thy c _ [] = [] 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

106 
 apply_functrans thy c [] eqns = eqns 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

107 
 apply_functrans thy c functrans eqns = eqns 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

108 
> perhaps (perhaps_loop (perhaps_apply functrans)) 
32349  109 
> Code.assert_eqns_const thy c 
110 
(*FIXME in future, the check here should be more accurate wrt. type schemes 

111 
 perhaps by means of upcoming code certificates with a corresponding 

112 
preprocessor protocol*); 

31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

113 

80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

114 
fun rhs_conv conv thm = Thm.transitive thm ((conv o Thm.rhs_of) thm); 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

115 

31957  116 
fun eqn_conv conv = 
117 
let 

118 
fun lhs_conv ct = if can Thm.dest_comb ct 

119 
then Conv.combination_conv lhs_conv conv ct 

120 
else Conv.all_conv ct; 

121 
in Conv.combination_conv (Conv.arg_conv lhs_conv) conv end; 

122 

123 
val rewrite_eqn = Conv.fconv_rule o eqn_conv o Simplifier.rewrite; 

124 

31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

125 
fun term_of_conv thy f = 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

126 
Thm.cterm_of thy 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

127 
#> f 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

128 
#> Thm.prop_of 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

129 
#> Logic.dest_equals 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

130 
#> snd; 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

131 

80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

132 
fun preprocess thy c eqns = 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

133 
let 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

134 
val pre = (Simplifier.theory_context thy o #pre o the_thmproc) thy; 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

135 
val functrans = (map (fn (_, (_, f)) => f thy) o #functrans 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

136 
o the_thmproc) thy; 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

137 
in 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

138 
eqns 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

139 
> apply_functrans thy c functrans 
31957  140 
> (map o apfst) (rewrite_eqn pre) 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

141 
> (map o apfst) (AxClass.unoverload thy) 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

142 
> map (Code.assert_eqn thy) 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

143 
end; 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

144 

80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

145 
fun preprocess_conv thy ct = 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

146 
let 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

147 
val pre = (Simplifier.theory_context thy o #pre o the_thmproc) thy; 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

148 
in 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

149 
ct 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

150 
> Simplifier.rewrite pre 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

151 
> rhs_conv (AxClass.unoverload_conv thy) 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

152 
end; 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

153 

80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

154 
fun postprocess_conv thy ct = 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

155 
let 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

156 
val post = (Simplifier.theory_context thy o #post o the_thmproc) thy; 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

157 
in 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

158 
ct 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

159 
> AxClass.overload_conv thy 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

160 
> rhs_conv (Simplifier.rewrite post) 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

161 
end; 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

162 

32544  163 
fun postprocess_term thy = term_of_conv thy (postprocess_conv thy); 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

164 

80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

165 
fun print_codeproc thy = 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

166 
let 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

167 
val ctxt = ProofContext.init thy; 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

168 
val pre = (#pre o the_thmproc) thy; 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

169 
val post = (#post o the_thmproc) thy; 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

170 
val functrans = (map fst o #functrans o the_thmproc) thy; 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

171 
in 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

172 
(Pretty.writeln o Pretty.chunks) [ 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

173 
Pretty.block [ 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

174 
Pretty.str "preprocessing simpset:", 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

175 
Pretty.fbrk, 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

176 
Simplifier.pretty_ss ctxt pre 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

177 
], 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

178 
Pretty.block [ 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

179 
Pretty.str "postprocessing simpset:", 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

180 
Pretty.fbrk, 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

181 
Simplifier.pretty_ss ctxt post 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

182 
], 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

183 
Pretty.block ( 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

184 
Pretty.str "function transformers:" 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

185 
:: Pretty.fbrk 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

186 
:: (Pretty.fbreaks o map Pretty.str) functrans 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

187 
) 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

188 
] 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

189 
end; 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

190 

80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

191 
fun simple_functrans f thy eqns = case f thy (map fst eqns) 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

192 
of SOME thms' => SOME (map (rpair (forall snd eqns)) thms') 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

193 
 NONE => NONE; 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

194 

80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

195 

80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

196 
(** sort algebra and code equation graph types **) 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

197 

30947  198 
type code_algebra = (sort > sort) * Sorts.algebra; 
199 
type code_graph = (((string * sort) list * typ) * (thm * bool) list) Graph.T; 

30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

200 

862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

201 
fun eqns eqngr = these o Option.map snd o try (Graph.get_node eqngr); 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

202 
fun typ eqngr = fst o Graph.get_node eqngr; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

203 
fun all eqngr = Graph.keys eqngr; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

204 

862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

205 
fun pretty thy eqngr = 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

206 
AList.make (snd o Graph.get_node eqngr) (Graph.keys eqngr) 
31156  207 
> (map o apfst) (Code.string_of_const thy) 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

208 
> sort (string_ord o pairself fst) 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

209 
> map (fn (s, thms) => 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

210 
(Pretty.block o Pretty.fbreaks) ( 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

211 
Pretty.str s 
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or oldstyle versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
32072
diff
changeset

212 
:: map (Display.pretty_thm_global thy o fst) thms 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

213 
)) 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

214 
> Pretty.chunks; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

215 

862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

216 

862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

217 
(** the Waisenhaus algorithm **) 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

218 

862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

219 
(* auxiliary *) 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

220 

30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

221 
fun is_proper_class thy = can (AxClass.get_info thy); 
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

222 

30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

223 
fun complete_proper_sort thy = 
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

224 
Sign.complete_sort thy #> filter (is_proper_class thy); 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

225 

30029  226 
fun inst_params thy tyco = 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

227 
map (fn (c, _) => AxClass.param_of_inst thy (c, tyco)) 
30029  228 
o maps (#params o AxClass.get_info thy); 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

229 

862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

230 
fun consts_of thy eqns = [] > (fold o fold o fold_aterms) 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

231 
(fn Const (c, ty) => insert (op =) (c, Sign.const_typargs thy (c, Logic.unvarifyT ty))  _ => I) 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

232 
(map (op :: o swap o apfst (snd o strip_comb) o Logic.dest_equals o Thm.plain_prop_of o fst) eqns); 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

233 

31957  234 
fun default_typscheme_of thy c = 
235 
let 

31977  236 
val ty = (snd o dest_Const o Term_Subst.zero_var_indexes o curry Const c 
31957  237 
o Type.strip_sorts o Sign.the_const_type thy) c; 
238 
in case AxClass.class_of_param thy c 

239 
of SOME class => ([(Name.aT, [class])], ty) 

240 
 NONE => Code.typscheme thy (c, ty) 

241 
end; 

242 

30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

243 
fun tyscm_rhss_of thy c eqns = 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

244 
let 
31957  245 
val tyscm = case eqns 
246 
of [] => default_typscheme_of thy c 

31156  247 
 ((thm, _) :: _) => Code.typscheme_eqn thy thm; 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

248 
val rhss = consts_of thy eqns; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

249 
in (tyscm, rhss) end; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

250 

862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

251 

862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

252 
(* data structures *) 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

253 

862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

254 
datatype const = Fun of string  Inst of class * string; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

255 

862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

256 
fun const_ord (Fun c1, Fun c2) = fast_string_ord (c1, c2) 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

257 
 const_ord (Inst class_tyco1, Inst class_tyco2) = 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

258 
prod_ord fast_string_ord fast_string_ord (class_tyco1, class_tyco2) 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

259 
 const_ord (Fun _, Inst _) = LESS 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

260 
 const_ord (Inst _, Fun _) = GREATER; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

261 

862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

262 
type var = const * int; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

263 

862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

264 
structure Vargraph = 
31971
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents:
31962
diff
changeset

265 
Graph(type key = var val ord = prod_ord const_ord int_ord); 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

266 

30054  267 
datatype styp = Tyco of string * styp list  Var of var  Free; 
268 

269 
fun styp_of c_lhs (Type (tyco, tys)) = Tyco (tyco, map (styp_of c_lhs) tys) 

270 
 styp_of c_lhs (TFree (v, _)) = case c_lhs 

271 
of SOME (c, lhs) => Var (Fun c, find_index (fn (v', _) => v = v') lhs) 

272 
 NONE => Free; 

30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

273 

30054  274 
type vardeps_data = ((string * styp list) list * class list) Vargraph.T 
275 
* (((string * sort) list * (thm * bool) list) Symtab.table 

276 
* (class * string) list); 

30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

277 

30054  278 
val empty_vardeps_data : vardeps_data = 
279 
(Vargraph.empty, (Symtab.empty, [])); 

280 

30876  281 

30054  282 
(* retrieving equations and instances from the background context *) 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

283 

862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

284 
fun obtain_eqns thy eqngr c = 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

285 
case try (Graph.get_node eqngr) c 
30058  286 
of SOME ((lhs, _), eqns) => ((lhs, []), []) 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

287 
 NONE => let 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

288 
val eqns = Code.these_eqns thy c 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

289 
> preprocess thy c; 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

290 
val ((lhs, _), rhss) = tyscm_rhss_of thy c eqns; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

291 
in ((lhs, rhss), eqns) end; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

292 

862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

293 
fun obtain_instance thy arities (inst as (class, tyco)) = 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

294 
case AList.lookup (op =) arities inst 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

295 
of SOME classess => (classess, ([], [])) 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

296 
 NONE => let 
30029  297 
val all_classes = complete_proper_sort thy [class]; 
298 
val superclasses = remove (op =) class all_classes 

30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

299 
val classess = map (complete_proper_sort thy) 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

300 
(Sign.arity_sorts thy tyco [class]); 
30029  301 
val inst_params = inst_params thy tyco all_classes; 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

302 
in (classess, (superclasses, inst_params)) end; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

303 

30054  304 

305 
(* computing instantiations *) 

306 

30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

307 
fun add_classes thy arities eqngr c_k new_classes vardeps_data = 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

308 
let 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

309 
val (styps, old_classes) = Vargraph.get_node (fst vardeps_data) c_k; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

310 
val diff_classes = new_classes > subtract (op =) old_classes; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

311 
in if null diff_classes then vardeps_data 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

312 
else let 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

313 
val c_ks = Vargraph.imm_succs (fst vardeps_data) c_k > insert (op =) c_k; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

314 
in 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

315 
vardeps_data 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

316 
> (apfst o Vargraph.map_node c_k o apsnd) (append diff_classes) 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

317 
> fold (fn styp => fold (ensure_typmatch_inst thy arities eqngr styp) new_classes) styps 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

318 
> fold (fn c_k => add_classes thy arities eqngr c_k diff_classes) c_ks 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

319 
end end 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

320 
and add_styp thy arities eqngr c_k tyco_styps vardeps_data = 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

321 
let 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

322 
val (old_styps, classes) = Vargraph.get_node (fst vardeps_data) c_k; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

323 
in if member (op =) old_styps tyco_styps then vardeps_data 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

324 
else 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

325 
vardeps_data 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

326 
> (apfst o Vargraph.map_node c_k o apfst) (cons tyco_styps) 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

327 
> fold (ensure_typmatch_inst thy arities eqngr tyco_styps) classes 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

328 
end 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

329 
and add_dep thy arities eqngr c_k c_k' vardeps_data = 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

330 
let 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

331 
val (_, classes) = Vargraph.get_node (fst vardeps_data) c_k; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

332 
in 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

333 
vardeps_data 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

334 
> add_classes thy arities eqngr c_k' classes 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

335 
> apfst (Vargraph.add_edge (c_k, c_k')) 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

336 
end 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

337 
and ensure_typmatch_inst thy arities eqngr (tyco, styps) class vardeps_data = 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

338 
if can (Sign.arity_sorts thy tyco) [class] 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

339 
then vardeps_data 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

340 
> ensure_inst thy arities eqngr (class, tyco) 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

341 
> fold_index (fn (k, styp) => 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

342 
ensure_typmatch thy arities eqngr styp (Inst (class, tyco), k)) styps 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

343 
else vardeps_data (*permissive!*) 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

344 
and ensure_inst thy arities eqngr (inst as (class, tyco)) (vardeps_data as (_, (_, insts))) = 
30054  345 
if member (op =) insts inst then vardeps_data 
346 
else let 

30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

347 
val (classess, (superclasses, inst_params)) = 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

348 
obtain_instance thy arities inst; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

349 
in 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

350 
vardeps_data 
30054  351 
> (apsnd o apsnd) (insert (op =) inst) 
30083  352 
> fold_index (fn (k, _) => 
353 
apfst (Vargraph.new_node ((Inst (class, tyco), k), ([] ,[])))) classess 

31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

354 
> fold (fn superclass => ensure_inst thy arities eqngr (superclass, tyco)) superclasses 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

355 
> fold (ensure_fun thy arities eqngr) inst_params 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

356 
> fold_index (fn (k, classes) => 
30075  357 
add_classes thy arities eqngr (Inst (class, tyco), k) classes 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

358 
#> fold (fn superclass => 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

359 
add_dep thy arities eqngr (Inst (superclass, tyco), k) 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

360 
(Inst (class, tyco), k)) superclasses 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

361 
#> fold (fn inst_param => 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

362 
add_dep thy arities eqngr (Fun inst_param, k) 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

363 
(Inst (class, tyco), k) 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

364 
) inst_params 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

365 
) classess 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

366 
end 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

367 
and ensure_typmatch thy arities eqngr (Tyco tyco_styps) c_k vardeps_data = 
30054  368 
vardeps_data 
369 
> add_styp thy arities eqngr c_k tyco_styps 

31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

370 
 ensure_typmatch thy arities eqngr (Var c_k') c_k vardeps_data = 
30054  371 
vardeps_data 
372 
> add_dep thy arities eqngr c_k c_k' 

31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

373 
 ensure_typmatch thy arities eqngr Free c_k vardeps_data = 
30054  374 
vardeps_data 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

375 
and ensure_rhs thy arities eqngr (c', styps) vardeps_data = 
30054  376 
vardeps_data 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

377 
> ensure_fun thy arities eqngr c' 
30054  378 
> fold_index (fn (k, styp) => 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

379 
ensure_typmatch thy arities eqngr styp (Fun c', k)) styps 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

380 
and ensure_fun thy arities eqngr c (vardeps_data as (_, (eqntab, _))) = 
30054  381 
if Symtab.defined eqntab c then vardeps_data 
382 
else let 

30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

383 
val ((lhs, rhss), eqns) = obtain_eqns thy eqngr c; 
30054  384 
val rhss' = (map o apsnd o map) (styp_of (SOME (c, lhs))) rhss; 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

385 
in 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

386 
vardeps_data 
30054  387 
> (apsnd o apfst) (Symtab.update_new (c, (lhs, eqns))) 
30083  388 
> fold_index (fn (k, _) => 
389 
apfst (Vargraph.new_node ((Fun c, k), ([] ,[])))) lhs 

30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

390 
> fold_index (fn (k, (_, sort)) => 
30083  391 
add_classes thy arities eqngr (Fun c, k) (complete_proper_sort thy sort)) lhs 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

392 
> fold (ensure_rhs thy arities eqngr) rhss' 
30054  393 
end; 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

394 

862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

395 

862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

396 
(* applying instantiations *) 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

397 

862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

398 
fun dicts_of thy (proj_sort, algebra) (T, sort) = 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

399 
let 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

400 
fun class_relation (x, _) _ = x; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

401 
fun type_constructor tyco xs class = 
30054  402 
inst_params thy tyco (Sorts.complete_sort algebra [class]) 
403 
@ (maps o maps) fst xs; 

30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

404 
fun type_variable (TFree (_, sort)) = map (pair []) (proj_sort sort); 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

405 
in 
32795  406 
flat (Sorts.of_sort_derivation algebra 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

407 
{ class_relation = class_relation, type_constructor = type_constructor, 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

408 
type_variable = type_variable } (T, proj_sort sort) 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

409 
handle Sorts.CLASS_ERROR _ => [] (*permissive!*)) 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

410 
end; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

411 

31957  412 
fun inst_thm thy tvars' thm = 
413 
let 

414 
val tvars = (Term.add_tvars o Thm.prop_of) thm []; 

415 
val inter_sort = Sorts.inter_sort (Sign.classes_of thy); 

416 
fun mk_inst (tvar as (v, sort)) = case Vartab.lookup tvars' v 

417 
of SOME sort' => SOME (pairself (Thm.ctyp_of thy o TVar) 

418 
(tvar, (v, inter_sort (sort, sort')))) 

419 
 NONE => NONE; 

420 
val insts = map_filter mk_inst tvars; 

421 
in Thm.instantiate (insts, []) thm end; 

422 

30054  423 
fun add_arity thy vardeps (class, tyco) = 
30058  424 
AList.default (op =) 
30054  425 
((class, tyco), map (fn k => (snd o Vargraph.get_node vardeps) (Inst (class, tyco), k)) 
426 
(0 upto Sign.arity_number thy tyco  1)); 

30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

427 

30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

428 
fun add_eqs thy vardeps (c, (proto_lhs, proto_eqns)) (rhss, eqngr) = 
30058  429 
if can (Graph.get_node eqngr) c then (rhss, eqngr) 
430 
else let 

30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

431 
val lhs = map_index (fn (k, (v, _)) => 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

432 
(v, snd (Vargraph.get_node vardeps (Fun c, k)))) proto_lhs; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

433 
val inst_tab = Vartab.empty > fold (fn (v, sort) => 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

434 
Vartab.update ((v, 0), sort)) lhs; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

435 
val eqns = proto_eqns 
31957  436 
> (map o apfst) (inst_thm thy inst_tab); 
30054  437 
val (tyscm, rhss') = tyscm_rhss_of thy c eqns; 
438 
val eqngr' = Graph.new_node (c, (tyscm, eqns)) eqngr; 

439 
in (map (pair c) rhss' @ rhss, eqngr') end; 

30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

440 

30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

441 
fun extend_arities_eqngr thy cs ts (arities, eqngr) = 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

442 
let 
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

443 
val cs_rhss = (fold o fold_aterms) (fn Const (c_ty as (c, _)) => 
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

444 
insert (op =) (c, (map (styp_of NONE) o Sign.const_typargs thy) c_ty)  _ => I) ts []; 
30054  445 
val (vardeps, (eqntab, insts)) = empty_vardeps_data 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

446 
> fold (ensure_fun thy arities eqngr) cs 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

447 
> fold (ensure_rhs thy arities eqngr) cs_rhss; 
30054  448 
val arities' = fold (add_arity thy vardeps) insts arities; 
30064  449 
val pp = Syntax.pp_global thy; 
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

450 
val algebra = Sorts.subalgebra pp (is_proper_class thy) 
30064  451 
(AList.lookup (op =) arities') (Sign.classes_of thy); 
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

452 
val (rhss, eqngr') = Symtab.fold (add_eqs thy vardeps) eqntab ([], eqngr); 
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

453 
fun deps_of (c, rhs) = c :: maps (dicts_of thy algebra) 
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

454 
(rhs ~~ (map snd o fst o fst o Graph.get_node eqngr') c); 
30054  455 
val eqngr'' = fold (fn (c, rhs) => fold 
456 
(curry Graph.add_edge c) (deps_of rhs)) rhss eqngr'; 

30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

457 
in (algebra, (arities', eqngr'')) end; 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

458 

862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

459 

31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

460 
(** store for preprocessed arities and code equations **) 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

461 

31962  462 
structure Wellsorted = Code_Data_Fun 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

463 
( 
30947  464 
type T = ((string * class) * sort list) list * code_graph; 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

465 
val empty = ([], Graph.empty); 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

466 
fun purge thy cs (arities, eqngr) = 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

467 
let 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

468 
val del_cs = ((Graph.all_preds eqngr 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

469 
o filter (can (Graph.get_node eqngr))) cs); 
30050
916a8427f65a
first attempt to solve evaluation bootstrap problem
haftmann
parents:
30029
diff
changeset

470 
val del_arities = del_cs 
916a8427f65a
first attempt to solve evaluation bootstrap problem
haftmann
parents:
30029
diff
changeset

471 
> map_filter (AxClass.inst_of_param thy) 
916a8427f65a
first attempt to solve evaluation bootstrap problem
haftmann
parents:
30029
diff
changeset

472 
> maps (fn (c, tyco) => 
916a8427f65a
first attempt to solve evaluation bootstrap problem
haftmann
parents:
30029
diff
changeset

473 
(map (rpair tyco) o Sign.complete_sort thy o the_list 
916a8427f65a
first attempt to solve evaluation bootstrap problem
haftmann
parents:
30029
diff
changeset

474 
o AxClass.class_of_param thy) c); 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

475 
val arities' = fold (AList.delete (op =)) del_arities arities; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

476 
val eqngr' = Graph.del_nodes del_cs eqngr; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

477 
in (arities', eqngr') end; 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

478 
); 
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

479 

862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

480 

31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

481 
(** retrieval and evaluation interfaces **) 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

482 

30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

483 
fun obtain thy cs ts = apsnd snd 
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

484 
(Wellsorted.change_yield thy (extend_arities_eqngr thy cs ts)); 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

485 

32123
8bac9ee4b28d
integrated add_triv_classes into evaluation stack
haftmann
parents:
32072
diff
changeset

486 
fun gen_eval thy cterm_of conclude_evaluation evaluator proto_ct = 
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

487 
let 
31063
88aaab83b6fc
dropped explicit suppport for frees in evaluation conversion stack
haftmann
parents:
31036
diff
changeset

488 
val pp = Syntax.pp_global thy; 
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

489 
val ct = cterm_of proto_ct; 
31063
88aaab83b6fc
dropped explicit suppport for frees in evaluation conversion stack
haftmann
parents:
31036
diff
changeset

490 
val _ = (Sign.no_frees pp o map_types (K dummyT) o Sign.no_vars pp) 
30947  491 
(Thm.term_of ct); 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

492 
val thm = preprocess_conv thy ct; 
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

493 
val ct' = Thm.rhs_of thm; 
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

494 
val t' = Thm.term_of ct'; 
30947  495 
val vs = Term.add_tfrees t' []; 
496 
val consts = fold_aterms 

497 
(fn Const (c, _) => insert (op =) c  _ => I) t' []; 

32544  498 
val (algebra', eqngr') = obtain thy consts [t']; 
499 
in conclude_evaluation (evaluator algebra' eqngr' vs t' ct') thm end; 

30947  500 

501 
fun simple_evaluator evaluator algebra eqngr vs t ct = 

502 
evaluator algebra eqngr vs t; 

30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

503 

30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

504 
fun eval_conv thy = 
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

505 
let 
30947  506 
fun conclude_evaluation thm2 thm1 = 
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

507 
let 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

508 
val thm3 = postprocess_conv thy (Thm.rhs_of thm2); 
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

509 
in 
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

510 
Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ => 
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

511 
error ("could not construct evaluation proof:\n" 
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or oldstyle versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
32072
diff
changeset

512 
^ (cat_lines o map (Display.string_of_thm_global thy)) [thm1, thm2, thm3]) 
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30876
diff
changeset

513 
end; 
30947  514 
in gen_eval thy I conclude_evaluation end; 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

515 

32123
8bac9ee4b28d
integrated add_triv_classes into evaluation stack
haftmann
parents:
32072
diff
changeset

516 
fun eval thy postproc evaluator = gen_eval thy (Thm.cterm_of thy) 
8bac9ee4b28d
integrated add_triv_classes into evaluation stack
haftmann
parents:
32072
diff
changeset

517 
(K o postproc (postprocess_term thy)) (simple_evaluator evaluator); 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

518 

80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

519 

80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

520 
(** setup **) 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

521 

80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

522 
val setup = 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

523 
let 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

524 
fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I); 
32072  525 
fun add_del_attribute_parser add del = 
31998
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31977
diff
changeset

526 
Attrib.add_del (mk_attribute add) (mk_attribute del); 
32072  527 
fun both f g thm = f thm #> g thm; 
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

528 
in 
32072  529 
Attrib.setup @{binding code_unfold} (add_del_attribute_parser 
530 
(both Codegen.add_unfold add_unfold) (both Codegen.del_unfold del_unfold)) 

531 
"preprocessing equations for code generators" 

532 
#> Attrib.setup @{binding code_inline} (add_del_attribute_parser add_unfold del_unfold) 

31998
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31977
diff
changeset

533 
"preprocessing equations for code generator" 
32072  534 
#> Attrib.setup @{binding code_post} (add_del_attribute_parser add_post del_post) 
31998
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31977
diff
changeset

535 
"postprocessing equations for code generator" 
32072  536 
#> Attrib.setup @{binding code_unfold_post} (Scan.succeed (mk_attribute add_unfold_post)) 
537 
"pre and postprocessing equations for code generator" 

31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

538 
end; 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

539 

80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

540 
val _ = 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

541 
OuterSyntax.improper_command "print_codeproc" "print code preprocessor setup" 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

542 
OuterKeyword.diag (Scan.succeed 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

543 
(Toplevel.no_timing o Toplevel.unknown_theory o Toplevel.keep 
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31089
diff
changeset

544 
(print_codeproc o Toplevel.theory_of))); 
30010
862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

545 

862fc7751a15
tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff
changeset

546 
end; (*struct*) 