author | blanchet |
Wed, 17 Feb 2010 12:14:08 +0100 | |
changeset 35185 | 9b8f351cced6 |
parent 35028 | 108662d50512 |
child 35280 | 54ab4921f826 |
permissions | -rw-r--r-- |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
1 |
(* Title: HOL/Tools/Nitpick/minipick.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:
34124
diff
changeset
|
3 |
Copyright 2009, 2010 |
33192 | 4 |
|
5 |
Finite model generation for HOL formulas using Kodkod, minimalistic version. |
|
6 |
*) |
|
7 |
||
8 |
signature MINIPICK = |
|
9 |
sig |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
10 |
datatype rep = SRep | RRep |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
11 |
type styp = Nitpick_Util.styp |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
12 |
|
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
13 |
val vars_for_bound_var : |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
14 |
(typ -> int) -> rep -> typ list -> int -> Kodkod.rel_expr list |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
15 |
val rel_expr_for_bound_var : |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
16 |
(typ -> int) -> rep -> typ list -> int -> Kodkod.rel_expr |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
17 |
val decls_for : rep -> (typ -> int) -> typ list -> typ -> Kodkod.decl list |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
18 |
val false_atom : Kodkod.rel_expr |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
19 |
val true_atom : Kodkod.rel_expr |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
20 |
val formula_from_atom : Kodkod.rel_expr -> Kodkod.formula |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
21 |
val atom_from_formula : Kodkod.formula -> Kodkod.rel_expr |
33192 | 22 |
val pick_nits_in_term : Proof.context -> (typ -> int) -> term -> string |
23 |
end; |
|
24 |
||
25 |
structure Minipick : MINIPICK = |
|
26 |
struct |
|
27 |
||
28 |
open Kodkod |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
29 |
open Nitpick_Util |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
30 |
open Nitpick_HOL |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
31 |
open Nitpick_Peephole |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
32 |
open Nitpick_Kodkod |
33192 | 33 |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
34 |
datatype rep = SRep | RRep |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
35 |
|
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
36 |
(* Proof.context -> typ -> unit *) |
33192 | 37 |
fun check_type ctxt (Type ("fun", Ts)) = List.app (check_type ctxt) Ts |
38 |
| check_type ctxt (Type ("*", Ts)) = List.app (check_type ctxt) Ts |
|
39 |
| check_type _ @{typ bool} = () |
|
40 |
| check_type _ (TFree (_, @{sort "{}"})) = () |
|
41 |
| check_type _ (TFree (_, @{sort HOL.type})) = () |
|
42 |
| check_type ctxt T = |
|
43 |
raise NOT_SUPPORTED ("type " ^ quote (Syntax.string_of_typ ctxt T)) |
|
44 |
||
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
45 |
(* rep -> (typ -> int) -> typ -> int list *) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
46 |
fun atom_schema_of SRep card (Type ("fun", [T1, T2])) = |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
47 |
replicate_list (card T1) (atom_schema_of SRep card T2) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
48 |
| atom_schema_of RRep card (Type ("fun", [T1, @{typ bool}])) = |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
49 |
atom_schema_of SRep card T1 |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
50 |
| atom_schema_of RRep card (Type ("fun", [T1, T2])) = |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
51 |
atom_schema_of SRep card T1 @ atom_schema_of RRep card T2 |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
52 |
| atom_schema_of _ card (Type ("*", Ts)) = maps (atom_schema_of SRep card) Ts |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
53 |
| atom_schema_of _ card T = [card T] |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
54 |
(* rep -> (typ -> int) -> typ -> int *) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
55 |
val arity_of = length ooo atom_schema_of |
33192 | 56 |
|
57 |
(* (typ -> int) -> typ list -> int -> int *) |
|
58 |
fun index_for_bound_var _ [_] 0 = 0 |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
59 |
| index_for_bound_var card (_ :: Ts) 0 = |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
60 |
index_for_bound_var card Ts 0 + arity_of SRep card (hd Ts) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
61 |
| index_for_bound_var card Ts n = index_for_bound_var card (tl Ts) (n - 1) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
62 |
(* (typ -> int) -> rep -> typ list -> int -> rel_expr list *) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
63 |
fun vars_for_bound_var card R Ts j = |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
64 |
map (curry Var 1) (index_seq (index_for_bound_var card Ts j) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
65 |
(arity_of R card (nth Ts j))) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
66 |
(* (typ -> int) -> rep -> typ list -> int -> rel_expr *) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
67 |
val rel_expr_for_bound_var = foldl1 Product oooo vars_for_bound_var |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
68 |
(* rep -> (typ -> int) -> typ list -> typ -> decl list *) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
69 |
fun decls_for R card Ts T = |
33192 | 70 |
map2 (curry DeclOne o pair 1) |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
71 |
(index_seq (index_for_bound_var card (T :: Ts) 0) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
72 |
(arity_of R card (nth (T :: Ts) 0))) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
73 |
(map (AtomSeq o rpair 0) (atom_schema_of R card T)) |
33192 | 74 |
|
75 |
(* int list -> rel_expr *) |
|
76 |
val atom_product = foldl1 Product o map Atom |
|
77 |
||
78 |
val false_atom = Atom 0 |
|
79 |
val true_atom = Atom 1 |
|
80 |
||
81 |
(* rel_expr -> formula *) |
|
82 |
fun formula_from_atom r = RelEq (r, true_atom) |
|
83 |
(* formula -> rel_expr *) |
|
84 |
fun atom_from_formula f = RelIf (f, true_atom, false_atom) |
|
85 |
||
86 |
(* Proof.context -> (typ -> int) -> styp list -> term -> formula *) |
|
35185
9b8f351cced6
added yet another hint to Nitpick's output, this time warning about problems for which nothing was effectively tested
blanchet
parents:
35028
diff
changeset
|
87 |
fun kodkod_formula_from_term ctxt card frees = |
33192 | 88 |
let |
89 |
(* typ -> rel_expr -> rel_expr *) |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
90 |
fun R_rep_from_S_rep (T as Type ("fun", [T1, @{typ bool}])) r = |
33192 | 91 |
let |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
92 |
val jss = atom_schema_of SRep card T1 |> map (rpair 0) |
33192 | 93 |
|> all_combinations |
94 |
in |
|
95 |
map2 (fn i => fn js => |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
96 |
RelIf (formula_from_atom (Project (r, [Num i])), |
33192 | 97 |
atom_product js, empty_n_ary_rel (length js))) |
98 |
(index_seq 0 (length jss)) jss |
|
99 |
|> foldl1 Union |
|
100 |
end |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
101 |
| R_rep_from_S_rep (Type ("fun", [T1, T2])) r = |
33192 | 102 |
let |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
103 |
val jss = atom_schema_of SRep card T1 |> map (rpair 0) |
33192 | 104 |
|> all_combinations |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
105 |
val arity2 = arity_of SRep card T2 |
33192 | 106 |
in |
107 |
map2 (fn i => fn js => |
|
108 |
Product (atom_product js, |
|
109 |
Project (r, num_seq (i * arity2) arity2) |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
110 |
|> R_rep_from_S_rep T2)) |
33192 | 111 |
(index_seq 0 (length jss)) jss |
112 |
|> foldl1 Union |
|
113 |
end |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
114 |
| R_rep_from_S_rep _ r = r |
33192 | 115 |
(* typ list -> typ -> rel_expr -> rel_expr *) |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
116 |
fun S_rep_from_R_rep Ts (T as Type ("fun", _)) r = |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
117 |
Comprehension (decls_for SRep card Ts T, |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
118 |
RelEq (R_rep_from_S_rep T |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
119 |
(rel_expr_for_bound_var card SRep (T :: Ts) 0), r)) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
120 |
| S_rep_from_R_rep _ _ r = r |
33192 | 121 |
(* typ list -> term -> formula *) |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
122 |
fun to_F Ts t = |
33192 | 123 |
(case t of |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
124 |
@{const Not} $ t1 => Not (to_F Ts t1) |
33192 | 125 |
| @{const False} => False |
126 |
| @{const True} => True |
|
127 |
| Const (@{const_name All}, _) $ Abs (s, T, t') => |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
128 |
All (decls_for SRep card Ts T, to_F (T :: Ts) t') |
33192 | 129 |
| (t0 as Const (@{const_name All}, _)) $ t1 => |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
130 |
to_F Ts (t0 $ eta_expand Ts t1 1) |
33192 | 131 |
| Const (@{const_name Ex}, _) $ Abs (s, T, t') => |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
132 |
Exist (decls_for SRep card Ts T, to_F (T :: Ts) t') |
33192 | 133 |
| (t0 as Const (@{const_name Ex}, _)) $ t1 => |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
134 |
to_F Ts (t0 $ eta_expand Ts t1 1) |
33192 | 135 |
| Const (@{const_name "op ="}, _) $ t1 $ t2 => |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
136 |
RelEq (to_R_rep Ts t1, to_R_rep Ts t2) |
33192 | 137 |
| Const (@{const_name ord_class.less_eq}, |
138 |
Type ("fun", [Type ("fun", [_, @{typ bool}]), _])) $ t1 $ t2 => |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
139 |
Subset (to_R_rep Ts t1, to_R_rep Ts t2) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
140 |
| @{const "op &"} $ t1 $ t2 => And (to_F Ts t1, to_F Ts t2) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
141 |
| @{const "op |"} $ t1 $ t2 => Or (to_F Ts t1, to_F Ts t2) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
142 |
| @{const "op -->"} $ t1 $ t2 => Implies (to_F Ts t1, to_F Ts t2) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
143 |
| t1 $ t2 => Subset (to_S_rep Ts t2, to_R_rep Ts t1) |
33192 | 144 |
| Free _ => raise SAME () |
145 |
| Term.Var _ => raise SAME () |
|
146 |
| Bound _ => raise SAME () |
|
147 |
| Const (s, _) => raise NOT_SUPPORTED ("constant " ^ quote s) |
|
35185
9b8f351cced6
added yet another hint to Nitpick's output, this time warning about problems for which nothing was effectively tested
blanchet
parents:
35028
diff
changeset
|
148 |
| _ => raise TERM ("Minipick.kodkod_formula_from_term.to_F", [t])) |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
149 |
handle SAME () => formula_from_atom (to_R_rep Ts t) |
33192 | 150 |
(* typ list -> term -> rel_expr *) |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
151 |
and to_S_rep Ts t = |
33192 | 152 |
case t of |
153 |
Const (@{const_name Pair}, _) $ t1 $ t2 => |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
154 |
Product (to_S_rep Ts t1, to_S_rep Ts t2) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
155 |
| Const (@{const_name Pair}, _) $ _ => to_S_rep Ts (eta_expand Ts t 1) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
156 |
| Const (@{const_name Pair}, _) => to_S_rep Ts (eta_expand Ts t 2) |
33192 | 157 |
| Const (@{const_name fst}, _) $ t1 => |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
158 |
let val fst_arity = arity_of SRep card (fastype_of1 (Ts, t)) in |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
159 |
Project (to_S_rep Ts t1, num_seq 0 fst_arity) |
33192 | 160 |
end |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
161 |
| Const (@{const_name fst}, _) => to_S_rep Ts (eta_expand Ts t 1) |
33192 | 162 |
| Const (@{const_name snd}, _) $ t1 => |
163 |
let |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
164 |
val pair_arity = arity_of SRep card (fastype_of1 (Ts, t1)) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
165 |
val snd_arity = arity_of SRep card (fastype_of1 (Ts, t)) |
33192 | 166 |
val fst_arity = pair_arity - snd_arity |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
167 |
in Project (to_S_rep Ts t1, num_seq fst_arity snd_arity) end |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
168 |
| Const (@{const_name snd}, _) => to_S_rep Ts (eta_expand Ts t 1) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
169 |
| Bound j => rel_expr_for_bound_var card SRep Ts j |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
170 |
| _ => S_rep_from_R_rep Ts (fastype_of1 (Ts, t)) (to_R_rep Ts t) |
33192 | 171 |
(* term -> rel_expr *) |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
172 |
and to_R_rep Ts t = |
33192 | 173 |
(case t of |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
174 |
@{const Not} => to_R_rep Ts (eta_expand Ts t 1) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
175 |
| Const (@{const_name All}, _) => to_R_rep Ts (eta_expand Ts t 1) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
176 |
| Const (@{const_name Ex}, _) => to_R_rep Ts (eta_expand Ts t 1) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
177 |
| Const (@{const_name "op ="}, _) $ _ => to_R_rep Ts (eta_expand Ts t 1) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
178 |
| Const (@{const_name "op ="}, _) => to_R_rep Ts (eta_expand Ts t 2) |
33192 | 179 |
| Const (@{const_name ord_class.less_eq}, |
180 |
Type ("fun", [Type ("fun", [_, @{typ bool}]), _])) $ _ => |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
181 |
to_R_rep Ts (eta_expand Ts t 1) |
33192 | 182 |
| Const (@{const_name ord_class.less_eq}, _) => |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
183 |
to_R_rep Ts (eta_expand Ts t 2) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
184 |
| @{const "op &"} $ _ => to_R_rep Ts (eta_expand Ts t 1) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
185 |
| @{const "op &"} => to_R_rep Ts (eta_expand Ts t 2) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
186 |
| @{const "op |"} $ _ => to_R_rep Ts (eta_expand Ts t 1) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
187 |
| @{const "op |"} => to_R_rep Ts (eta_expand Ts t 2) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
188 |
| @{const "op -->"} $ _ => to_R_rep Ts (eta_expand Ts t 1) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
189 |
| @{const "op -->"} => to_R_rep Ts (eta_expand Ts t 2) |
33192 | 190 |
| Const (@{const_name bot_class.bot}, |
191 |
T as Type ("fun", [_, @{typ bool}])) => |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
192 |
empty_n_ary_rel (arity_of RRep card T) |
33192 | 193 |
| Const (@{const_name insert}, _) $ t1 $ t2 => |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
194 |
Union (to_S_rep Ts t1, to_R_rep Ts t2) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
195 |
| Const (@{const_name insert}, _) $ _ => to_R_rep Ts (eta_expand Ts t 1) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
196 |
| Const (@{const_name insert}, _) => to_R_rep Ts (eta_expand Ts t 2) |
33192 | 197 |
| Const (@{const_name trancl}, _) $ t1 => |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
198 |
if arity_of RRep card (fastype_of1 (Ts, t1)) = 2 then |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
199 |
Closure (to_R_rep Ts t1) |
33192 | 200 |
else |
201 |
raise NOT_SUPPORTED "transitive closure for function or pair type" |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
202 |
| Const (@{const_name trancl}, _) => to_R_rep Ts (eta_expand Ts t 1) |
35028
108662d50512
more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
haftmann
parents:
34982
diff
changeset
|
203 |
| Const (@{const_name semilattice_inf_class.inf}, |
33192 | 204 |
Type ("fun", [Type ("fun", [_, @{typ bool}]), _])) $ t1 $ t2 => |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
205 |
Intersect (to_R_rep Ts t1, to_R_rep Ts t2) |
35028
108662d50512
more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
haftmann
parents:
34982
diff
changeset
|
206 |
| Const (@{const_name semilattice_inf_class.inf}, _) $ _ => |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
207 |
to_R_rep Ts (eta_expand Ts t 1) |
35028
108662d50512
more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
haftmann
parents:
34982
diff
changeset
|
208 |
| Const (@{const_name semilattice_inf_class.inf}, _) => |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
209 |
to_R_rep Ts (eta_expand Ts t 2) |
35028
108662d50512
more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
haftmann
parents:
34982
diff
changeset
|
210 |
| Const (@{const_name semilattice_sup_class.sup}, |
33192 | 211 |
Type ("fun", [Type ("fun", [_, @{typ bool}]), _])) $ t1 $ t2 => |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
212 |
Union (to_R_rep Ts t1, to_R_rep Ts t2) |
35028
108662d50512
more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
haftmann
parents:
34982
diff
changeset
|
213 |
| Const (@{const_name semilattice_sup_class.sup}, _) $ _ => |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
214 |
to_R_rep Ts (eta_expand Ts t 1) |
35028
108662d50512
more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
haftmann
parents:
34982
diff
changeset
|
215 |
| Const (@{const_name semilattice_sup_class.sup}, _) => |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
216 |
to_R_rep Ts (eta_expand Ts t 2) |
33192 | 217 |
| Const (@{const_name minus_class.minus}, |
218 |
Type ("fun", [Type ("fun", [_, @{typ bool}]), _])) $ t1 $ t2 => |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
219 |
Difference (to_R_rep Ts t1, to_R_rep Ts t2) |
33192 | 220 |
| Const (@{const_name minus_class.minus}, |
221 |
Type ("fun", [Type ("fun", [_, @{typ bool}]), _])) $ _ => |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
222 |
to_R_rep Ts (eta_expand Ts t 1) |
33192 | 223 |
| Const (@{const_name minus_class.minus}, |
224 |
Type ("fun", [Type ("fun", [_, @{typ bool}]), _])) => |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
225 |
to_R_rep Ts (eta_expand Ts t 2) |
33192 | 226 |
| Const (@{const_name Pair}, _) $ _ $ _ => raise SAME () |
227 |
| Const (@{const_name Pair}, _) $ _ => raise SAME () |
|
228 |
| Const (@{const_name Pair}, _) => raise SAME () |
|
229 |
| Const (@{const_name fst}, _) $ _ => raise SAME () |
|
230 |
| Const (@{const_name fst}, _) => raise SAME () |
|
231 |
| Const (@{const_name snd}, _) $ _ => raise SAME () |
|
232 |
| Const (@{const_name snd}, _) => raise SAME () |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
233 |
| Const (_, @{typ bool}) => atom_from_formula (to_F Ts t) |
33192 | 234 |
| Free (x as (_, T)) => |
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33980
diff
changeset
|
235 |
Rel (arity_of RRep card T, find_index (curry (op =) x) frees) |
33192 | 236 |
| Term.Var _ => raise NOT_SUPPORTED "schematic variables" |
237 |
| Bound j => raise SAME () |
|
238 |
| Abs (_, T, t') => |
|
239 |
(case fastype_of1 (T :: Ts, t') of |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
240 |
@{typ bool} => Comprehension (decls_for SRep card Ts T, |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
241 |
to_F (T :: Ts) t') |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
242 |
| T' => Comprehension (decls_for SRep card Ts T @ |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
243 |
decls_for RRep card (T :: Ts) T', |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
244 |
Subset (rel_expr_for_bound_var card RRep |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
245 |
(T' :: T :: Ts) 0, |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
246 |
to_R_rep (T :: Ts) t'))) |
33192 | 247 |
| t1 $ t2 => |
248 |
(case fastype_of1 (Ts, t) of |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
249 |
@{typ bool} => atom_from_formula (to_F Ts t) |
33192 | 250 |
| T => |
251 |
let val T2 = fastype_of1 (Ts, t2) in |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
252 |
case arity_of SRep card T2 of |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
253 |
1 => Join (to_S_rep Ts t2, to_R_rep Ts t1) |
33192 | 254 |
| n => |
255 |
let |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
256 |
val arity2 = arity_of SRep card T2 |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
257 |
val res_arity = arity_of RRep card T |
33192 | 258 |
in |
259 |
Project (Intersect |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
260 |
(Product (to_S_rep Ts t2, |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
261 |
atom_schema_of RRep card T |
33192 | 262 |
|> map (AtomSeq o rpair 0) |> foldl1 Product), |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
263 |
to_R_rep Ts t1), |
33192 | 264 |
num_seq arity2 res_arity) |
265 |
end |
|
266 |
end) |
|
267 |
| _ => raise NOT_SUPPORTED ("term " ^ |
|
268 |
quote (Syntax.string_of_term ctxt t))) |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
269 |
handle SAME () => R_rep_from_S_rep (fastype_of1 (Ts, t)) (to_S_rep Ts t) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
270 |
in to_F [] end |
33192 | 271 |
|
272 |
(* (typ -> int) -> int -> styp -> bound *) |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
273 |
fun bound_for_free card i (s, T) = |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
274 |
let val js = atom_schema_of RRep card T in |
33192 | 275 |
([((length js, i), s)], |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
276 |
[TupleSet [], atom_schema_of RRep card T |> map (rpair 0) |
33192 | 277 |
|> tuple_set_from_atom_schema]) |
278 |
end |
|
279 |
||
280 |
(* (typ -> int) -> typ list -> typ -> rel_expr -> formula *) |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
281 |
fun declarative_axiom_for_rel_expr card Ts (Type ("fun", [T1, T2])) r = |
33192 | 282 |
if body_type T2 = bool_T then |
283 |
True |
|
284 |
else |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
285 |
All (decls_for SRep card Ts T1, |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
286 |
declarative_axiom_for_rel_expr card (T1 :: Ts) T2 |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
287 |
(List.foldl Join r (vars_for_bound_var card SRep (T1 :: Ts) 0))) |
33192 | 288 |
| declarative_axiom_for_rel_expr _ _ _ r = One r |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
289 |
(* (typ -> int) -> bool -> int -> styp -> formula *) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
290 |
fun declarative_axiom_for_free card i (_, T) = |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
291 |
declarative_axiom_for_rel_expr card [] T (Rel (arity_of RRep card T, i)) |
33192 | 292 |
|
293 |
(* Proof.context -> (typ -> int) -> term -> string *) |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
294 |
fun pick_nits_in_term ctxt raw_card t = |
33192 | 295 |
let |
296 |
val thy = ProofContext.theory_of ctxt |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
297 |
val {overlord, ...} = Nitpick_Isar.default_params thy [] |
33192 | 298 |
(* typ -> int *) |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
299 |
fun card (Type ("fun", [T1, T2])) = reasonable_power (card T2) (card T1) |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
300 |
| card (Type ("*", [T1, T2])) = card T1 * card T2 |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
301 |
| card @{typ bool} = 2 |
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
302 |
| card T = Int.max (1, raw_card T) |
33192 | 303 |
val neg_t = @{const Not} $ ObjectLogic.atomize_term thy t |
304 |
val _ = fold_types (K o check_type ctxt) neg_t () |
|
305 |
val frees = Term.add_frees neg_t [] |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
306 |
val bounds = map2 (bound_for_free card) (index_seq 0 (length frees)) frees |
33192 | 307 |
val declarative_axioms = |
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
308 |
map2 (declarative_axiom_for_free card) (index_seq 0 (length frees)) frees |
35185
9b8f351cced6
added yet another hint to Nitpick's output, this time warning about problems for which nothing was effectively tested
blanchet
parents:
35028
diff
changeset
|
309 |
val formula = kodkod_formula_from_term ctxt card frees neg_t |
33192 | 310 |
|> fold_rev (curry And) declarative_axioms |
311 |
val univ_card = univ_card 0 0 0 bounds formula |
|
312 |
val problem = |
|
313 |
{comment = "", settings = [], univ_card = univ_card, tuple_assigns = [], |
|
314 |
bounds = bounds, int_bounds = [], expr_assigns = [], formula = formula} |
|
315 |
in |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
316 |
case solve_any_problem overlord NONE 0 1 [problem] of |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
317 |
NotInstalled => "unknown" |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
318 |
| Normal ([], _) => "none" |
33192 | 319 |
| Normal _ => "genuine" |
320 |
| TimedOut _ => "unknown" |
|
321 |
| Interrupted _ => "unknown" |
|
322 |
| Error (s, _) => error ("Kodkod error: " ^ s) |
|
323 |
end |
|
324 |
handle NOT_SUPPORTED details => |
|
325 |
(warning ("Unsupported case: " ^ details ^ "."); "unknown") |
|
33980
a28733ef3a82
export symbols from Minipick (so I can use them in other programs)
blanchet
parents:
33232
diff
changeset
|
326 |
|
33192 | 327 |
end; |