author  blanchet 
Fri, 06 Aug 2010 17:05:29 +0200  
changeset 38240  a44d108a8d39 
parent 38215  1c7d7eaebdf2 
child 38242  f26d590dce0f 
permissions  rwrr 
33978
2380c1dac86e
fix soundness bug in Nitpick's "destroy_constrs" optimization
blanchet
parents:
33968
diff
changeset

1 
(* Title: HOL/Tools/Nitpick/nitpick_hol.ML 
33192  2 
Author: Jasmin Blanchette, TU Muenchen 
34982
7b8c366e34a2
added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents:
34936
diff
changeset

3 
Copyright 2008, 2009, 2010 
33192  4 

5 
Auxiliary HOLrelated functions used by Nitpick. 

6 
*) 

7 

8 
signature NITPICK_HOL = 

9 
sig 

33705
947184dc75c9
removed a few global names in Nitpick (styp, nat_less, pairf)
blanchet
parents:
33583
diff
changeset

10 
type styp = Nitpick_Util.styp 
33192  11 
type const_table = term list Symtab.table 
12 
type special_fun = (styp * int list * term list) * styp 

13 
type unrolled = styp * styp 

14 
type wf_cache = (styp * (bool * bool)) list 

15 

36390
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

16 
type hol_context = 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

17 
{thy: theory, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

18 
ctxt: Proof.context, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

19 
max_bisim_depth: int, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

20 
boxes: (typ option * bool option) list, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

21 
stds: (typ option * bool) list, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

22 
wfs: (styp option * bool option) list, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

23 
user_axioms: bool option, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

24 
debug: bool, 
38209  25 
whacks: term list, 
36390
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

26 
binary_ints: bool option, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

27 
destroy_constrs: bool, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

28 
specialize: bool, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

29 
star_linear_preds: bool, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

30 
fast_descrs: bool, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

31 
tac_timeout: Time.time option, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

32 
evals: term list, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

33 
case_names: (string * int) list, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

34 
def_table: const_table, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

35 
nondef_table: const_table, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

36 
user_nondefs: term list, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

37 
simp_table: const_table Unsynchronized.ref, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

38 
psimp_table: const_table, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

39 
choice_spec_table: const_table, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

40 
intro_table: const_table, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

41 
ground_thm_table: term list Inttab.table, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

42 
ersatz_table: (string * string) list, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

43 
skolems: (string * string list) list Unsynchronized.ref, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

44 
special_funs: special_fun list Unsynchronized.ref, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

45 
unrolled_preds: unrolled list Unsynchronized.ref, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

46 
wf_cache: wf_cache Unsynchronized.ref, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

47 
constr_cache: (typ * styp list) list Unsynchronized.ref} 
33192  48 

35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

49 
datatype fixpoint_kind = Lfp  Gfp  NoFp 
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

50 
datatype boxability = 
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

51 
InConstr  InSel  InExpr  InPair  InFunLHS  InFunRHS1  InFunRHS2 
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

52 

33192  53 
val name_sep : string 
54 
val numeral_prefix : string 

35718  55 
val base_prefix : string 
56 
val step_prefix : string 

57 
val unrolled_prefix : string 

35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

58 
val ubfp_prefix : string 
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

59 
val lbfp_prefix : string 
35311  60 
val quot_normal_prefix : string 
33192  61 
val skolem_prefix : string 
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

62 
val special_prefix : string 
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

63 
val uncurry_prefix : string 
33192  64 
val eval_prefix : string 
35718  65 
val iter_var_prefix : string 
66 
val strip_first_name_sep : string > string * string 

33192  67 
val original_name : string > string 
37476
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

68 
val abs_var : indexname * typ > term > term 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

69 
val s_let : string > int > typ > typ > (term > term) > term > term 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

70 
val s_betapply : typ list > term * term > term 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

71 
val s_betapplys : typ list > term * term list > term 
34998  72 
val s_conj : term * term > term 
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

73 
val s_disj : term * term > term 
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

74 
val strip_any_connective : term > term list * term 
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

75 
val conjuncts_of : term > term list 
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

76 
val disjuncts_of : term > term list 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

77 
val unarize_unbox_etc_type : typ > typ 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

78 
val uniterize_unarize_unbox_etc_type : typ > typ 
33192  79 
val string_for_type : Proof.context > typ > string 
38188  80 
val pretty_for_type : Proof.context > typ > Pretty.T 
33192  81 
val prefix_name : string > string > string 
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

82 
val shortest_name : string > string 
33192  83 
val short_name : string > string 
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

84 
val shorten_names_in_term : term > term 
35711
548d3f16404b
added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents:
35671
diff
changeset

85 
val strict_type_match : theory > typ * typ > bool 
33192  86 
val type_match : theory > typ * typ > bool 
87 
val const_match : theory > styp * styp > bool 

88 
val term_match : theory > term * term > bool 

35711
548d3f16404b
added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents:
35671
diff
changeset

89 
val frac_from_term_pair : typ > term > term > term 
33192  90 
val is_TFree : typ > bool 
91 
val is_higher_order_type : typ > bool 

92 
val is_fun_type : typ > bool 

93 
val is_set_type : typ > bool 

94 
val is_pair_type : typ > bool 

95 
val is_lfp_iterator_type : typ > bool 

96 
val is_gfp_iterator_type : typ > bool 

97 
val is_fp_iterator_type : typ > bool 

35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset

98 
val is_iterator_type : typ > bool 
33192  99 
val is_boolean_type : typ > bool 
100 
val is_integer_type : typ > bool 

34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset

101 
val is_bit_type : typ > bool 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset

102 
val is_word_type : typ > bool 
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

103 
val is_integer_like_type : typ > bool 
33192  104 
val is_record_type : typ > bool 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

105 
val is_number_type : Proof.context > typ > bool 
33192  106 
val const_for_iterator_type : typ > styp 
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

107 
val strip_n_binders : int > typ > typ list * typ 
33192  108 
val nth_range_type : int > typ > typ 
109 
val num_factors_in_type : typ > int 

110 
val num_binder_types : typ > int 

111 
val curried_binder_types : typ > typ list 

112 
val mk_flat_tuple : typ > term list > term 

113 
val dest_n_tuple : int > term > term list 

33978
2380c1dac86e
fix soundness bug in Nitpick's "destroy_constrs" optimization
blanchet
parents:
33968
diff
changeset

114 
val is_real_datatype : theory > string > bool 
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

115 
val is_standard_datatype : theory > (typ option * bool) list > typ > bool 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

116 
val is_codatatype : Proof.context > typ > bool 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

117 
val is_quot_type : Proof.context > typ > bool 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

118 
val is_pure_typedef : Proof.context > typ > bool 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

119 
val is_univ_typedef : Proof.context > typ > bool 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

120 
val is_datatype : Proof.context > (typ option * bool) list > typ > bool 
33192  121 
val is_record_constr : styp > bool 
122 
val is_record_get : theory > styp > bool 

123 
val is_record_update : theory > styp > bool 

37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

124 
val is_abs_fun : Proof.context > styp > bool 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

125 
val is_rep_fun : Proof.context > styp > bool 
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34126
diff
changeset

126 
val is_quot_abs_fun : Proof.context > styp > bool 
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34126
diff
changeset

127 
val is_quot_rep_fun : Proof.context > styp > bool 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

128 
val mate_of_rep_fun : Proof.context > styp > styp 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

129 
val is_constr_like : Proof.context > styp > bool 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

130 
val is_constr : Proof.context > (typ option * bool) list > styp > bool 
33192  131 
val is_sel : string > bool 
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset

132 
val is_sel_like_and_no_discr : string > bool 
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

133 
val box_type : hol_context > boxability > typ > typ 
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

134 
val binarize_nat_and_int_in_type : typ > typ 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

135 
val binarize_nat_and_int_in_term : term > term 
33192  136 
val discr_for_constr : styp > styp 
137 
val num_sels_for_constr_type : typ > int 

138 
val nth_sel_name_for_constr_name : string > int > string 

139 
val nth_sel_for_constr : styp > int > styp 

35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

140 
val binarized_and_boxed_nth_sel_for_constr : 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

141 
hol_context > bool > styp > int > styp 
33192  142 
val sel_no_from_name : string > int 
35078
6fd1052fe463
optimization to quantifiers in Nitpick's handling of simp rules + renamed some SAT solvers
blanchet
parents:
35075
diff
changeset

143 
val close_form : term > term 
33192  144 
val eta_expand : typ list > term > int > term 
145 
val distinctness_formula : typ > term list > term 

38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

146 
val register_frac_type : 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

147 
string > (string * string) list > Proof.context > Proof.context 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

148 
val register_frac_type_global : 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

149 
string > (string * string) list > theory > theory 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

150 
val unregister_frac_type : string > Proof.context > Proof.context 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

151 
val unregister_frac_type_global : string > theory > theory 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

152 
val register_codatatype : 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

153 
typ > string > styp list > Proof.context > Proof.context 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

154 
val register_codatatype_global : 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

155 
typ > string > styp list > theory > theory 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

156 
val unregister_codatatype : typ > Proof.context > Proof.context 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

157 
val unregister_codatatype_global : typ > theory > theory 
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

158 
val datatype_constrs : hol_context > typ > styp list 
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

159 
val binarized_and_boxed_datatype_constrs : 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

160 
hol_context > bool > typ > styp list 
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

161 
val num_datatype_constrs : hol_context > typ > int 
33192  162 
val constr_name_for_sel_like : string > string 
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

163 
val binarized_and_boxed_constr_for_sel : hol_context > bool > styp > styp 
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

164 
val discriminate_value : hol_context > styp > term > term 
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

165 
val select_nth_constr_arg : 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

166 
Proof.context > (typ option * bool) list > styp > term > int > typ 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

167 
> term 
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

168 
val construct_value : 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

169 
Proof.context > (typ option * bool) list > styp > term list > term 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

170 
val coerce_term : hol_context > typ list > typ > typ > term > term 
33192  171 
val card_of_type : (typ * int) list > typ > int 
172 
val bounded_card_of_type : int > int > (typ * int) list > typ > int 

34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset

173 
val bounded_exact_card_of_type : 
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35384
diff
changeset

174 
hol_context > typ list > int > int > (typ * int) list > typ > int 
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

175 
val is_finite_type : hol_context > typ > bool 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

176 
val is_small_finite_type : hol_context > typ > bool 
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

177 
val special_bounds : term list > (indexname * typ) list 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

178 
val is_funky_typedef : Proof.context > typ > bool 
35335  179 
val all_axioms_of : 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

180 
Proof.context > (term * term) list > term list * term list * term list 
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

181 
val arity_of_built_in_const : 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

182 
theory > (typ option * bool) list > bool > styp > int option 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

183 
val is_built_in_const : 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

184 
theory > (typ option * bool) list > bool > styp > bool 
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

185 
val term_under_def : term > term 
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

186 
val case_const_names : 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

187 
Proof.context > (typ option * bool) list > (string * int) list 
35718  188 
val unfold_defs_in_term : hol_context > term > term 
35335  189 
val const_def_table : 
190 
Proof.context > (term * term) list > term list > const_table 

33192  191 
val const_nondef_table : term list > const_table 
35335  192 
val const_simp_table : Proof.context > (term * term) list > const_table 
193 
val const_psimp_table : Proof.context > (term * term) list > const_table 

35807
e4d1b5cbd429
added support for "specification" and "ax_specification" constructs to Nitpick
blanchet
parents:
35743
diff
changeset

194 
val const_choice_spec_table : 
e4d1b5cbd429
added support for "specification" and "ax_specification" constructs to Nitpick
blanchet
parents:
35743
diff
changeset

195 
Proof.context > (term * term) list > const_table 
35335  196 
val inductive_intro_table : 
197 
Proof.context > (term * term) list > const_table > const_table 

33192  198 
val ground_theorem_table : theory > term list Inttab.table 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

199 
val ersatz_table : Proof.context > (string * string) list 
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

200 
val add_simps : const_table Unsynchronized.ref > string > term list > unit 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

201 
val inverse_axioms_for_rep_fun : Proof.context > styp > term list 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

202 
val optimized_typedef_axioms : Proof.context > string * typ list > term list 
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

203 
val optimized_quot_type_axioms : 
35311  204 
Proof.context > (typ option * bool) list > string * typ list > term list 
33192  205 
val def_of_const : theory > const_table > styp > term option 
35718  206 
val fixpoint_kind_of_rhs : term > fixpoint_kind 
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

207 
val fixpoint_kind_of_const : 
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

208 
theory > const_table > string * typ > fixpoint_kind 
38205
37a7272cb453
handle inductive predicates correctly after change in "def" semantics
blanchet
parents:
38204
diff
changeset

209 
val is_real_inductive_pred : hol_context > styp > bool 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

210 
val is_constr_pattern_lhs : Proof.context > term > bool 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

211 
val is_constr_pattern_formula : Proof.context > term > bool 
35807
e4d1b5cbd429
added support for "specification" and "ax_specification" constructs to Nitpick
blanchet
parents:
35743
diff
changeset

212 
val nondef_props_for_const : 
e4d1b5cbd429
added support for "specification" and "ax_specification" constructs to Nitpick
blanchet
parents:
35743
diff
changeset

213 
theory > bool > const_table > styp > term list 
e4d1b5cbd429
added support for "specification" and "ax_specification" constructs to Nitpick
blanchet
parents:
35743
diff
changeset

214 
val is_choice_spec_fun : hol_context > styp > bool 
e4d1b5cbd429
added support for "specification" and "ax_specification" constructs to Nitpick
blanchet
parents:
35743
diff
changeset

215 
val is_choice_spec_axiom : theory > const_table > term > bool 
38205
37a7272cb453
handle inductive predicates correctly after change in "def" semantics
blanchet
parents:
38204
diff
changeset

216 
val is_real_equational_fun : hol_context > styp > bool 
38202
379fb08da97b
prevent the expansion of too large definitions  use equations for these instead
blanchet
parents:
38201
diff
changeset

217 
val is_equational_fun_but_no_plain_def : hol_context > styp > bool 
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

218 
val codatatype_bisim_axioms : hol_context > typ > term list 
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

219 
val is_well_founded_inductive_pred : hol_context > styp > bool 
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

220 
val unrolled_inductive_pred_const : hol_context > bool > styp > term 
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

221 
val equational_fun_axioms : hol_context > styp > term list 
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

222 
val is_equational_fun_surely_complete : hol_context > styp > bool 
38212
a7e92239922f
improved "merge_type_vars" option: map supersorts to subsorts, to avoid distinguishing, say, "{}", and "HOL.type"
blanchet
parents:
38209
diff
changeset

223 
val merged_type_var_table_for_terms : 
a7e92239922f
improved "merge_type_vars" option: map supersorts to subsorts, to avoid distinguishing, say, "{}", and "HOL.type"
blanchet
parents:
38209
diff
changeset

224 
theory > term list > (sort * string) list 
a7e92239922f
improved "merge_type_vars" option: map supersorts to subsorts, to avoid distinguishing, say, "{}", and "HOL.type"
blanchet
parents:
38209
diff
changeset

225 
val merge_type_vars_in_term : 
a7e92239922f
improved "merge_type_vars" option: map supersorts to subsorts, to avoid distinguishing, say, "{}", and "HOL.type"
blanchet
parents:
38209
diff
changeset

226 
theory > bool > (sort * string) list > term > term 
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

227 
val ground_types_in_type : hol_context > bool > typ > typ list 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

228 
val ground_types_in_terms : hol_context > bool > term list > typ list 
33192  229 
end; 
230 

33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset

231 
structure Nitpick_HOL : NITPICK_HOL = 
33192  232 
struct 
233 

33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset

234 
open Nitpick_Util 
33192  235 

236 
type const_table = term list Symtab.table 

237 
type special_fun = (styp * int list * term list) * styp 

238 
type unrolled = styp * styp 

239 
type wf_cache = (styp * (bool * bool)) list 

240 

36390
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

241 
type hol_context = 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

242 
{thy: theory, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

243 
ctxt: Proof.context, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

244 
max_bisim_depth: int, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

245 
boxes: (typ option * bool option) list, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

246 
stds: (typ option * bool) list, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

247 
wfs: (styp option * bool option) list, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

248 
user_axioms: bool option, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

249 
debug: bool, 
38209  250 
whacks: term list, 
36390
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

251 
binary_ints: bool option, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

252 
destroy_constrs: bool, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

253 
specialize: bool, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

254 
star_linear_preds: bool, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

255 
fast_descrs: bool, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

256 
tac_timeout: Time.time option, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

257 
evals: term list, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

258 
case_names: (string * int) list, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

259 
def_table: const_table, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

260 
nondef_table: const_table, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

261 
user_nondefs: term list, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

262 
simp_table: const_table Unsynchronized.ref, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

263 
psimp_table: const_table, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

264 
choice_spec_table: const_table, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

265 
intro_table: const_table, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

266 
ground_thm_table: term list Inttab.table, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

267 
ersatz_table: (string * string) list, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

268 
skolems: (string * string list) list Unsynchronized.ref, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

269 
special_funs: special_fun list Unsynchronized.ref, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

270 
unrolled_preds: unrolled list Unsynchronized.ref, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

271 
wf_cache: wf_cache Unsynchronized.ref, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

272 
constr_cache: (typ * styp list) list Unsynchronized.ref} 
33192  273 

35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

274 
datatype fixpoint_kind = Lfp  Gfp  NoFp 
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

275 
datatype boxability = 
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

276 
InConstr  InSel  InExpr  InPair  InFunLHS  InFunRHS1  InFunRHS2 
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

277 

38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

278 
structure Data = Generic_Data( 
33192  279 
type T = {frac_types: (string * (string * string) list) list, 
280 
codatatypes: (string * (string * styp list)) list} 

281 
val empty = {frac_types = [], codatatypes = []} 

282 
val extend = I 

33522  283 
fun merge ({frac_types = fs1, codatatypes = cs1}, 
36391  284 
{frac_types = fs2, codatatypes = cs2}) : T = 
33699
f33b036ef318
permissive AList.merge  most likely setup for theory data (beware of spurious AList.DUP);
wenzelm
parents:
33583
diff
changeset

285 
{frac_types = AList.merge (op =) (K true) (fs1, fs2), 
f33b036ef318
permissive AList.merge  most likely setup for theory data (beware of spurious AList.DUP);
wenzelm
parents:
33583
diff
changeset

286 
codatatypes = AList.merge (op =) (K true) (cs1, cs2)}) 
33192  287 

288 
val name_sep = "$" 

289 
val numeral_prefix = nitpick_prefix ^ "num" ^ name_sep 

290 
val sel_prefix = nitpick_prefix ^ "sel" 

291 
val discr_prefix = nitpick_prefix ^ "is" ^ name_sep 

292 
val set_prefix = nitpick_prefix ^ "set" ^ name_sep 

293 
val lfp_iterator_prefix = nitpick_prefix ^ "lfpit" ^ name_sep 

294 
val gfp_iterator_prefix = nitpick_prefix ^ "gfpit" ^ name_sep 

295 
val unrolled_prefix = nitpick_prefix ^ "unroll" ^ name_sep 

296 
val base_prefix = nitpick_prefix ^ "base" ^ name_sep 

297 
val step_prefix = nitpick_prefix ^ "step" ^ name_sep 

298 
val ubfp_prefix = nitpick_prefix ^ "ubfp" ^ name_sep 

299 
val lbfp_prefix = nitpick_prefix ^ "lbfp" ^ name_sep 

35311  300 
val quot_normal_prefix = nitpick_prefix ^ "qn" ^ name_sep 
33192  301 
val skolem_prefix = nitpick_prefix ^ "sk" 
302 
val special_prefix = nitpick_prefix ^ "sp" 

303 
val uncurry_prefix = nitpick_prefix ^ "unc" 

304 
val eval_prefix = nitpick_prefix ^ "eval" 

305 
val iter_var_prefix = "i" 

35718  306 

307 
(** Constant/type information and term/type manipulation **) 

33192  308 

309 
fun sel_prefix_for j = sel_prefix ^ string_of_int j ^ name_sep 

35311  310 
fun quot_normal_name_for_type ctxt T = 
311 
quot_normal_prefix ^ unyxml (Syntax.string_of_typ ctxt T) 

33192  312 

313 
val strip_first_name_sep = 

314 
Substring.full #> Substring.position name_sep ##> Substring.triml 1 

315 
#> pairself Substring.string 

316 
fun original_name s = 

317 
if String.isPrefix nitpick_prefix s then 

318 
case strip_first_name_sep s of (s1, "") => s1  (_, s2) => original_name s2 

319 
else 

320 
s 

35718  321 

37476
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

322 
fun abs_var ((s, j), T) body = Abs (s, T, abstract_over (Var ((s, j), T), body)) 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

323 

0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

324 
fun let_var s = (nitpick_prefix ^ s, 999) 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

325 
val let_inline_threshold = 20 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

326 

0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

327 
fun s_let s n abs_T body_T f t = 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

328 
if (n  1) * (size_of_term t  1) <= let_inline_threshold then 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

329 
f t 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

330 
else 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

331 
let val z = (let_var s, abs_T) in 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

332 
Const (@{const_name Let}, abs_T > (abs_T > body_T) > body_T) 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

333 
$ t $ abs_var z (incr_boundvars 1 (f (Var z))) 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

334 
end 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

335 

0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

336 
fun loose_bvar1_count (Bound i, k) = if i = k then 1 else 0 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

337 
 loose_bvar1_count (t1 $ t2, k) = 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

338 
loose_bvar1_count (t1, k) + loose_bvar1_count (t2, k) 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

339 
 loose_bvar1_count (Abs (_, _, t), k) = loose_bvar1_count (t, k + 1) 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

340 
 loose_bvar1_count _ = 0 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

341 

0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

342 
fun s_betapply _ (Const (@{const_name If}, _) $ @{const True} $ t1', _) = t1' 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

343 
 s_betapply _ (Const (@{const_name If}, _) $ @{const False} $ _, t2) = t2 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

344 
 s_betapply Ts (Const (@{const_name Let}, 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

345 
Type (_, [bound_T, Type (_, [_, body_T])])) 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

346 
$ t12 $ Abs (s, T, t13'), t2) = 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

347 
let val body_T' = range_type body_T in 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

348 
Const (@{const_name Let}, bound_T > (bound_T > body_T') > body_T') 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

349 
$ t12 $ Abs (s, T, s_betapply (T :: Ts) (t13', incr_boundvars 1 t2)) 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

350 
end 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

351 
 s_betapply Ts (t1 as Abs (s1, T1, t1'), t2) = 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

352 
(s_let s1 (loose_bvar1_count (t1', 0)) T1 (fastype_of1 (T1 :: Ts, t1')) 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

353 
(curry betapply t1) t2 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

354 
handle TERM _ => betapply (t1, t2)) (* FIXME: fix all uses *) 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

355 
 s_betapply _ (t1, t2) = t1 $ t2 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

356 
fun s_betapplys Ts = Library.foldl (s_betapply Ts) 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

357 

0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

358 
fun s_beta_norm Ts t = 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

359 
let 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

360 
fun aux _ (Var _) = raise Same.SAME 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

361 
 aux Ts (Abs (s, T, t')) = Abs (s, T, aux (T :: Ts) t') 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

362 
 aux Ts ((t1 as Abs _) $ t2) = 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

363 
Same.commit (aux Ts) (s_betapply Ts (t1, t2)) 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

364 
 aux Ts (t1 $ t2) = 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

365 
((case aux Ts t1 of 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

366 
t1 as Abs _ => Same.commit (aux Ts) (s_betapply Ts (t1, t2)) 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

367 
 t1 => t1 $ Same.commit (aux Ts) t2) 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

368 
handle Same.SAME => t1 $ aux Ts t2) 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

369 
 aux _ _ = raise Same.SAME 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

370 
in aux Ts t handle Same.SAME => t end 
33192  371 

34998  372 
fun s_conj (t1, @{const True}) = t1 
373 
 s_conj (@{const True}, t2) = t2 

374 
 s_conj (t1, t2) = 

375 
if t1 = @{const False} orelse t2 = @{const False} then @{const False} 

376 
else HOLogic.mk_conj (t1, t2) 

377 
fun s_disj (t1, @{const False}) = t1 

378 
 s_disj (@{const False}, t2) = t2 

379 
 s_disj (t1, t2) = 

380 
if t1 = @{const True} orelse t2 = @{const True} then @{const True} 

381 
else HOLogic.mk_disj (t1, t2) 

382 

383 
fun strip_connective conn_t (t as (t0 $ t1 $ t2)) = 

384 
if t0 = conn_t then strip_connective t0 t2 @ strip_connective t0 t1 else [t] 

385 
 strip_connective _ t = [t] 

35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset

386 
fun strip_any_connective (t as (t0 $ _ $ _)) = 
34998  387 
if t0 = @{const "op &"} orelse t0 = @{const "op "} then 
388 
(strip_connective t0 t, t0) 

389 
else 

390 
([t], @{const Not}) 

391 
 strip_any_connective t = ([t], @{const Not}) 

35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

392 
val conjuncts_of = strip_connective @{const "op &"} 
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

393 
val disjuncts_of = strip_connective @{const "op "} 
34998  394 

33192  395 
(* When you add constants to these lists, make sure to handle them in 
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset

396 
"Nitpick_Nut.nut_from_term", and perhaps in "Nitpick_Mono.consider_term" as 
33192  397 
well. *) 
398 
val built_in_consts = 

399 
[(@{const_name all}, 1), 

400 
(@{const_name "=="}, 2), 

401 
(@{const_name "==>"}, 2), 

402 
(@{const_name Pure.conjunction}, 2), 

403 
(@{const_name Trueprop}, 1), 

404 
(@{const_name Not}, 1), 

405 
(@{const_name False}, 0), 

406 
(@{const_name True}, 0), 

407 
(@{const_name All}, 1), 

408 
(@{const_name Ex}, 1), 

37476
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

409 
(@{const_name "op ="}, 1), 
33192  410 
(@{const_name "op &"}, 2), 
411 
(@{const_name "op "}, 2), 

412 
(@{const_name "op >"}, 2), 

413 
(@{const_name If}, 3), 

414 
(@{const_name Let}, 2), 

415 
(@{const_name Pair}, 2), 

416 
(@{const_name fst}, 1), 

417 
(@{const_name snd}, 1), 

418 
(@{const_name Id}, 0), 

419 
(@{const_name converse}, 1), 

420 
(@{const_name trancl}, 1), 

421 
(@{const_name rel_comp}, 2), 

422 
(@{const_name image}, 2), 

423 
(@{const_name finite}, 1), 

34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34126
diff
changeset

424 
(@{const_name unknown}, 0), 
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34126
diff
changeset

425 
(@{const_name is_unknown}, 1), 
35671
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = 1"
blanchet
parents:
35665
diff
changeset

426 
(@{const_name safe_The}, 1), 
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = 1"
blanchet
parents:
35665
diff
changeset

427 
(@{const_name safe_Eps}, 1), 
33192  428 
(@{const_name Frac}, 0), 
429 
(@{const_name norm_frac}, 0)] 

35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

430 
val built_in_nat_consts = 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

431 
[(@{const_name Suc}, 0), 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

432 
(@{const_name nat}, 0), 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

433 
(@{const_name nat_gcd}, 0), 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

434 
(@{const_name nat_lcm}, 0)] 
33192  435 
val built_in_descr_consts = 
436 
[(@{const_name The}, 1), 

437 
(@{const_name Eps}, 1)] 

438 
val built_in_typed_consts = 

35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

439 
[((@{const_name zero_class.zero}, int_T), 0), 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

440 
((@{const_name one_class.one}, int_T), 0), 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

441 
((@{const_name plus_class.plus}, int_T > int_T > int_T), 0), 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

442 
((@{const_name minus_class.minus}, int_T > int_T > int_T), 0), 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

443 
((@{const_name times_class.times}, int_T > int_T > int_T), 0), 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

444 
((@{const_name div_class.div}, int_T > int_T > int_T), 0), 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

445 
((@{const_name uminus_class.uminus}, int_T > int_T), 0), 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

446 
((@{const_name ord_class.less}, int_T > int_T > bool_T), 2), 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

447 
((@{const_name ord_class.less_eq}, int_T > int_T > bool_T), 2)] 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

448 
val built_in_typed_nat_consts = 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

449 
[((@{const_name zero_class.zero}, nat_T), 0), 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

450 
((@{const_name one_class.one}, nat_T), 0), 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

451 
((@{const_name plus_class.plus}, nat_T > nat_T > nat_T), 0), 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

452 
((@{const_name minus_class.minus}, nat_T > nat_T > nat_T), 0), 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

453 
((@{const_name times_class.times}, nat_T > nat_T > nat_T), 0), 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

454 
((@{const_name div_class.div}, nat_T > nat_T > nat_T), 0), 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

455 
((@{const_name ord_class.less}, nat_T > nat_T > bool_T), 2), 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

456 
((@{const_name ord_class.less_eq}, nat_T > nat_T > bool_T), 2), 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

457 
((@{const_name of_nat}, nat_T > int_T), 0)] 
33192  458 
val built_in_set_consts = 
37476
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

459 
[(@{const_name ord_class.less_eq}, 2)] 
33192  460 

35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

461 
fun unarize_type @{typ "unsigned_bit word"} = nat_T 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

462 
 unarize_type @{typ "signed_bit word"} = int_T 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

463 
 unarize_type (Type (s, Ts as _ :: _)) = Type (s, map unarize_type Ts) 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

464 
 unarize_type T = T 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

465 
fun unarize_unbox_etc_type (Type (@{type_name fin_fun}, Ts)) = 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

466 
unarize_unbox_etc_type (Type (@{type_name fun}, Ts)) 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

467 
 unarize_unbox_etc_type (Type (@{type_name fun_box}, Ts)) = 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

468 
unarize_unbox_etc_type (Type (@{type_name fun}, Ts)) 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

469 
 unarize_unbox_etc_type (Type (@{type_name pair_box}, Ts)) = 
38190
b02e204b613a
get rid of all "optimizations" regarding "unit" and other cardinality1 types
blanchet
parents:
38188
diff
changeset

470 
Type (@{type_name prod}, map unarize_unbox_etc_type Ts) 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

471 
 unarize_unbox_etc_type @{typ "unsigned_bit word"} = nat_T 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

472 
 unarize_unbox_etc_type @{typ "signed_bit word"} = int_T 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

473 
 unarize_unbox_etc_type (Type (s, Ts as _ :: _)) = 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

474 
Type (s, map unarize_unbox_etc_type Ts) 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

475 
 unarize_unbox_etc_type T = T 
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset

476 
fun uniterize_type (Type (s, Ts as _ :: _)) = Type (s, map uniterize_type Ts) 
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset

477 
 uniterize_type @{typ bisim_iterator} = nat_T 
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset

478 
 uniterize_type T = T 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

479 
val uniterize_unarize_unbox_etc_type = uniterize_type o unarize_unbox_etc_type 
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset

480 

35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

481 
fun string_for_type ctxt = Syntax.string_of_typ ctxt o unarize_unbox_etc_type 
38188  482 
fun pretty_for_type ctxt = Syntax.pretty_typ ctxt o unarize_unbox_etc_type 
33192  483 

484 
val prefix_name = Long_Name.qualify o Long_Name.base_name 

34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

485 
fun shortest_name s = List.last (space_explode "." s) handle List.Empty => "" 
33192  486 
val prefix_abs_vars = Term.map_abs_vars o prefix_name 
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

487 
fun short_name s = 
33192  488 
case space_explode name_sep s of 
489 
[_] => s > String.isPrefix nitpick_prefix s ? unprefix nitpick_prefix 

34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

490 
 ss => map shortest_name ss > space_implode "_" 
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

491 
fun shorten_names_in_type (Type (s, Ts)) = 
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

492 
Type (short_name s, map shorten_names_in_type Ts) 
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

493 
 shorten_names_in_type T = T 
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

494 
val shorten_names_in_term = 
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

495 
map_aterms (fn Const (s, T) => Const (short_name s, T)  t => t) 
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

496 
#> map_types shorten_names_in_type 
33192  497 

35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

498 
fun strict_type_match thy (T1, T2) = 
33192  499 
(Sign.typ_match thy (T2, T1) Vartab.empty; true) 
500 
handle Type.TYPE_MATCH => false 

35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

501 
fun type_match thy = strict_type_match thy o pairself unarize_unbox_etc_type 
33192  502 
fun const_match thy ((s1, T1), (s2, T2)) = 
503 
s1 = s2 andalso type_match thy (T1, T2) 

504 
fun term_match thy (Const x1, Const x2) = const_match thy (x1, x2) 

505 
 term_match thy (Free (s1, T1), Free (s2, T2)) = 

34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

506 
const_match thy ((shortest_name s1, T1), (shortest_name s2, T2)) 
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset

507 
 term_match _ (t1, t2) = t1 aconv t2 
33192  508 

35711
548d3f16404b
added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents:
35671
diff
changeset

509 
fun frac_from_term_pair T t1 t2 = 
548d3f16404b
added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents:
35671
diff
changeset

510 
case snd (HOLogic.dest_number t1) of 
548d3f16404b
added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents:
35671
diff
changeset

511 
0 => HOLogic.mk_number T 0 
548d3f16404b
added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents:
35671
diff
changeset

512 
 n1 => case snd (HOLogic.dest_number t2) of 
548d3f16404b
added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents:
35671
diff
changeset

513 
1 => HOLogic.mk_number T n1 
548d3f16404b
added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents:
35671
diff
changeset

514 
 n2 => Const (@{const_name divide}, T > T > T) 
548d3f16404b
added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents:
35671
diff
changeset

515 
$ HOLogic.mk_number T n1 $ HOLogic.mk_number T n2 
548d3f16404b
added term postprocessor to Nitpick, to provide custom syntax for typedefs
blanchet
parents:
35671
diff
changeset

516 

33192  517 
fun is_TFree (TFree _) = true 
518 
 is_TFree _ = false 

35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

519 
fun is_higher_order_type (Type (@{type_name fun}, _)) = true 
33192  520 
 is_higher_order_type (Type (_, Ts)) = exists is_higher_order_type Ts 
521 
 is_higher_order_type _ = false 

35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

522 
fun is_fun_type (Type (@{type_name fun}, _)) = true 
33192  523 
 is_fun_type _ = false 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

524 
fun is_set_type (Type (@{type_name fun}, [_, @{typ bool}])) = true 
33192  525 
 is_set_type _ = false 
38190
b02e204b613a
get rid of all "optimizations" regarding "unit" and other cardinality1 types
blanchet
parents:
38188
diff
changeset

526 
fun is_pair_type (Type (@{type_name prod}, _)) = true 
33192  527 
 is_pair_type _ = false 
528 
fun is_lfp_iterator_type (Type (s, _)) = String.isPrefix lfp_iterator_prefix s 

529 
 is_lfp_iterator_type _ = false 

530 
fun is_gfp_iterator_type (Type (s, _)) = String.isPrefix gfp_iterator_prefix s 

531 
 is_gfp_iterator_type _ = false 

532 
val is_fp_iterator_type = is_lfp_iterator_type orf is_gfp_iterator_type 

35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset

533 
fun is_iterator_type T = 
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset

534 
(T = @{typ bisim_iterator} orelse is_fp_iterator_type T) 
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

535 
fun is_boolean_type T = (T = prop_T orelse T = bool_T) 
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

536 
fun is_integer_type T = (T = nat_T orelse T = int_T) 
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset

537 
fun is_bit_type T = (T = @{typ unsigned_bit} orelse T = @{typ signed_bit}) 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset

538 
fun is_word_type (Type (@{type_name word}, _)) = true 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset

539 
 is_word_type _ = false 
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset

540 
val is_integer_like_type = is_iterator_type orf is_integer_type orf is_word_type 
33192  541 
val is_record_type = not o null o Record.dest_recTs 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

542 
fun is_frac_type ctxt (Type (s, [])) = 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

543 
s > AList.lookup (op =) (#frac_types (Data.get (Context.Proof ctxt))) 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

544 
> these > null > not 
33192  545 
 is_frac_type _ _ = false 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

546 
fun is_number_type ctxt = is_integer_like_type orf is_frac_type ctxt 
33192  547 

548 
fun iterator_type_for_const gfp (s, T) = 

549 
Type ((if gfp then gfp_iterator_prefix else lfp_iterator_prefix) ^ s, 

550 
binder_types T) 

35718  551 
fun const_for_iterator_type (Type (s, Ts)) = 
552 
(strip_first_name_sep s > snd, Ts > bool_T) 

33192  553 
 const_for_iterator_type T = 
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset

554 
raise TYPE ("Nitpick_HOL.const_for_iterator_type", [T], []) 
33192  555 

556 
fun strip_n_binders 0 T = ([], T) 

35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

557 
 strip_n_binders n (Type (@{type_name fun}, [T1, T2])) = 
33192  558 
strip_n_binders (n  1) T2 >> cons T1 
559 
 strip_n_binders n (Type (@{type_name fun_box}, Ts)) = 

35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

560 
strip_n_binders n (Type (@{type_name fun}, Ts)) 
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset

561 
 strip_n_binders _ T = raise TYPE ("Nitpick_HOL.strip_n_binders", [T], []) 
33192  562 
val nth_range_type = snd oo strip_n_binders 
563 

38190
b02e204b613a
get rid of all "optimizations" regarding "unit" and other cardinality1 types
blanchet
parents:
38188
diff
changeset

564 
fun num_factors_in_type (Type (@{type_name prod}, [T1, T2])) = 
33192  565 
fold (Integer.add o num_factors_in_type) [T1, T2] 0 
566 
 num_factors_in_type _ = 1 

35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

567 
fun num_binder_types (Type (@{type_name fun}, [_, T2])) = 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

568 
1 + num_binder_types T2 
33192  569 
 num_binder_types _ = 0 
570 
val curried_binder_types = maps HOLogic.flatten_tupleT o binder_types 

571 
fun maybe_curried_binder_types T = 

572 
(if is_pair_type (body_type T) then binder_types else curried_binder_types) T 

573 

574 
fun mk_flat_tuple _ [t] = t 

38190
b02e204b613a
get rid of all "optimizations" regarding "unit" and other cardinality1 types
blanchet
parents:
38188
diff
changeset

575 
 mk_flat_tuple (Type (@{type_name prod}, [T1, T2])) (t :: ts) = 
33192  576 
HOLogic.pair_const T1 T2 $ t $ (mk_flat_tuple T2 ts) 
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset

577 
 mk_flat_tuple T ts = raise TYPE ("Nitpick_HOL.mk_flat_tuple", [T], ts) 
33192  578 
fun dest_n_tuple 1 t = [t] 
579 
 dest_n_tuple n t = HOLogic.dest_prod t > dest_n_tuple (n  1) > op :: 

580 

35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

581 
type typedef_info = 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

582 
{rep_type: typ, abs_type: typ, Rep_name: string, Abs_name: string, 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

583 
set_def: thm option, prop_of_Rep: thm, set_name: string, 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

584 
Abs_inverse: thm option, Rep_inverse: thm option} 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

585 

37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

586 
fun typedef_info ctxt s = 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

587 
if is_frac_type ctxt (Type (s, [])) then 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

588 
SOME {abs_type = Type (s, []), rep_type = @{typ "int * int"}, 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

589 
Abs_name = @{const_name Abs_Frac}, Rep_name = @{const_name Rep_Frac}, 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

590 
set_def = NONE, prop_of_Rep = @{prop "Rep_Frac x \<in> Frac"} 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

591 
> Logic.varify_global, 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

592 
set_name = @{const_name Frac}, Abs_inverse = NONE, Rep_inverse = NONE} 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

593 
else case Typedef.get_info ctxt s of 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

594 
(* When several entries are returned, it shouldn't matter much which one 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

595 
we take (according to Florian Haftmann). *) 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

596 
({abs_type, rep_type, Abs_name, Rep_name, ...}, 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

597 
{set_def, Rep, Abs_inverse, Rep_inverse, ...}) :: _ => 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

598 
SOME {abs_type = abs_type, rep_type = rep_type, Abs_name = Abs_name, 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

599 
Rep_name = Rep_name, set_def = set_def, prop_of_Rep = prop_of Rep, 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

600 
set_name = set_prefix ^ s, Abs_inverse = SOME Abs_inverse, 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

601 
Rep_inverse = SOME Rep_inverse} 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

602 
 _ => NONE 
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

603 

2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

604 
val is_typedef = is_some oo typedef_info 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

605 
val is_real_datatype = is_some oo Datatype.get_info 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

606 
fun is_standard_datatype thy = the oo triple_lookup (type_match thy) 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

607 

34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

608 
(* FIXME: Use antiquotation for "code_numeral" below or detect "rep_datatype", 
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

609 
e.g., by adding a field to "Datatype_Aux.info". *) 
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

610 
fun is_basic_datatype thy stds s = 
38190
b02e204b613a
get rid of all "optimizations" regarding "unit" and other cardinality1 types
blanchet
parents:
38188
diff
changeset

611 
member (op =) [@{type_name prod}, @{type_name bool}, @{type_name int}, 
b02e204b613a
get rid of all "optimizations" regarding "unit" and other cardinality1 types
blanchet
parents:
38188
diff
changeset

612 
"Code_Numeral.code_numeral"] s orelse 
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

613 
(s = @{type_name nat} andalso is_standard_datatype thy stds nat_T) 
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

614 

38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

615 
(* TODO: use "Term_Subst.instantiateT" instead? *) 
33192  616 
fun instantiate_type thy T1 T1' T2 = 
617 
Same.commit (Envir.subst_type_same 

35311  618 
(Sign.typ_match thy (T1, T1') Vartab.empty)) T2 
33192  619 
handle Type.TYPE_MATCH => 
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset

620 
raise TYPE ("Nitpick_HOL.instantiate_type", [T1, T1'], []) 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

621 
fun varify_and_instantiate_type ctxt T1 T1' T2 = 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

622 
let val thy = ProofContext.theory_of ctxt in 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

623 
instantiate_type thy (varify_type ctxt T1) T1' (varify_type ctxt T2) 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

624 
end 
33192  625 

38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

626 
fun repair_constr_type ctxt body_T' T = 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

627 
varify_and_instantiate_type ctxt (body_type T) body_T' T 
33192  628 

38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

629 
fun register_frac_type_generic frac_s ersaetze generic = 
33192  630 
let 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

631 
val {frac_types, codatatypes} = Data.get generic 
33192  632 
val frac_types = AList.update (op =) (frac_s, ersaetze) frac_types 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

633 
in Data.put {frac_types = frac_types, codatatypes = codatatypes} generic end 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

634 
val register_frac_type = Context.proof_map oo register_frac_type_generic 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

635 
val register_frac_type_global = Context.theory_map oo register_frac_type_generic 
33192  636 

38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

637 
fun unregister_frac_type_generic frac_s = register_frac_type_generic frac_s [] 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

638 
val unregister_frac_type = Context.proof_map o unregister_frac_type_generic 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

639 
val unregister_frac_type_global = 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

640 
Context.theory_map o unregister_frac_type_generic 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

641 

a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

642 
fun register_codatatype_generic co_T case_name constr_xs generic = 
33192  643 
let 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

644 
val ctxt = Context.proof_of generic 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

645 
val thy = Context.theory_of generic 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

646 
val {frac_types, codatatypes} = Data.get generic 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

647 
val constr_xs = map (apsnd (repair_constr_type ctxt co_T)) constr_xs 
33192  648 
val (co_s, co_Ts) = dest_Type co_T 
649 
val _ = 

34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34126
diff
changeset

650 
if forall is_TFree co_Ts andalso not (has_duplicates (op =) co_Ts) andalso 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

651 
co_s <> @{type_name fun} andalso 
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

652 
not (is_basic_datatype thy [(NONE, true)] co_s) then 
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

653 
() 
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

654 
else 
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

655 
raise TYPE ("Nitpick_HOL.register_codatatype", [co_T], []) 
33192  656 
val codatatypes = AList.update (op =) (co_s, (case_name, constr_xs)) 
657 
codatatypes 

38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

658 
in Data.put {frac_types = frac_types, codatatypes = codatatypes} generic end 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

659 
val register_codatatype = Context.proof_map ooo register_codatatype_generic 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

660 
val register_codatatype_global = 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

661 
Context.theory_map ooo register_codatatype_generic 
33192  662 

38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

663 
fun unregister_codatatype_generic co_T = register_codatatype_generic co_T "" [] 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

664 
val unregister_codatatype = Context.proof_map o unregister_codatatype_generic 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

665 
val unregister_codatatype_global = 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

666 
Context.theory_map o unregister_codatatype_generic 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

667 

a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

668 
fun is_codatatype ctxt (Type (s, _)) = 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

669 
s > AList.lookup (op =) (#codatatypes (Data.get (Context.Proof ctxt))) 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

670 
> Option.map snd > these > null > not 
33192  671 
 is_codatatype _ _ = false 
38215
1c7d7eaebdf2
quotient types registered as codatatypes are no longer quotient types
blanchet
parents:
38212
diff
changeset

672 
fun is_real_quot_type thy (Type (s, _)) = 
1c7d7eaebdf2
quotient types registered as codatatypes are no longer quotient types
blanchet
parents:
38212
diff
changeset

673 
is_some (Quotient_Info.quotdata_lookup_raw thy s) 
1c7d7eaebdf2
quotient types registered as codatatypes are no longer quotient types
blanchet
parents:
38212
diff
changeset

674 
 is_real_quot_type _ _ = false 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

675 
fun is_quot_type ctxt T = 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

676 
let val thy = ProofContext.theory_of ctxt in 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

677 
is_real_quot_type thy T andalso not (is_codatatype ctxt T) 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

678 
end 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

679 
fun is_pure_typedef ctxt (T as Type (s, _)) = 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

680 
let val thy = ProofContext.theory_of ctxt in 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

681 
is_typedef ctxt s andalso 
38215
1c7d7eaebdf2
quotient types registered as codatatypes are no longer quotient types
blanchet
parents:
38212
diff
changeset

682 
not (is_real_datatype thy s orelse is_real_quot_type thy T orelse 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

683 
is_codatatype ctxt T orelse is_record_type T orelse 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

684 
is_integer_like_type T) 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

685 
end 
33192  686 
 is_pure_typedef _ _ = false 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

687 
fun is_univ_typedef ctxt (Type (s, _)) = 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

688 
(case typedef_info ctxt s of 
33192  689 
SOME {set_def, prop_of_Rep, ...} => 
35332
22442ab3e7a3
optimized multisets in Nitpick by fishing "finite"
blanchet
parents:
35311
diff
changeset

690 
let 
22442ab3e7a3
optimized multisets in Nitpick by fishing "finite"
blanchet
parents:
35311
diff
changeset

691 
val t_opt = 
22442ab3e7a3
optimized multisets in Nitpick by fishing "finite"
blanchet
parents:
35311
diff
changeset

692 
case set_def of 
22442ab3e7a3
optimized multisets in Nitpick by fishing "finite"
blanchet
parents:
35311
diff
changeset

693 
SOME thm => try (snd o Logic.dest_equals o prop_of) thm 
22442ab3e7a3
optimized multisets in Nitpick by fishing "finite"
blanchet
parents:
35311
diff
changeset

694 
 NONE => try (snd o HOLogic.dest_mem o HOLogic.dest_Trueprop) 
22442ab3e7a3
optimized multisets in Nitpick by fishing "finite"
blanchet
parents:
35311
diff
changeset

695 
prop_of_Rep 
22442ab3e7a3
optimized multisets in Nitpick by fishing "finite"
blanchet
parents:
35311
diff
changeset

696 
in 
22442ab3e7a3
optimized multisets in Nitpick by fishing "finite"
blanchet
parents:
35311
diff
changeset

697 
case t_opt of 
22442ab3e7a3
optimized multisets in Nitpick by fishing "finite"
blanchet
parents:
35311
diff
changeset

698 
SOME (Const (@{const_name top}, _)) => true 
35386
45a4e19d3ebd
more work on the new monotonicity stuff in Nitpick
blanchet
parents:
35385
diff
changeset

699 
(* "Multiset.multiset" *) 
35332
22442ab3e7a3
optimized multisets in Nitpick by fishing "finite"
blanchet
parents:
35311
diff
changeset

700 
 SOME (Const (@{const_name Collect}, _) 
22442ab3e7a3
optimized multisets in Nitpick by fishing "finite"
blanchet
parents:
35311
diff
changeset

701 
$ Abs (_, _, Const (@{const_name finite}, _) $ _)) => true 
35386
45a4e19d3ebd
more work on the new monotonicity stuff in Nitpick
blanchet
parents:
35385
diff
changeset

702 
(* "FinFun.finfun" *) 
45a4e19d3ebd
more work on the new monotonicity stuff in Nitpick
blanchet
parents:
35385
diff
changeset

703 
 SOME (Const (@{const_name Collect}, _) $ Abs (_, _, 
45a4e19d3ebd
more work on the new monotonicity stuff in Nitpick
blanchet
parents:
35385
diff
changeset

704 
Const (@{const_name Ex}, _) $ Abs (_, _, 
45a4e19d3ebd
more work on the new monotonicity stuff in Nitpick
blanchet
parents:
35385
diff
changeset

705 
Const (@{const_name finite}, _) $ _))) => true 
35332
22442ab3e7a3
optimized multisets in Nitpick by fishing "finite"
blanchet
parents:
35311
diff
changeset

706 
 _ => false 
22442ab3e7a3
optimized multisets in Nitpick by fishing "finite"
blanchet
parents:
35311
diff
changeset

707 
end 
33192  708 
 NONE => false) 
709 
 is_univ_typedef _ _ = false 

37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

710 
fun is_datatype ctxt stds (T as Type (s, _)) = 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

711 
let val thy = ProofContext.theory_of ctxt in 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

712 
(is_typedef ctxt s orelse is_codatatype ctxt T orelse 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

713 
T = @{typ ind} orelse is_real_quot_type thy T) andalso 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

714 
not (is_basic_datatype thy stds s) 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

715 
end 
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

716 
 is_datatype _ _ _ = false 
33192  717 

718 
fun all_record_fields thy T = 

719 
let val (recs, more) = Record.get_extT_fields thy T in 

720 
recs @ more :: all_record_fields thy (snd more) 

721 
end 

722 
handle TYPE _ => [] 

35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset

723 
fun is_record_constr (s, T) = 
33192  724 
String.isSuffix Record.extN s andalso 
725 
let val dataT = body_type T in 

726 
is_record_type dataT andalso 

727 
s = unsuffix Record.ext_typeN (fst (dest_Type dataT)) ^ Record.extN 

728 
end 

729 
val num_record_fields = Integer.add 1 o length o fst oo Record.get_extT_fields 

730 
fun no_of_record_field thy s T1 = 

34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

731 
find_index (curry (op =) s o fst) 
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

732 
(Record.get_extT_fields thy T1 > single > op @) 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

733 
fun is_record_get thy (s, Type (@{type_name fun}, [T1, _])) = 
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

734 
exists (curry (op =) s o fst) (all_record_fields thy T1) 
33192  735 
 is_record_get _ _ = false 
736 
fun is_record_update thy (s, T) = 

737 
String.isSuffix Record.updateN s andalso 

34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

738 
exists (curry (op =) (unsuffix Record.updateN s) o fst) 
33192  739 
(all_record_fields thy (body_type T)) 
740 
handle TYPE _ => false 

37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

741 
fun is_abs_fun ctxt (s, Type (@{type_name fun}, [_, Type (s', _)])) = 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

742 
(case typedef_info ctxt s' of 
33192  743 
SOME {Abs_name, ...} => s = Abs_name 
744 
 NONE => false) 

745 
 is_abs_fun _ _ = false 

37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

746 
fun is_rep_fun ctxt (s, Type (@{type_name fun}, [Type (s', _), _])) = 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

747 
(case typedef_info ctxt s' of 
33192  748 
SOME {Rep_name, ...} => s = Rep_name 
749 
 NONE => false) 

750 
 is_rep_fun _ _ = false 

38215
1c7d7eaebdf2
quotient types registered as codatatypes are no longer quotient types
blanchet
parents:
38212
diff
changeset

751 
fun is_quot_abs_fun ctxt (x as (_, Type (@{type_name fun}, 
1c7d7eaebdf2
quotient types registered as codatatypes are no longer quotient types
blanchet
parents:
38212
diff
changeset

752 
[_, abs_T as Type (s', _)]))) = 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

753 
try (Quotient_Term.absrep_const_chk Quotient_Term.AbsF ctxt) s' 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

754 
= SOME (Const x) andalso not (is_codatatype ctxt abs_T) 
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34126
diff
changeset

755 
 is_quot_abs_fun _ _ = false 
38215
1c7d7eaebdf2
quotient types registered as codatatypes are no longer quotient types
blanchet
parents:
38212
diff
changeset

756 
fun is_quot_rep_fun ctxt (x as (_, Type (@{type_name fun}, 
1c7d7eaebdf2
quotient types registered as codatatypes are no longer quotient types
blanchet
parents:
38212
diff
changeset

757 
[abs_T as Type (s', _), _]))) = 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

758 
try (Quotient_Term.absrep_const_chk Quotient_Term.RepF ctxt) s' 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

759 
= SOME (Const x) andalso not (is_codatatype ctxt abs_T) 
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34126
diff
changeset

760 
 is_quot_rep_fun _ _ = false 
33192  761 

37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

762 
fun mate_of_rep_fun ctxt (x as (_, Type (@{type_name fun}, 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

763 
[T1 as Type (s', _), T2]))) = 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

764 
(case typedef_info ctxt s' of 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

765 
SOME {Abs_name, ...} => (Abs_name, Type (@{type_name fun}, [T2, T1])) 
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset

766 
 NONE => raise TERM ("Nitpick_HOL.mate_of_rep_fun", [Const x])) 
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset

767 
 mate_of_rep_fun _ x = raise TERM ("Nitpick_HOL.mate_of_rep_fun", [Const x]) 
35284
9edc2bd6d2bd
enabled Nitpick's support for quotient types + shortened the Nitpick tests a bit
blanchet
parents:
35283
diff
changeset

768 
fun rep_type_for_quot_type thy (T as Type (s, _)) = 
9edc2bd6d2bd
enabled Nitpick's support for quotient types + shortened the Nitpick tests a bit
blanchet
parents:
35283
diff
changeset

769 
let val {qtyp, rtyp, ...} = Quotient_Info.quotdata_lookup thy s in 
9edc2bd6d2bd
enabled Nitpick's support for quotient types + shortened the Nitpick tests a bit
blanchet
parents:
35283
diff
changeset

770 
instantiate_type thy qtyp T rtyp 
9edc2bd6d2bd
enabled Nitpick's support for quotient types + shortened the Nitpick tests a bit
blanchet
parents:
35283
diff
changeset

771 
end 
9edc2bd6d2bd
enabled Nitpick's support for quotient types + shortened the Nitpick tests a bit
blanchet
parents:
35283
diff
changeset

772 
fun equiv_relation_for_quot_type thy (Type (s, Ts)) = 
9edc2bd6d2bd
enabled Nitpick's support for quotient types + shortened the Nitpick tests a bit
blanchet
parents:
35283
diff
changeset

773 
let 
9edc2bd6d2bd
enabled Nitpick's support for quotient types + shortened the Nitpick tests a bit
blanchet
parents:
35283
diff
changeset

774 
val {qtyp, equiv_rel, ...} = Quotient_Info.quotdata_lookup thy s 
9edc2bd6d2bd
enabled Nitpick's support for quotient types + shortened the Nitpick tests a bit
blanchet
parents:
35283
diff
changeset

775 
val Ts' = qtyp > dest_Type > snd 
9edc2bd6d2bd
enabled Nitpick's support for quotient types + shortened the Nitpick tests a bit
blanchet
parents:
35283
diff
changeset

776 
in subst_atomic_types (Ts' ~~ Ts) equiv_rel end 
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34126
diff
changeset

777 
 equiv_relation_for_quot_type _ T = 
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34126
diff
changeset

778 
raise TYPE ("Nitpick_HOL.equiv_relation_for_quot_type", [T], []) 
33192  779 

38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

780 
fun is_coconstr ctxt (s, T) = 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

781 
case body_type T of 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

782 
co_T as Type (co_s, _) => 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

783 
let val {codatatypes, ...} = Data.get (Context.Proof ctxt) in 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

784 
exists (fn (s', T') => s = s' andalso repair_constr_type ctxt co_T T' = T) 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

785 
(AList.lookup (op =) codatatypes co_s > Option.map snd > these) 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

786 
end 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

787 
 _ => false 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

788 
fun is_constr_like ctxt (s, T) = 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

789 
member (op =) [@{const_name FinFun}, @{const_name FunBox}, 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

790 
@{const_name PairBox}, @{const_name Quot}, 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

791 
@{const_name Zero_Rep}, @{const_name Suc_Rep}] s orelse 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

792 
let 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

793 
val thy = ProofContext.theory_of ctxt 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

794 
val (x as (_, T)) = (s, unarize_unbox_etc_type T) 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

795 
in 
37260
dde817e6dfb1
added "atoms" option to Nitpick (request from Karlsruhe) + wrap Refute. functions to "nitpick_util.ML"
blanchet
parents:
37258
diff
changeset

796 
is_real_constr thy x orelse is_record_constr x orelse 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

797 
(is_abs_fun ctxt x andalso is_pure_typedef ctxt (range_type T)) orelse 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

798 
is_coconstr ctxt x 
33192  799 
end 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

800 
fun is_stale_constr ctxt (x as (_, T)) = 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

801 
is_codatatype ctxt (body_type T) andalso is_constr_like ctxt x andalso 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

802 
not (is_coconstr ctxt x) 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

803 
fun is_constr ctxt stds (x as (_, T)) = 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

804 
let val thy = ProofContext.theory_of ctxt in 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

805 
is_constr_like ctxt x andalso 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

806 
not (is_basic_datatype thy stds 
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

807 
(fst (dest_Type (unarize_type (body_type T))))) andalso 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

808 
not (is_stale_constr ctxt x) 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

809 
end 
33192  810 
val is_sel = String.isPrefix discr_prefix orf String.isPrefix sel_prefix 
811 
val is_sel_like_and_no_discr = 

35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

812 
String.isPrefix sel_prefix orf 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

813 
(member (op =) [@{const_name fst}, @{const_name snd}]) 
33192  814 

815 
fun in_fun_lhs_for InConstr = InSel 

816 
 in_fun_lhs_for _ = InFunLHS 

817 
fun in_fun_rhs_for InConstr = InConstr 

818 
 in_fun_rhs_for InSel = InSel 

819 
 in_fun_rhs_for InFunRHS1 = InFunRHS2 

820 
 in_fun_rhs_for _ = InFunRHS1 

821 

35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

822 
fun is_boxing_worth_it (hol_ctxt : hol_context) boxy T = 
33192  823 
case T of 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

824 
Type (@{type_name fun}, _) => 
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34126
diff
changeset

825 
(boxy = InPair orelse boxy = InFunLHS) andalso 
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34126
diff
changeset

826 
not (is_boolean_type (body_type T)) 
38190
b02e204b613a
get rid of all "optimizations" regarding "unit" and other cardinality1 types
blanchet
parents:
38188
diff
changeset

827 
 Type (@{type_name prod}, Ts) => 
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34126
diff
changeset

828 
boxy = InPair orelse boxy = InFunRHS1 orelse boxy = InFunRHS2 orelse 
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34126
diff
changeset

829 
((boxy = InExpr orelse boxy = InFunLHS) andalso 
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

830 
exists (is_boxing_worth_it hol_ctxt InPair) 
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

831 
(map (box_type hol_ctxt InPair) Ts)) 
33192  832 
 _ => false 
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset

833 
and should_box_type (hol_ctxt as {thy, boxes, ...}) boxy z = 
33192  834 
case triple_lookup (type_match thy) boxes (Type z) of 
835 
SOME (SOME box_me) => box_me 

35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

836 
 _ => is_boxing_worth_it hol_ctxt boxy (Type z) 
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

837 
and box_type hol_ctxt boxy T = 
33192  838 
case T of 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

839 
Type (z as (@{type_name fun}, [T1, T2])) => 
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34126
diff
changeset

840 
if boxy <> InConstr andalso boxy <> InSel andalso 
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

841 
should_box_type hol_ctxt boxy z then 
33192  842 
Type (@{type_name fun_box}, 
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

843 
[box_type hol_ctxt InFunLHS T1, box_type hol_ctxt InFunRHS1 T2]) 
33192  844 
else 
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

845 
box_type hol_ctxt (in_fun_lhs_for boxy) T1 
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

846 
> box_type hol_ctxt (in_fun_rhs_for boxy) T2 
38190
b02e204b613a
get rid of all "optimizations" regarding "unit" and other cardinality1 types
blanchet
parents:
38188
diff
changeset

847 
 Type (z as (@{type_name prod}, Ts)) => 
34982
7b8c366e34a2
added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents:
34936
diff
changeset

848 
if boxy <> InConstr andalso boxy <> InSel 
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

849 
andalso should_box_type hol_ctxt boxy z then 
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

850 
Type (@{type_name pair_box}, map (box_type hol_ctxt InSel) Ts) 
33192  851 
else 
38190
b02e204b613a
get rid of all "optimizations" regarding "unit" and other cardinality1 types
blanchet
parents:
38188
diff
changeset

852 
Type (@{type_name prod}, 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

853 
map (box_type hol_ctxt 
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

854 
(if boxy = InConstr orelse boxy = InSel then boxy 
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

855 
else InPair)) Ts) 
33192  856 
 _ => T 
857 

35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

858 
fun binarize_nat_and_int_in_type @{typ nat} = @{typ "unsigned_bit word"} 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

859 
 binarize_nat_and_int_in_type @{typ int} = @{typ "signed_bit word"} 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

860 
 binarize_nat_and_int_in_type (Type (s, Ts)) = 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

861 
Type (s, map binarize_nat_and_int_in_type Ts) 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

862 
 binarize_nat_and_int_in_type T = T 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

863 
val binarize_nat_and_int_in_term = map_types binarize_nat_and_int_in_type 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

864 

33192  865 
fun discr_for_constr (s, T) = (discr_prefix ^ s, body_type T > bool_T) 
866 

867 
fun num_sels_for_constr_type T = length (maybe_curried_binder_types T) 

868 
fun nth_sel_name_for_constr_name s n = 

869 
if s = @{const_name Pair} then 

870 
if n = 0 then @{const_name fst} else @{const_name snd} 

871 
else 

872 
sel_prefix_for n ^ s 

873 
fun nth_sel_for_constr x ~1 = discr_for_constr x 

874 
 nth_sel_for_constr (s, T) n = 

875 
(nth_sel_name_for_constr_name s n, 

876 
body_type T > nth (maybe_curried_binder_types T) n) 

35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

877 
fun binarized_and_boxed_nth_sel_for_constr hol_ctxt binarize = 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

878 
apsnd ((binarize ? binarize_nat_and_int_in_type) o box_type hol_ctxt InSel) 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

879 
oo nth_sel_for_constr 
33192  880 

881 
fun sel_no_from_name s = 

882 
if String.isPrefix discr_prefix s then 

883 
~1 

884 
else if String.isPrefix sel_prefix s then 

885 
s > unprefix sel_prefix > Int.fromString > the 

886 
else if s = @{const_name snd} then 

887 
1 

888 
else 

889 
0 

890 

35078
6fd1052fe463
optimization to quantifiers in Nitpick's handling of simp rules + renamed some SAT solvers
blanchet
parents:
35075
diff
changeset

891 
val close_form = 
6fd1052fe463
optimization to quantifiers in Nitpick's handling of simp rules + renamed some SAT solvers
blanchet
parents:
35075
diff
changeset

892 
let 
6fd1052fe463
optimization to quantifiers in Nitpick's handling of simp rules + renamed some SAT solvers
blanchet
parents:
35075
diff
changeset

893 
fun close_up zs zs' = 
6fd1052fe463
optimization to quantifiers in Nitpick's handling of simp rules + renamed some SAT solvers
blanchet
parents:
35075
diff
changeset

894 
fold (fn (z as ((s, _), T)) => fn t' => 
6fd1052fe463
optimization to quantifiers in Nitpick's handling of simp rules + renamed some SAT solvers
blanchet
parents:
35075
diff
changeset

895 
Term.all T $ Abs (s, T, abstract_over (Var z, t'))) 
6fd1052fe463
optimization to quantifiers in Nitpick's handling of simp rules + renamed some SAT solvers
blanchet
parents:
35075
diff
changeset

896 
(take (length zs'  length zs) zs') 
6fd1052fe463
optimization to quantifiers in Nitpick's handling of simp rules + renamed some SAT solvers
blanchet
parents:
35075
diff
changeset

897 
fun aux zs (@{const "==>"} $ t1 $ t2) = 
6fd1052fe463
optimization to quantifiers in Nitpick's handling of simp rules + renamed some SAT solvers
blanchet
parents:
35075
diff
changeset

898 
let val zs' = Term.add_vars t1 zs in 
6fd1052fe463
optimization to quantifiers in Nitpick's handling of simp rules + renamed some SAT solvers
blanchet
parents:
35075
diff
changeset

899 
close_up zs zs' (Logic.mk_implies (t1, aux zs' t2)) 
6fd1052fe463
optimization to quantifiers in Nitpick's handling of simp rules + renamed some SAT solvers
blanchet
parents:
35075
diff
changeset

900 
end 
6fd1052fe463
optimization to quantifiers in Nitpick's handling of simp rules + renamed some SAT solvers
blanchet
parents:
35075
diff
changeset

901 
 aux zs t = close_up zs (Term.add_vars t zs) t 
6fd1052fe463
optimization to quantifiers in Nitpick's handling of simp rules + renamed some SAT solvers
blanchet
parents:
35075
diff
changeset

902 
in aux [] end 
6fd1052fe463
optimization to quantifiers in Nitpick's handling of simp rules + renamed some SAT solvers
blanchet
parents:
35075
diff
changeset

903 

33192  904 
fun eta_expand _ t 0 = t 
905 
 eta_expand Ts (Abs (s, T, t')) n = 

906 
Abs (s, T, eta_expand (T :: Ts) t' (n  1)) 

907 
 eta_expand Ts t n = 

37261  908 
fold_rev (curry3 Abs ("x" ^ nat_subscript n)) 
33192  909 
(List.take (binder_types (fastype_of1 (Ts, t)), n)) 
910 
(list_comb (incr_boundvars n t, map Bound (n  1 downto 0))) 

911 

912 
fun distinctness_formula T = 

913 
all_distinct_unordered_pairs_of 

914 
#> map (fn (t1, t2) => @{const Not} $ (HOLogic.eq_const T $ t1 $ t2)) 

915 
#> List.foldr (s_conj o swap) @{const True} 

916 

35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

917 
fun zero_const T = Const (@{const_name zero_class.zero}, T) 
33192  918 
fun suc_const T = Const (@{const_name Suc}, T > T) 
919 

37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

920 
fun uncached_datatype_constrs ({thy, ctxt, stds, ...} : hol_context) 
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

921 
(T as Type (s, Ts)) = 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

922 
(case AList.lookup (op =) (#codatatypes (Data.get (Context.Proof ctxt))) 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

923 
s of 
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

924 
SOME (_, xs' as (_ :: _)) => map (apsnd (repair_constr_type ctxt T)) xs' 
33581
e1e77265fb1d
added possibility to register datatypes as codatatypes in Nitpick;
blanchet
parents:
33580
diff
changeset

925 
 _ => 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

926 
if is_datatype ctxt stds T then 
33581
e1e77265fb1d
added possibility to register datatypes as codatatypes in Nitpick;
blanchet
parents:
33580
diff
changeset

927 
case Datatype.get_info thy s of 
e1e77265fb1d
added possibility to register datatypes as codatatypes in Nitpick;
blanchet
parents:
33580
diff
changeset

928 
SOME {index, descr, ...} => 
e1e77265fb1d
added possibility to register datatypes as codatatypes in Nitpick;
blanchet
parents:
33580
diff
changeset

929 
let 
e1e77265fb1d
added possibility to register datatypes as codatatypes in Nitpick;
blanchet
parents:
33580
diff
changeset

930 
val (_, dtyps, constrs) = AList.lookup (op =) descr index > the 
e1e77265fb1d
added possibility to register datatypes as codatatypes in Nitpick;
blanchet
parents:
33580
diff
changeset

931 
in 
37260
dde817e6dfb1
added "atoms" option to Nitpick (request from Karlsruhe) + wrap Refute. functions to "nitpick_util.ML"
blanchet
parents:
37258
diff
changeset

932 
map (apsnd (fn Us => 
dde817e6dfb1
added "atoms" option to Nitpick (request from Karlsruhe) + wrap Refute. functions to "nitpick_util.ML"
blanchet
parents:
37258
diff
changeset

933 
map (typ_of_dtyp descr (dtyps ~~ Ts)) Us > T)) 
dde817e6dfb1
added "atoms" option to Nitpick (request from Karlsruhe) + wrap Refute. functions to "nitpick_util.ML"
blanchet
parents:
37258
diff
changeset

934 
constrs 
33581
e1e77265fb1d
added possibility to register datatypes as codatatypes in Nitpick;
blanchet
parents:
33580
diff
changeset

935 
end 
e1e77265fb1d
added possibility to register datatypes as codatatypes in Nitpick;
blanchet
parents:
33580
diff
changeset

936 
 NONE => 
e1e77265fb1d
added possibility to register datatypes as codatatypes in Nitpick;
blanchet
parents:
33580
diff
changeset

937 
if is_record_type T then 
e1e77265fb1d
added possibility to register datatypes as codatatypes in Nitpick;
blanchet
parents:
33580
diff
changeset

938 
let 
e1e77265fb1d
added possibility to register datatypes as codatatypes in Nitpick;
blanchet
parents:
33580
diff
changeset

939 
val s' = unsuffix Record.ext_typeN s ^ Record.extN 
e1e77265fb1d
added possibility to register datatypes as codatatypes in Nitpick;
blanchet
parents:
33580
diff
changeset

940 
val T' = (Record.get_extT_fields thy T 
e1e77265fb1d
added possibility to register datatypes as codatatypes in Nitpick;
blanchet
parents:
33580
diff
changeset

941 
> apsnd single > uncurry append > map snd) > T 
e1e77265fb1d
added possibility to register datatypes as codatatypes in Nitpick;
blanchet
parents:
33580
diff
changeset

942 
in [(s', T')] end 
38215
1c7d7eaebdf2
quotient types registered as codatatypes are no longer quotient types
blanchet
parents:
38212
diff
changeset

943 
else if is_real_quot_type thy T then 
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34126
diff
changeset

944 
[(@{const_name Quot}, rep_type_for_quot_type thy T > T)] 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

945 
else case typedef_info ctxt s of 
33581
e1e77265fb1d
added possibility to register datatypes as codatatypes in Nitpick;
blanchet
parents:
33580
diff
changeset

946 
SOME {abs_type, rep_type, Abs_name, ...} => 
35311  947 
[(Abs_name, 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

948 
varify_and_instantiate_type ctxt abs_type T rep_type > T)] 
33581
e1e77265fb1d
added possibility to register datatypes as codatatypes in Nitpick;
blanchet
parents:
33580
diff
changeset

949 
 NONE => 
e1e77265fb1d
added possibility to register datatypes as codatatypes in Nitpick;
blanchet
parents:
33580
diff
changeset

950 
if T = @{typ ind} then 
e1e77265fb1d
added possibility to register datatypes as codatatypes in Nitpick;
blanchet
parents:
33580
diff
changeset

951 
[dest_Const @{const Zero_Rep}, dest_Const @{const Suc_Rep}] 
e1e77265fb1d
added possibility to register datatypes as codatatypes in Nitpick;
blanchet
parents:
33580
diff
changeset

952 
else 
e1e77265fb1d
added possibility to register datatypes as codatatypes in Nitpick;
blanchet
parents:
33580
diff
changeset

953 
[] 
e1e77265fb1d
added possibility to register datatypes as codatatypes in Nitpick;
blanchet
parents:
33580
diff
changeset

954 
else 
e1e77265fb1d
added possibility to register datatypes as codatatypes in Nitpick;
blanchet
parents:
33580
diff
changeset

955 
[]) 
33580
45c33e97cb86
added datatype constructor cache in Nitpick (to speed up the scope enumeration) and never test more than 4096 scopes
blanchet
parents:
33578
diff
changeset

956 
 uncached_datatype_constrs _ _ = [] 
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

957 
fun datatype_constrs (hol_ctxt as {constr_cache, ...}) T = 
33580
45c33e97cb86
added datatype constructor cache in Nitpick (to speed up the scope enumeration) and never test more than 4096 scopes
blanchet
parents:
33578
diff
changeset

958 
case AList.lookup (op =) (!constr_cache) T of 
45c33e97cb86
added datatype constructor cache in Nitpick (to speed up the scope enumeration) and never test more than 4096 scopes
blanchet
parents:
33578
diff
changeset

959 
SOME xs => xs 
45c33e97cb86
added datatype constructor cache in Nitpick (to speed up the scope enumeration) and never test more than 4096 scopes
blanchet
parents:
33578
diff
changeset

960 
 NONE => 
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

961 
let val xs = uncached_datatype_constrs hol_ctxt T in 
33580
45c33e97cb86
added datatype constructor cache in Nitpick (to speed up the scope enumeration) and never test more than 4096 scopes
blanchet
parents:
33578
diff
changeset

962 
(Unsynchronized.change constr_cache (cons (T, xs)); xs) 
45c33e97cb86
added datatype constructor cache in Nitpick (to speed up the scope enumeration) and never test more than 4096 scopes
blanchet
parents:
33578
diff
changeset

963 
end 
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

964 
fun binarized_and_boxed_datatype_constrs hol_ctxt binarize = 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

965 
map (apsnd ((binarize ? binarize_nat_and_int_in_type) 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

966 
o box_type hol_ctxt InConstr)) o datatype_constrs hol_ctxt 
33192  967 
val num_datatype_constrs = length oo datatype_constrs 
968 

969 
fun constr_name_for_sel_like @{const_name fst} = @{const_name Pair} 

970 
 constr_name_for_sel_like @{const_name snd} = @{const_name Pair} 

971 
 constr_name_for_sel_like s' = original_name s' 

35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

972 
fun binarized_and_boxed_constr_for_sel hol_ctxt binarize (s', T') = 
33192  973 
let val s = constr_name_for_sel_like s' in 
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

974 
AList.lookup (op =) 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

975 
(binarized_and_boxed_datatype_constrs hol_ctxt binarize (domain_type T')) 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35181
diff
changeset

976 
s 
33192  977 
> the > pair s 
978 
end 

34982
7b8c366e34a2
added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents:
34936
diff
changeset

979 

35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

980 
fun discr_term_for_constr hol_ctxt (x as (s, T)) = 
33192  981 
let val dataT = body_type T in 
982 
if s = @{const_name Suc} then 

983 
Abs (Name.uu, dataT, 

984 
@{const Not} $ HOLogic.mk_eq (zero_const dataT, Bound 0)) 

35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

985 
else if num_datatype_constrs hol_ctxt dataT >= 2 then 
33192  986 
Const (discr_for_constr x) 
987 
else 

988 
Abs (Name.uu, dataT, @{const True}) 

989 
end 

37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

990 
fun discriminate_value (hol_ctxt as {ctxt, ...}) x t = 
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset

991 
case head_of t of 
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset

992 
Const x' => 
33192  993 
if x = x' then @{const True} 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

994 
else if is_constr_like ctxt x' then @{const False} 
37476
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

995 
else s_betapply [] (discr_term_for_constr hol_ctxt x, t) 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

996 
 _ => s_betapply [] (discr_term_for_constr hol_ctxt x, t) 
33192  997 

35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

998 
fun nth_arg_sel_term_for_constr thy stds (x as (s, T)) n = 
33192  999 
let val (arg_Ts, dataT) = strip_type T in 
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

1000 
if dataT = nat_T andalso is_standard_datatype thy stds nat_T then 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

1001 
@{term "%n::nat. n  1"} 
33192  1002 
else if is_pair_type dataT then 
1003 
Const (nth_sel_for_constr x n) 

1004 
else 

1005 
let 

38190
b02e204b613a
get rid of all "optimizations" regarding "unit" and other cardinality1 types
blanchet
parents:
38188
diff
changeset

1006 
fun aux m (Type (@{type_name prod}, [T1, T2])) = 
33192  1007 
let 
1008 
val (m, t1) = aux m T1 

1009 
val (m, t2) = aux m T2 

1010 
in (m, HOLogic.mk_prod (t1, t2)) end 

1011 
 aux m T = 

1012 
(m + 1, Const (nth_sel_name_for_constr_name s m, dataT > T) 

1013 
$ Bound 0) 

1014 
val m = fold (Integer.add o num_factors_in_type) 

1015 
(List.take (arg_Ts, n)) 0 

1016 
in Abs ("x", dataT, aux m (nth arg_Ts n) > snd) end 

1017 
end 

37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

1018 
fun select_nth_constr_arg ctxt stds x t n res_T = 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

1019 
let val thy = ProofContext.theory_of ctxt in 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

1020 
(case strip_comb t of 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

1021 
(Const x', args) => 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

1022 
if x = x' then nth args n 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

1023 
else if is_constr_like ctxt x' then Const (@{const_name unknown}, res_T) 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

1024 
else raise SAME () 
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

1025 
 _ => raise SAME()) 
37476
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

1026 
handle SAME () => 
0681e46b4022
optimized code generated for datatype cases + more;
blanchet
parents:
37269
diff
changeset

1027 
s_betapply [] (nth_arg_sel_term_for_constr thy stds x n, t) 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

1028 
end 
33192  1029 

35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

1030 
fun construct_value _ _ x [] = Const x 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

1031 
 construct_value ctxt stds (x as (s, _)) args = 
33192  1032 
let val args = map Envir.eta_contract args in 
1033 
case hd args of 

35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset

1034 
Const (s', _) $ t => 
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34126
diff
changeset

1035 
if is_sel_like_and_no_discr s' andalso 
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34126
diff
changeset

1036 
constr_name_for_sel_like s' = s andalso 
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset

1037 
forall (fn (n, t') => 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

1038 
select_nth_constr_arg ctxt stds x t n dummyT = t') 
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34126
diff
changeset

1039 
(index_seq 0 (length args) ~~ args) then 
33192  1040 
t 
1041 
else 

1042 
list_comb (Const x, args) 

1043 
 _ => list_comb (Const x, args) 

1044 
end 

1045 

37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

1046 
fun constr_expand (hol_ctxt as {ctxt, stds, ...}) T t = 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1047 
(case head_of t of 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

1048 
Const x => if is_constr_like ctxt x then t else raise SAME () 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1049 
 _ => raise SAME ()) 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1050 
handle SAME () => 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1051 
let 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1052 
val x' as (_, T') = 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1053 
if is_pair_type T then 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1054 
let val (T1, T2) = HOLogic.dest_prodT T in 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1055 
(@{const_name Pair}, T1 > T2 > T) 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1056 
end 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1057 
else 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1058 
datatype_constrs hol_ctxt T > hd 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1059 
val arg_Ts = binder_types T' 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1060 
in 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

1061 
list_comb (Const x', map2 (select_nth_constr_arg ctxt stds x' t) 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1062 
(index_seq 0 (length arg_Ts)) arg_Ts) 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1063 
end 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1064 

ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1065 
fun coerce_bound_no f j t = 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1066 
case t of 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1067 
t1 $ t2 => coerce_bound_no f j t1 $ coerce_bound_no f j t2 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1068 
 Abs (s, T, t') => Abs (s, T, coerce_bound_no f (j + 1) t') 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1069 
 Bound j' => if j' = j then f t else t 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1070 
 _ => t 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1071 
fun coerce_bound_0_in_term hol_ctxt new_T old_T = 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1072 
old_T <> new_T ? coerce_bound_no (coerce_term hol_ctxt [new_T] old_T new_T) 0 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

1073 
and coerce_term (hol_ctxt as {ctxt, stds, fast_descrs, ...}) Ts new_T old_T t = 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1074 
if old_T = new_T then 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1075 
t 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1076 
else 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1077 
case (new_T, old_T) of 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1078 
(Type (new_s, new_Ts as [new_T1, new_T2]), 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1079 
Type (@{type_name fun}, [old_T1, old_T2])) => 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1080 
(case eta_expand Ts t 1 of 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1081 
Abs (s, _, t') => 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1082 
Abs (s, new_T1, 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1083 
t' > coerce_bound_0_in_term hol_ctxt new_T1 old_T1 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1084 
> coerce_term hol_ctxt (new_T1 :: Ts) new_T2 old_T2) 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1085 
> Envir.eta_contract 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1086 
> new_s <> @{type_name fun} 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

1087 
? construct_value ctxt stds 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1088 
(if new_s = @{type_name fin_fun} then @{const_name FinFun} 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1089 
else @{const_name FunBox}, 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1090 
Type (@{type_name fun}, new_Ts) > new_T) 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1091 
o single 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1092 
 t' => raise TERM ("Nitpick_HOL.coerce_term", [t'])) 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1093 
 (Type (new_s, new_Ts as [new_T1, new_T2]), 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1094 
Type (old_s, old_Ts as [old_T1, old_T2])) => 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1095 
if old_s = @{type_name fin_fun} orelse old_s = @{type_name fun_box} orelse 
38190
b02e204b613a
get rid of all "optimizations" regarding "unit" and other cardinality1 types
blanchet
parents:
38188
diff
changeset

1096 
old_s = @{type_name pair_box} orelse old_s = @{type_name prod} then 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1097 
case constr_expand hol_ctxt old_T t of 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1098 
Const (old_s, _) $ t1 => 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1099 
if new_s = @{type_name fun} then 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1100 
coerce_term hol_ctxt Ts new_T (Type (@{type_name fun}, old_Ts)) t1 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1101 
else 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

1102 
construct_value ctxt stds 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1103 
(old_s, Type (@{type_name fun}, new_Ts) > new_T) 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1104 
[coerce_term hol_ctxt Ts (Type (@{type_name fun}, new_Ts)) 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1105 
(Type (@{type_name fun}, old_Ts)) t1] 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1106 
 Const _ $ t1 $ t2 => 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

1107 
construct_value ctxt stds 
38190
b02e204b613a
get rid of all "optimizations" regarding "unit" and other cardinality1 types
blanchet
parents:
38188
diff
changeset

1108 
(if new_s = @{type_name prod} then @{const_name Pair} 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1109 
else @{const_name PairBox}, new_Ts > new_T) 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1110 
(map3 (coerce_term hol_ctxt Ts) [new_T1, new_T2] [old_T1, old_T2] 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1111 
[t1, t2]) 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1112 
 t' => raise TERM ("Nitpick_HOL.coerce_term", [t']) 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1113 
else 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1114 
raise TYPE ("Nitpick_HOL.coerce_term", [new_T, old_T], [t]) 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1115 
 _ => raise TYPE ("Nitpick_HOL.coerce_term", [new_T, old_T], [t]) 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1116 

ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1117 
fun card_of_type assigns (Type (@{type_name fun}, [T1, T2])) = 
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset

1118 
reasonable_power (card_of_type assigns T2) (card_of_type assigns T1) 
38190
b02e204b613a
get rid of all "optimizations" regarding "unit" and other cardinality1 types
blanchet
parents:
38188
diff
changeset

1119 
 card_of_type assigns (Type (@{type_name prod}, [T1, T2])) = 
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset

1120 
card_of_type assigns T1 * card_of_type assigns T2 
33192  1121 
 card_of_type _ (Type (@{type_name itself}, _)) = 1 
1122 
 card_of_type _ @{typ prop} = 2 

1123 
 card_of_type _ @{typ bool} = 2 

34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset

1124 
 card_of_type assigns T = 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset

1125 
case AList.lookup (op =) assigns T of 
33192  1126 
SOME k => k 
1127 
 NONE => if T = @{typ bisim_iterator} then 0 

33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset

1128 
else raise TYPE ("Nitpick_HOL.card_of_type", [T], []) 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1129 
fun bounded_card_of_type max default_card assigns 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1130 
(Type (@{type_name fun}, [T1, T2])) = 
33192  1131 
let 
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset

1132 
val k1 = bounded_card_of_type max default_card assigns T1 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset

1133 
val k2 = bounded_card_of_type max default_card assigns T2 
33192  1134 
in 
1135 
if k1 = max orelse k2 = max then max 

1136 
else Int.min (max, reasonable_power k2 k1) 

1137 
end 

35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1138 
 bounded_card_of_type max default_card assigns 
38190
b02e204b613a
get rid of all "optimizations" regarding "unit" and other cardinality1 types
blanchet
parents:
38188
diff
changeset

1139 
(Type (@{type_name prod}, [T1, T2])) = 
33192  1140 
let 
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset

1141 
val k1 = bounded_card_of_type max default_card assigns T1 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset

1142 
val k2 = bounded_card_of_type max default_card assigns T2 
33192  1143 
in if k1 = max orelse k2 = max then max else Int.min (max, k1 * k2) end 
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset

1144 
 bounded_card_of_type max default_card assigns T = 
33192  1145 
Int.min (max, if default_card = ~1 then 
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset

1146 
card_of_type assigns T 
33192  1147 
else 
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset

1148 
card_of_type assigns T 
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset

1149 
handle TYPE ("Nitpick_HOL.card_of_type", _, _) => 
33192  1150 
default_card) 
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35384
diff
changeset

1151 
fun bounded_exact_card_of_type hol_ctxt finitizable_dataTs max default_card 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35384
diff
changeset

1152 
assigns T = 
33192  1153 
let 
1154 
fun aux avoid T = 

34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

1155 
(if member (op =) avoid T then 
33192  1156 
0 
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35384
diff
changeset

1157 
else if member (op =) finitizable_dataTs T then 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35384
diff
changeset

1158 
raise SAME () 
33192  1159 
else case T of 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1160 
Type (@{type_name fun}, [T1, T2]) => 
33192  1161 
let 
1162 
val k1 = aux avoid T1 

1163 
val k2 = aux avoid T2 

1164 
in 

1165 
if k1 = 0 orelse k2 = 0 then 0 

1166 
else if k1 >= max orelse k2 >= max then max 

1167 
else Int.min (max, reasonable_power k2 k1) 

1168 
end 

38190
b02e204b613a
get rid of all "optimizations" regarding "unit" and other cardinality1 types
blanchet
parents:
38188
diff
changeset

1169 
 Type (@{type_name prod}, [T1, T2]) => 
33192  1170 
let 
1171 
val k1 = aux avoid T1 

1172 
val k2 = aux avoid T2 

1173 
in 

1174 
if k1 = 0 orelse k2 = 0 then 0 

1175 
else if k1 >= max orelse k2 >= max then max 

1176 
else Int.min (max, k1 * k2) 

1177 
end 

1178 
 Type (@{type_name itself}, _) => 1 

1179 
 @{typ prop} => 2 

1180 
 @{typ bool} => 2 

1181 
 Type _ => 

35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34998
diff
changeset

1182 
(case datatype_constrs hol_ctxt T of 
34126  1183 
[] => if is_integer_type T orelse is_bit_type T then 0 
1184 
else raise SAME () 

33192  1185 
 constrs => 
1186 
let 

1187 
val constr_cards = 

35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset

1188 
map (Integer.prod o map (aux (T :: avoid)) o binder_types o snd) 
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset

1189 
constrs 
33192  1190 
in 
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33978
diff
changeset

1191 
if exists (curry (op =) 0) constr_cards then 0 
33192  1192 
else Integer.sum constr_cards 
1193 
end) 

1194 
 _ => raise SAME ()) 

34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset

1195 
handle SAME () => 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset

1196 
AList.lookup (op =) assigns T > the_default default_card 
33192  1197 
in Int.min (max, aux [] T) end 
1198 

35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1199 
val small_type_max_card = 5 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1200 

35384  1201 
fun is_finite_type hol_ctxt T = 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1202 
bounded_exact_card_of_type hol_ctxt [] 1 2 [] T > 0 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1203 
fun is_small_finite_type hol_ctxt T = 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1204 
let val n = bounded_exact_card_of_type hol_ctxt [] 1 2 [] T in 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1205 
n > 0 andalso n <= small_type_max_card 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1206 
end 
33192  1207 

1208 
fun is_ground_term (t1 $ t2) = is_ground_term t1 andalso is_ground_term t2 

1209 
 is_ground_term (Const _) = true 

1210 
 is_ground_term _ = false 

1211 

36062  1212 
fun hashw_term (t1 $ t2) = hashw (hashw_term t1, hashw_term t2) 
1213 
 hashw_term (Const (s, _)) = hashw_string (s, 0w0) 

33192  1214 
 hashw_term _ = 0w0 
1215 
val hash_term = Word.toInt o hashw_term 

1216 

1217 
fun special_bounds ts = 

35408  1218 
fold Term.add_vars ts [] > sort (Term_Ord.fast_indexname_ord o pairself fst) 