author  blanchet 
Tue, 07 Dec 2010 11:56:01 +0100  
changeset 41047  9f1d3fcef1ca 
parent 41046  f2e94005d283 
child 41049  0edd245892ed 
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 
tac_timeout: Time.time option, 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
blanchet
parents:
36389
diff
changeset

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

50 
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

51 

33192  52 
val name_sep : string 
53 
val numeral_prefix : string 

35718  54 
val base_prefix : string 
55 
val step_prefix : string 

56 
val unrolled_prefix : string 

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

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

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

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

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

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

67 
val abs_var : indexname * typ > term > term 
41045
2a41709f34c1
use heuristic to determine whether to keep or drop an existing "let"  and drop all higherorder lets
blanchet
parents:
40132
diff
changeset

68 
val is_higher_order_type : typ > bool 
37476
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_fun_type : typ > bool 

92 
val is_set_type : typ > bool 

93 
val is_pair_type : typ > bool 

94 
val is_lfp_iterator_type : typ > bool 

95 
val is_gfp_iterator_type : typ > bool 

96 
val is_fp_iterator_type : typ > bool 

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

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

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

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

101 
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

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

104 
val is_number_type : Proof.context > typ > bool 
33192  105 
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

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

109 
val num_binder_types : typ > int 

110 
val curried_binder_types : typ > typ list 

111 
val mk_flat_tuple : typ > term list > term 

112 
val dest_n_tuple : int > term > term list 

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

113 
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

114 
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

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

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

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

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

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

122 
val is_record_update : theory > styp > bool 

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

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

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

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

126 
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

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

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

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

131 
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

132 
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

133 
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

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

137 
val nth_sel_name_for_constr_name : string > int > string 

138 
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

139 
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

140 
hol_context > bool > styp > int > styp 
33192  141 
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

142 
val close_form : term > term 
33192  143 
val distinctness_formula : typ > term list > term 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

144 
val register_frac_type : 
38284
9f98107ad8b4
use "declaration" instead of "setup" to register Nitpick extensions
blanchet
parents:
38282
diff
changeset

145 
string > (string * string) list > morphism > Context.generic 
9f98107ad8b4
use "declaration" instead of "setup" to register Nitpick extensions
blanchet
parents:
38282
diff
changeset

146 
> Context.generic 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

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

148 
string > (string * string) list > theory > theory 
38284
9f98107ad8b4
use "declaration" instead of "setup" to register Nitpick extensions
blanchet
parents:
38282
diff
changeset

149 
val unregister_frac_type : 
9f98107ad8b4
use "declaration" instead of "setup" to register Nitpick extensions
blanchet
parents:
38282
diff
changeset

150 
string > morphism > Context.generic > Context.generic 
38240
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 : 
38284
9f98107ad8b4
use "declaration" instead of "setup" to register Nitpick extensions
blanchet
parents:
38282
diff
changeset

153 
typ > string > styp list > morphism > Context.generic > Context.generic 
38240
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 
38284
9f98107ad8b4
use "declaration" instead of "setup" to register Nitpick extensions
blanchet
parents:
38282
diff
changeset

156 
val unregister_codatatype : 
9f98107ad8b4
use "declaration" instead of "setup" to register Nitpick extensions
blanchet
parents:
38282
diff
changeset

157 
typ > morphism > Context.generic > Context.generic 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

158 
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

159 
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

160 
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

161 
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

162 
val num_datatype_constrs : hol_context > typ > int 
33192  163 
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

164 
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

165 
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

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

167 
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

168 
> 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

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

170 
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

171 
val coerce_term : hol_context > typ list > typ > typ > term > term 
33192  172 
val card_of_type : (typ * int) list > typ > int 
173 
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

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

175 
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

176 
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

177 
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

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

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

181 
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

182 
val arity_of_built_in_const : 
39359  183 
theory > (typ option * bool) list > styp > int option 
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

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

186 
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

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

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

33192  192 
val const_nondef_table : term list > const_table 
35335  193 
val const_simp_table : Proof.context > (term * term) list > const_table 
194 
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

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

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

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

200 
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

201 
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

202 
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

203 
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

204 
val optimized_quot_type_axioms : 
35311  205 
Proof.context > (typ option * bool) list > string * typ list > term list 
33192  206 
val def_of_const : theory > const_table > styp > term option 
35718  207 
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

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

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

210 
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

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

212 
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

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

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

215 
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

216 
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

217 
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

218 
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

219 
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

220 
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

221 
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

222 
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

223 
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

224 
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

225 
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

226 
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

227 
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

228 
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

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

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

232 
structure Nitpick_HOL : NITPICK_HOL = 
33192  233 
struct 
234 

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

235 
open Nitpick_Util 
33192  236 

237 
type const_table = term list Symtab.table 

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

239 
type unrolled = styp * styp 

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

241 

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

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

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

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

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

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

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

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

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

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

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

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

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

255 
star_linear_preds: 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 

41045
2a41709f34c1
use heuristic to determine whether to keep or drop an existing "let"  and drop all higherorder lets
blanchet
parents:
40132
diff
changeset

322 
fun is_higher_order_type (Type (@{type_name fun}, _)) = true 
2a41709f34c1
use heuristic to determine whether to keep or drop an existing "let"  and drop all higherorder lets
blanchet
parents:
40132
diff
changeset

323 
 is_higher_order_type (Type (_, Ts)) = exists is_higher_order_type Ts 
2a41709f34c1
use heuristic to determine whether to keep or drop an existing "let"  and drop all higherorder lets
blanchet
parents:
40132
diff
changeset

324 
 is_higher_order_type _ = false 
2a41709f34c1
use heuristic to determine whether to keep or drop an existing "let"  and drop all higherorder lets
blanchet
parents:
40132
diff
changeset

325 

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

326 
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

327 

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

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

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

330 

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

331 
fun s_let s n abs_T body_T f t = 
41045
2a41709f34c1
use heuristic to determine whether to keep or drop an existing "let"  and drop all higherorder lets
blanchet
parents:
40132
diff
changeset

332 
if (n  1) * (size_of_term t  1) <= let_inline_threshold orelse 
2a41709f34c1
use heuristic to determine whether to keep or drop an existing "let"  and drop all higherorder lets
blanchet
parents:
40132
diff
changeset

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

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

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

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

337 
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

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

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

340 

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

341 
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

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

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

344 
 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

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

346 

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

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

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

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

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

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

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

353 
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

354 
$ 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

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

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

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

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

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

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

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

362 

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

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

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

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

366 
 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

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

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

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

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

371 
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

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

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

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

375 
in aux Ts t handle Same.SAME => t end 
33192  376 

34998  377 
fun s_conj (t1, @{const True}) = t1 
378 
 s_conj (@{const True}, t2) = t2 

379 
 s_conj (t1, t2) = 

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

381 
else HOLogic.mk_conj (t1, t2) 

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

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

384 
 s_disj (t1, t2) = 

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

386 
else HOLogic.mk_disj (t1, t2) 

387 

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

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

390 
 strip_connective _ t = [t] 

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

391 
fun strip_any_connective (t as (t0 $ _ $ _)) = 
38795
848be46708dc
formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents:
38786
diff
changeset

392 
if t0 = @{const HOL.conj} orelse t0 = @{const HOL.disj} then 
34998  393 
(strip_connective t0 t, t0) 
394 
else 

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

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

38795
848be46708dc
formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents:
38786
diff
changeset

397 
val conjuncts_of = strip_connective @{const HOL.conj} 
848be46708dc
formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents:
38786
diff
changeset

398 
val disjuncts_of = strip_connective @{const HOL.disj} 
34998  399 

33192  400 
(* 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

401 
"Nitpick_Nut.nut_from_term", and perhaps in "Nitpick_Mono.consider_term" as 
33192  402 
well. *) 
403 
val built_in_consts = 

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

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

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

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

408 
(@{const_name Trueprop}, 1), 

409 
(@{const_name Not}, 1), 

410 
(@{const_name False}, 0), 

411 
(@{const_name True}, 0), 

412 
(@{const_name All}, 1), 

413 
(@{const_name Ex}, 1), 

38864
4abe644fcea5
formerly unnamed infix equality now named HOL.eq
haftmann
parents:
38795
diff
changeset

414 
(@{const_name HOL.eq}, 1), 
38795
848be46708dc
formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents:
38786
diff
changeset

415 
(@{const_name HOL.conj}, 2), 
848be46708dc
formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents:
38786
diff
changeset

416 
(@{const_name HOL.disj}, 2), 
38786
e46e7a9cb622
formerly unnamed infix impliciation now named HOL.implies
haftmann
parents:
38652
diff
changeset

417 
(@{const_name HOL.implies}, 2), 
33192  418 
(@{const_name If}, 3), 
419 
(@{const_name Let}, 2), 

420 
(@{const_name Pair}, 2), 

421 
(@{const_name fst}, 1), 

422 
(@{const_name snd}, 1), 

423 
(@{const_name Id}, 0), 

424 
(@{const_name converse}, 1), 

425 
(@{const_name trancl}, 1), 

426 
(@{const_name rel_comp}, 2), 

427 
(@{const_name image}, 2), 

428 
(@{const_name finite}, 1), 

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

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

430 
(@{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

431 
(@{const_name safe_The}, 1), 
33192  432 
(@{const_name Frac}, 0), 
433 
(@{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

434 
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

435 
[(@{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

436 
(@{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

437 
(@{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

438 
(@{const_name nat_lcm}, 0)] 
33192  439 
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

440 
[((@{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

441 
((@{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

442 
((@{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

443 
((@{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

444 
((@{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

445 
((@{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

446 
((@{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

447 
((@{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

448 
((@{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

449 
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

450 
[((@{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

451 
((@{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

452 
((@{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

453 
((@{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

454 
((@{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

455 
((@{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

456 
((@{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

457 
((@{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

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

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

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

462 
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

463 
 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

464 
 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

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

466 
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

467 
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

468 
 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

469 
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

470 
 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

471 
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

472 
 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

473 
 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

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

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

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

477 
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

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

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

480 
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

481 

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

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

485 
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

486 
fun shortest_name s = List.last (space_explode "." s) handle List.Empty => "" 
33192  487 
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

488 
fun short_name s = 
33192  489 
case space_explode name_sep s of 
490 
[_] => 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

491 
 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

492 
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

493 
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

494 
 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

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

496 
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

497 
#> map_types shorten_names_in_type 
33192  498 

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

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

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

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

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

506 
 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

507 
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

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

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

510 
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

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

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

513 
 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

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

515 
 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

516 
$ 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

517 

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

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

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

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

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

527 
 is_lfp_iterator_type _ = false 

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

529 
 is_gfp_iterator_type _ = false 

530 
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

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

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

533 
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

534 
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

535 
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

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

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

538 
val is_integer_like_type = is_iterator_type orf is_integer_type orf is_word_type 
33192  539 
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

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

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

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

544 
fun is_number_type ctxt = is_integer_like_type orf is_frac_type ctxt 
33192  545 

546 
fun iterator_type_for_const gfp (s, T) = 

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

548 
binder_types T) 

35718  549 
fun const_for_iterator_type (Type (s, Ts)) = 
550 
(strip_first_name_sep s > snd, Ts > bool_T) 

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

552 
raise TYPE ("Nitpick_HOL.const_for_iterator_type", [T], []) 
33192  553 

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

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

555 
 strip_n_binders n (Type (@{type_name fun}, [T1, T2])) = 
33192  556 
strip_n_binders (n  1) T2 >> cons T1 
557 
 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

558 
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

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

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

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

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

565 
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

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

569 
fun maybe_curried_binder_types T = 

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

571 

572 
fun mk_flat_tuple _ [t] = t 

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

573 
 mk_flat_tuple (Type (@{type_name prod}, [T1, T2])) (t :: ts) = 
33192  574 
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

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

578 

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

579 
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

580 
{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

581 
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

582 
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

583 

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

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

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

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

587 
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

588 
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

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

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

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

592 
(* 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

593 
we take (according to Florian Haftmann). *) 
39315
27f7b7748425
always handle type variables in typedefs as global
blanchet
parents:
38864
diff
changeset

594 
(* The "Logic.varifyT_global" calls are a temporary hack because these 
27f7b7748425
always handle type variables in typedefs as global
blanchet
parents:
38864
diff
changeset

595 
types's type variables sometimes clash with locally fixed type variables. 
27f7b7748425
always handle type variables in typedefs as global
blanchet
parents:
38864
diff
changeset

596 
Remove these calls once "Typedef" is fully localized. *) 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

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

598 
{set_def, Rep, Abs_inverse, Rep_inverse, ...}) :: _ => 
39315
27f7b7748425
always handle type variables in typedefs as global
blanchet
parents:
38864
diff
changeset

599 
SOME {abs_type = Logic.varifyT_global abs_type, 
27f7b7748425
always handle type variables in typedefs as global
blanchet
parents:
38864
diff
changeset

600 
rep_type = Logic.varifyT_global rep_type, Abs_name = Abs_name, 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

601 
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

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

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

604 
 _ => 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

605 

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

607 
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

608 
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

609 

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

610 
(* 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

611 
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

612 
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

613 
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

614 
"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

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

616 

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

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

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

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

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

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

625 
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

626 
end 
33192  627 

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

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

629 
varify_and_instantiate_type ctxt (body_type T) body_T' T 
33192  630 

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

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

633 
val {frac_types, codatatypes} = Data.get generic 
33192  634 
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

635 
in Data.put {frac_types = frac_types, codatatypes = codatatypes} generic end 
38284
9f98107ad8b4
use "declaration" instead of "setup" to register Nitpick extensions
blanchet
parents:
38282
diff
changeset

636 
(* TODO: Consider morphism. *) 
9f98107ad8b4
use "declaration" instead of "setup" to register Nitpick extensions
blanchet
parents:
38282
diff
changeset

637 
fun register_frac_type frac_s ersaetze (_ : morphism) = 
9f98107ad8b4
use "declaration" instead of "setup" to register Nitpick extensions
blanchet
parents:
38282
diff
changeset

638 
register_frac_type_generic frac_s ersaetze 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

639 
val register_frac_type_global = Context.theory_map oo register_frac_type_generic 
33192  640 

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

641 
fun unregister_frac_type_generic frac_s = register_frac_type_generic frac_s [] 
38284
9f98107ad8b4
use "declaration" instead of "setup" to register Nitpick extensions
blanchet
parents:
38282
diff
changeset

642 
(* TODO: Consider morphism. *) 
9f98107ad8b4
use "declaration" instead of "setup" to register Nitpick extensions
blanchet
parents:
38282
diff
changeset

643 
fun unregister_frac_type frac_s (_ : morphism) = 
9f98107ad8b4
use "declaration" instead of "setup" to register Nitpick extensions
blanchet
parents:
38282
diff
changeset

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

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

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

647 

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

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

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

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

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

653 
val constr_xs = map (apsnd (repair_constr_type ctxt co_T)) constr_xs 
33192  654 
val (co_s, co_Ts) = dest_Type co_T 
655 
val _ = 

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

656 
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

657 
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

658 
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

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

660 
else 
38242  661 
raise TYPE ("Nitpick_HOL.register_codatatype_generic", [co_T], []) 
33192  662 
val codatatypes = AList.update (op =) (co_s, (case_name, constr_xs)) 
663 
codatatypes 

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

664 
in Data.put {frac_types = frac_types, codatatypes = codatatypes} generic end 
38284
9f98107ad8b4
use "declaration" instead of "setup" to register Nitpick extensions
blanchet
parents:
38282
diff
changeset

665 
(* TODO: Consider morphism. *) 
9f98107ad8b4
use "declaration" instead of "setup" to register Nitpick extensions
blanchet
parents:
38282
diff
changeset

666 
fun register_codatatype co_T case_name constr_xs (_ : morphism) = 
9f98107ad8b4
use "declaration" instead of "setup" to register Nitpick extensions
blanchet
parents:
38282
diff
changeset

667 
register_codatatype_generic co_T case_name constr_xs 
38240
a44d108a8d39
local versions of Nitpick.register_xxx functions
blanchet
parents:
38215
diff
changeset

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

669 
Context.theory_map ooo register_codatatype_generic 
33192  670 

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

671 
fun unregister_codatatype_generic co_T = register_codatatype_generic co_T "" [] 
38284
9f98107ad8b4
use "declaration" instead of "setup" to register Nitpick extensions
blanchet
parents:
38282
diff
changeset

672 
(* TODO: Consider morphism. *) 
9f98107ad8b4
use "declaration" instead of "setup" to register Nitpick extensions
blanchet
parents:
38282
diff
changeset

673 
fun unregister_codatatype co_T (_ : morphism) = 
9f98107ad8b4
use "declaration" instead of "setup" to register Nitpick extensions
blanchet
parents:
38282
diff
changeset

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

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

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

677 

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

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

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

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

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

683 
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

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

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

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

687 
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

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

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

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

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

692 
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

693 
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

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

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

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

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

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

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

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

703 
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

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

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

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

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

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

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

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

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

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

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

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

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

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

717 
end 
33192  718 
 NONE => false) 
719 
 is_univ_typedef _ _ = false 

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

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

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

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

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

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

725 
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

726 
 is_datatype _ _ _ = false 
33192  727 

728 
fun all_record_fields thy T = 

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

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

731 
end 

732 
handle TYPE _ => [] 

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

733 
fun is_record_constr (s, T) = 
33192  734 
String.isSuffix Record.extN s andalso 
735 
let val dataT = body_type T in 

736 
is_record_type dataT andalso 

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

738 
end 

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

740 
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

741 
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

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

743 
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

744 
exists (curry (op =) s o fst) (all_record_fields thy T1) 
33192  745 
 is_record_get _ _ = false 
746 
fun is_record_update thy (s, T) = 

747 
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

748 
exists (curry (op =) (unsuffix Record.updateN s) o fst) 
33192  749 
(all_record_fields thy (body_type T)) 
750 
handle TYPE _ => false 

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

751 
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

752 
(case typedef_info ctxt s' of 
33192  753 
SOME {Abs_name, ...} => s = Abs_name 
754 
 NONE => false) 

755 
 is_abs_fun _ _ = false 

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

756 
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

757 
(case typedef_info ctxt s' of 
33192  758 
SOME {Rep_name, ...} => s = Rep_name 
759 
 NONE => false) 

760 
 is_rep_fun _ _ = false 

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

761 
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

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

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

764 
= 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

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

766 
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

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

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

769 
= 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

770 
 is_quot_rep_fun _ _ = false 
33192  771 

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

772 
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

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

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

775 
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

776 
 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

777 
 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

778 
fun rep_type_for_quot_type thy (T as Type (s, _)) = 
39345  779 
let val {qtyp, rtyp, ...} = Quotient_Info.quotdata_lookup thy s in 
780 
instantiate_type thy qtyp T rtyp 

781 
end 

782 
 rep_type_for_quot_type _ T = 

783 
raise TYPE ("Nitpick_HOL.rep_type_for_quot_type", [T], []) 

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

784 
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

785 
let 
38243  786 
val {qtyp, equiv_rel, equiv_thm, ...} = 
787 
Quotient_Info.quotdata_lookup thy s 

788 
val partial = 

789 
case prop_of equiv_thm of 

790 
@{const Trueprop} $ (Const (@{const_name equivp}, _) $ _) => false 

791 
 @{const Trueprop} $ (Const (@{const_name part_equivp}, _) $ _) => true 

792 
 _ => raise NOT_SUPPORTED "Illformed quotient type equivalence \ 

793 
\relation theorem" 

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

794 
val Ts' = qtyp > dest_Type > snd 
38243  795 
in (subst_atomic_types (Ts' ~~ Ts) equiv_rel, partial) end 
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34126
diff
changeset

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

797 
raise TYPE ("Nitpick_HOL.equiv_relation_for_quot_type", [T], []) 
33192  798 

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

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

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

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

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

803 
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

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

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

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

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

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

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

810 
@{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

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

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

813 
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

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

815 
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

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

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

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

820 
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

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

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

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

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

825 
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

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

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

828 
end 
33192  829 
val is_sel = String.isPrefix discr_prefix orf String.isPrefix sel_prefix 
830 
val is_sel_like_and_no_discr = 

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

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

832 
(member (op =) [@{const_name fst}, @{const_name snd}]) 
33192  833 

834 
fun in_fun_lhs_for InConstr = InSel 

835 
 in_fun_lhs_for _ = InFunLHS 

836 
fun in_fun_rhs_for InConstr = InConstr 

837 
 in_fun_rhs_for InSel = InSel 

838 
 in_fun_rhs_for InFunRHS1 = InFunRHS2 

839 
 in_fun_rhs_for _ = InFunRHS1 

840 

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

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

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

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

845 
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

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

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

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

849 
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

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

852 
and should_box_type (hol_ctxt as {thy, boxes, ...}) boxy z = 
33192  853 
case triple_lookup (type_match thy) boxes (Type z) of 
854 
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

855 
 _ => 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

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

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

859 
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

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

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

864 
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

865 
> 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

866 
 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

867 
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

868 
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

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

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

872 
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

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

874 
else InPair)) Ts) 
33192  875 
 _ => T 
876 

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

877 
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

878 
 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

879 
 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

880 
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

881 
 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

882 
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

883 

33192  884 
fun discr_for_constr (s, T) = (discr_prefix ^ s, body_type T > bool_T) 
885 

886 
fun num_sels_for_constr_type T = length (maybe_curried_binder_types T) 

887 
fun nth_sel_name_for_constr_name s n = 

888 
if s = @{const_name Pair} then 

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

890 
else 

891 
sel_prefix_for n ^ s 

892 
fun nth_sel_for_constr x ~1 = discr_for_constr x 

893 
 nth_sel_for_constr (s, T) n = 

894 
(nth_sel_name_for_constr_name s n, 

895 
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

896 
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

897 
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

898 
oo nth_sel_for_constr 
33192  899 

900 
fun sel_no_from_name s = 

901 
if String.isPrefix discr_prefix s then 

902 
~1 

903 
else if String.isPrefix sel_prefix s then 

904 
s > unprefix sel_prefix > Int.fromString > the 

905 
else if s = @{const_name snd} then 

906 
1 

907 
else 

908 
0 

909 

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

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

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

912 
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

913 
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

914 
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

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

916 
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

917 
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

918 
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

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

920 
 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

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

922 

33192  923 
fun distinctness_formula T = 
924 
all_distinct_unordered_pairs_of 

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

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

927 

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

928 
fun zero_const T = Const (@{const_name zero_class.zero}, T) 
33192  929 
fun suc_const T = Const (@{const_name Suc}, T > T) 
930 

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

931 
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

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

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

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

935 
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

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

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

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

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

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

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

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

943 
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

944 
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

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

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

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

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

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

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

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

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

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

954 
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

955 
[(@{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

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

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

959 
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

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

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

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

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

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

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

966 
[]) 
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

967 
 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

968 
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

969 
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

970 
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

971 
 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

972 
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

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

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

975 
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

976 
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

977 
o box_type hol_ctxt InConstr)) o datatype_constrs hol_ctxt 
33192  978 
val num_datatype_constrs = length oo datatype_constrs 
979 

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

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

982 
 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

983 
fun binarized_and_boxed_constr_for_sel hol_ctxt binarize (s', T') = 
33192  984 
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

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

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

987 
s 
33192  988 
> the > pair s 
989 
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

990 

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

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

994 
Abs (Name.uu, dataT, 

995 
@{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

996 
else if num_datatype_constrs hol_ctxt dataT >= 2 then 
33192  997 
Const (discr_for_constr x) 
998 
else 

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

1000 
end 

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

1001 
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

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

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

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

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

1007 
 _ => s_betapply [] (discr_term_for_constr hol_ctxt x, t) 
33192  1008 

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

1009 
fun nth_arg_sel_term_for_constr thy stds (x as (s, T)) n = 
33192  1010 
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

1011 
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

1012 
@{term "%n::nat. n  1"} 
33192  1013 
else if is_pair_type dataT then 
1014 
Const (nth_sel_for_constr x n) 

1015 
else 

1016 
let 

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

1017 
fun aux m (Type (@{type_name prod}, [T1, T2])) = 
33192  1018 
let 
1019 
val (m, t1) = aux m T1 

1020 
val (m, t2) = aux m T2 

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

1022 
 aux m T = 

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

1024 
$ Bound 0) 

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

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

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

1028 
end 

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

1029 
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

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

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

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

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

1034 
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

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

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

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

1038 
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

1039 
end 
33192  1040 

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

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

1042 
 construct_value ctxt stds (x as (s, _)) args = 
33192  1043 
let val args = map Envir.eta_contract args in 
1044 
case hd args of 

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

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

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

1047 
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

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

1049 
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

1050 
(index_seq 0 (length args) ~~ args) then 
33192  1051 
t 
1052 
else 

1053 
list_comb (Const x, args) 

1054 
 _ => list_comb (Const x, args) 

1055 
end 

1056 

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

1057 
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

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

1059 
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

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

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

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

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

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

1065 
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

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

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

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

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

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

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

1072 
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

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

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

1075 

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

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

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

1078 
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

1079 
 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

1080 
 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

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

1082 
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

1083 
old_T <> new_T ? coerce_bound_no (coerce_term hol_ctxt [new_T] old_T new_T) 0 
39345  1084 
and coerce_term (hol_ctxt as {ctxt, stds, ...}) Ts new_T old_T t = 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

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

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

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

1088 
case (new_T, old_T) of 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

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

1090 
Type (@{type_name fun}, [old_T1, old_T2])) => 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1091 
(case eta_expand Ts t 1 of 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1092 
Abs (s, _, t') => 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1093 
Abs (s, new_T1, 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1094 
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

1095 
> 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

1096 
> Envir.eta_contract 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1097 
> new_s <> @{type_name fun} 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

1098 
? construct_value ctxt stds 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

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

1100 
else @{const_name FunBox}, 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1101 
Type (@{type_name fun}, new_Ts) > new_T) 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

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

1103 
 t' => raise TERM ("Nitpick_HOL.coerce_term", [t'])) 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

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

1105 
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

1106 
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

1107 
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

1108 
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

1109 
Const (old_s, _) $ t1 => 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1110 
if new_s = @{type_name fun} then 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1111 
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

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

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

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

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

1116 
(Type (@{type_name fun}, old_Ts)) t1] 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1117 
 Const _ $ t1 $ t2 => 
37256
0dca1ec52999
thread along context instead of theory for typedef lookup
blanchet
parents:
37253
diff
changeset

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

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

1120 
else @{const_name PairBox}, new_Ts > new_T) 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

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

1122 
[t1, t2]) 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1123 
 t' => raise TERM ("Nitpick_HOL.coerce_term", [t']) 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

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

1125 
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

1126 
 _ => 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

1127 

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

1128 
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

1129 
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

1130 
 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

1131 
card_of_type assigns T1 * card_of_type assigns T2 
33192  1132 
 card_of_type _ (Type (@{type_name itself}, _)) = 1 
1133 
 card_of_type _ @{typ prop} = 2 

1134 
 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

1135 
 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

1136 
case AList.lookup (op =) assigns T of 
33192  1137 
SOME k => k 
1138 
 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

1139 
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

1140 
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

1141 
(Type (@{type_name fun}, [T1, T2])) = 
33192  1142 
let 
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset

1143 
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

1144 
val k2 = bounded_card_of_type max default_card assigns T2 
33192  1145 
in 
1146 
if k1 = max orelse k2 = max then max 

1147 
else Int.min (max, reasonable_power k2 k1) 

1148 
end 

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

1149 
 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

1150 
(Type (@{type_name prod}, [T1, T2])) = 
33192  1151 
let 
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset

1152 
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

1153 
val k2 = bounded_card_of_type max default_card assigns T2 
33192  1154 
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

1155 
 bounded_card_of_type max default_card assigns T = 
33192  1156 
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

1157 
card_of_type assigns T 
33192  1158 
else 
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset

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

1160 
handle TYPE ("Nitpick_HOL.card_of_type", _, _) => 
33192  1161 
default_card) 
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35384
diff
changeset

1162 
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

1163 
assigns T = 
33192  1164 
let 
1165 
fun aux avoid T = 

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

1166 
(if member (op =) avoid T then 
33192  1167 
0 
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35384
diff
changeset

1168 
else if member (op =) finitizable_dataTs T then 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35384
diff
changeset

1169 
raise SAME () 
33192  1170 
else case T of 
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1171 
Type (@{type_name fun}, [T1, T2]) => 
33192  1172 
let 
1173 
val k1 = aux avoid T1 

1174 
val k2 = aux avoid T2 

1175 
in 

1176 
if k1 = 0 orelse k2 = 0 then 0 

1177 
else if k1 >= max orelse k2 >= max then max 

1178 
else Int.min (max, reasonable_power k2 k1) 

1179 
end 

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

1180 
 Type (@{type_name prod}, [T1, T2]) => 
33192  1181 
let 
1182 
val k1 = aux avoid T1 

1183 
val k2 = aux avoid T2 

1184 
in 

1185 
if k1 = 0 orelse k2 = 0 then 0 

1186 
else if k1 >= max orelse k2 >= max then max 

1187 
else Int.min (max, k1 * k2) 

1188 
end 

1189 
 Type (@{type_name itself}, _) => 1 

1190 
 @{typ prop} => 2 

1191 
 @{typ bool} => 2 

1192 
 Type _ => 

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

1193 
(case datatype_constrs hol_ctxt T of 
34126  1194 
[] => if is_integer_type T orelse is_bit_type T then 0 
1195 
else raise SAME () 

33192  1196 
 constrs => 
1197 
let 

1198 
val constr_cards = 

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

1199 
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

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

1202 
if exists (curry (op =) 0) constr_cards then 0 
33192  1203 
else Integer.sum constr_cards 
1204 
end) 

1205 
 _ => raise SAME ()) 

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

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

1207 
AList.lookup (op =) assigns T > the_default default_card 
33192  1208 
in Int.min (max, aux [] T) end 
1209 

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

1210 
val small_type_max_card = 5 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35625
diff
changeset

1211 

35384  1212 
fun is_finite_type hol_ctxt T = 