| author | wenzelm |
| Fri, 02 May 2014 20:07:55 +0200 | |
| changeset 56832 | 93f05fa757dd |
| parent 56090 | 34bd10a9a2ad |
| permissions | -rw-r--r-- |
|
56088
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
1 |
(* Title: HOL/Tools/SMT2/z3_new_model.ML |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
2 |
Author: Sascha Boehme and Philipp Meyer, TU Muenchen |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
3 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
4 |
Parser for counterexamples generated by Z3. |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
5 |
*) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
6 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
7 |
signature Z3_NEW_MODEL = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
8 |
sig |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
9 |
val parse_counterex: Proof.context -> SMT2_Translate.replay_data -> string list -> |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
10 |
term list * term list |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
11 |
end |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
12 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
13 |
structure Z3_New_Model: Z3_NEW_MODEL = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
14 |
struct |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
15 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
16 |
(* counterexample expressions *) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
17 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
18 |
datatype expr = True | False | Number of int * int option | Value of int | |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
19 |
Array of array | App of string * expr list |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
20 |
and array = Fresh of expr | Store of (array * expr) * expr |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
21 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
22 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
23 |
(* parsing *) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
24 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
25 |
val space = Scan.many Symbol.is_ascii_blank |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
26 |
fun spaced p = p --| space |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
27 |
fun in_parens p = spaced (Scan.$$ "(") |-- p --| spaced (Scan.$$ ")")
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
28 |
fun in_braces p = spaced (Scan.$$ "{") |-- p --| spaced (Scan.$$ "}")
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
29 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
30 |
val digit = (fn |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
31 |
"0" => SOME 0 | "1" => SOME 1 | "2" => SOME 2 | "3" => SOME 3 | |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
32 |
"4" => SOME 4 | "5" => SOME 5 | "6" => SOME 6 | "7" => SOME 7 | |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
33 |
"8" => SOME 8 | "9" => SOME 9 | _ => NONE) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
34 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
35 |
val nat_num = spaced (Scan.repeat1 (Scan.some digit) >> |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
36 |
(fn ds => fold (fn d => fn i => i * 10 + d) ds 0)) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
37 |
val int_num = spaced (Scan.optional ($$ "-" >> K (fn i => ~i)) I :|-- |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
38 |
(fn sign => nat_num >> sign)) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
39 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
40 |
val is_char = Symbol.is_ascii_letter orf Symbol.is_ascii_digit orf |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
41 |
member (op =) (raw_explode "_+*-/%~=<>$&|?!.@^#") |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
42 |
val name = spaced (Scan.many1 is_char >> implode) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
43 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
44 |
fun $$$ s = spaced (Scan.this_string s) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
45 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
46 |
fun array_expr st = st |> in_parens ( |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
47 |
$$$ "const" |-- expr >> Fresh || |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
48 |
$$$ "store" |-- array_expr -- expr -- expr >> Store) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
49 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
50 |
and expr st = st |> ( |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
51 |
$$$ "true" >> K True || |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
52 |
$$$ "false" >> K False || |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
53 |
int_num -- Scan.option ($$$ "/" |-- int_num) >> Number || |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
54 |
$$$ "val!" |-- nat_num >> Value || |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
55 |
name >> (App o rpair []) || |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
56 |
array_expr >> Array || |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
57 |
in_parens (name -- Scan.repeat1 expr) >> App) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
58 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
59 |
fun args st = ($$$ "->" >> K [] || expr ::: args) st |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
60 |
val args_case = args -- expr |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
61 |
val else_case = $$$ "else" -- $$$ "->" |-- expr >> pair ([] : expr list) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
62 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
63 |
val func = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
64 |
let fun cases st = (else_case >> single || args_case ::: cases) st |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
65 |
in in_braces cases end |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
66 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
67 |
val cex = space |-- |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
68 |
Scan.repeat (name --| $$$ "->" -- (func || expr >> (single o pair []))) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
69 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
70 |
fun resolve terms ((n, k), cases) = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
71 |
(case Symtab.lookup terms n of |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
72 |
NONE => NONE |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
73 |
| SOME t => SOME ((t, k), cases)) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
74 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
75 |
fun annotate _ (_, []) = NONE |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
76 |
| annotate terms (n, [([], c)]) = resolve terms ((n, 0), (c, [])) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
77 |
| annotate _ (_, [_]) = NONE |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
78 |
| annotate terms (n, cases as (args, _) :: _) = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
79 |
let val (cases', (_, else_case)) = split_last cases |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
80 |
in resolve terms ((n, length args), (else_case, cases')) end |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
81 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
82 |
fun read_cex terms ls = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
83 |
maps (cons "\n" o raw_explode) ls |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
84 |
|> try (fst o Scan.finite Symbol.stopper cex) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
85 |
|> the_default [] |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
86 |
|> map_filter (annotate terms) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
87 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
88 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
89 |
(* translation into terms *) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
90 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
91 |
fun max_value vs = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
92 |
let |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
93 |
fun max_val_expr (Value i) = Integer.max i |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
94 |
| max_val_expr (App (_, es)) = fold max_val_expr es |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
95 |
| max_val_expr (Array a) = max_val_array a |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
96 |
| max_val_expr _ = I |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
97 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
98 |
and max_val_array (Fresh e) = max_val_expr e |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
99 |
| max_val_array (Store ((a, e1), e2)) = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
100 |
max_val_array a #> max_val_expr e1 #> max_val_expr e2 |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
101 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
102 |
fun max_val (_, (ec, cs)) = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
103 |
max_val_expr ec #> fold (fn (es, e) => fold max_val_expr (e :: es)) cs |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
104 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
105 |
in fold max_val vs ~1 end |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
106 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
107 |
fun with_context terms f vs = fst (fold_map f vs (terms, max_value vs + 1)) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
108 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
109 |
fun get_term n T es (cx as (terms, next_val)) = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
110 |
(case Symtab.lookup terms n of |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
111 |
SOME t => ((t, es), cx) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
112 |
| NONE => |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
113 |
let val t = Var (("skolem", next_val), T)
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
114 |
in ((t, []), (Symtab.update (n, t) terms, next_val + 1)) end) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
115 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
116 |
fun trans_expr _ True = pair @{const True}
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
117 |
| trans_expr _ False = pair @{const False}
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
118 |
| trans_expr T (Number (i, NONE)) = pair (HOLogic.mk_number T i) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
119 |
| trans_expr T (Number (i, SOME j)) = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
120 |
pair (Const (@{const_name divide}, [T, T] ---> T) $
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
121 |
HOLogic.mk_number T i $ HOLogic.mk_number T j) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
122 |
| trans_expr T (Value i) = pair (Var (("value", i), T))
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
123 |
| trans_expr T (Array a) = trans_array T a |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
124 |
| trans_expr T (App (n, es)) = get_term n T es #-> (fn (t, es') => |
| 56090 | 125 |
let val Ts = fst (SMT2_Util.dest_funT (length es') (Term.fastype_of t)) |
126 |
in fold_map (uncurry trans_expr) (Ts ~~ es') #>> Term.list_comb o pair t end) |
|
|
56088
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
127 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
128 |
and trans_array T a = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
129 |
let val (dT, rT) = Term.dest_funT T |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
130 |
in |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
131 |
(case a of |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
132 |
Fresh e => trans_expr rT e #>> (fn t => Abs ("x", dT, t))
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
133 |
| Store ((a', e1), e2) => |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
134 |
trans_array T a' ##>> trans_expr dT e1 ##>> trans_expr rT e2 #>> |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
135 |
(fn ((m, k), v) => |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
136 |
Const (@{const_name fun_upd}, [T, dT, rT] ---> T) $ m $ k $ v))
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
137 |
end |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
138 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
139 |
fun trans_pattern T ([], e) = trans_expr T e #>> pair [] |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
140 |
| trans_pattern T (arg :: args, e) = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
141 |
trans_expr (Term.domain_type T) arg ##>> |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
142 |
trans_pattern (Term.range_type T) (args, e) #>> |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
143 |
(fn (arg', (args', e')) => (arg' :: args', e')) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
144 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
145 |
fun mk_fun_upd T U = Const (@{const_name fun_upd}, [T --> U, T, U, T] ---> U)
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
146 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
147 |
fun mk_update ([], u) _ = u |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
148 |
| mk_update ([t], u) f = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
149 |
uncurry mk_fun_upd (Term.dest_funT (Term.fastype_of f)) $ f $ t $ u |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
150 |
| mk_update (t :: ts, u) f = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
151 |
let |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
152 |
val (dT, rT) = Term.dest_funT (Term.fastype_of f) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
153 |
val (dT', rT') = Term.dest_funT rT |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
154 |
in |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
155 |
mk_fun_upd dT rT $ f $ t $ |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
156 |
mk_update (ts, u) (absdummy dT' (Const ("_", rT')))
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
157 |
end |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
158 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
159 |
fun mk_lambda Ts (t, pats) = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
160 |
fold_rev absdummy Ts t |> fold mk_update pats |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
161 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
162 |
fun translate ((t, k), (e, cs)) = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
163 |
let |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
164 |
val T = Term.fastype_of t |
| 56090 | 165 |
val (Us, U) = SMT2_Util.dest_funT k (Term.fastype_of t) |
|
56088
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
166 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
167 |
fun mk_full_def u' pats = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
168 |
pats |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
169 |
|> filter_out (fn (_, u) => u aconv u') |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
170 |
|> HOLogic.mk_eq o pair t o mk_lambda Us o pair u' |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
171 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
172 |
fun mk_eq (us, u) = HOLogic.mk_eq (Term.list_comb (t, us), u) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
173 |
fun mk_eqs u' [] = [HOLogic.mk_eq (t, u')] |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
174 |
| mk_eqs _ pats = map mk_eq pats |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
175 |
in |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
176 |
trans_expr U e ##>> |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
177 |
(if k = 0 then pair [] else fold_map (trans_pattern T) cs) #>> |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
178 |
(fn (u', pats) => (mk_eqs u' pats, mk_full_def u' pats)) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
179 |
end |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
180 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
181 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
182 |
(* normalization *) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
183 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
184 |
fun partition_eqs f = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
185 |
let |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
186 |
fun part t (xs, ts) = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
187 |
(case try HOLogic.dest_eq t of |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
188 |
SOME (l, r) => (case f l r of SOME x => (x::xs, ts) | _ => (xs, t::ts)) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
189 |
| NONE => (xs, t :: ts)) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
190 |
in (fn ts => fold part ts ([], [])) end |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
191 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
192 |
fun first_eq pred = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
193 |
let |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
194 |
fun part _ [] = NONE |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
195 |
| part us (t :: ts) = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
196 |
(case try (pred o HOLogic.dest_eq) t of |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
197 |
SOME (SOME lr) => SOME (lr, fold cons us ts) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
198 |
| _ => part (t :: us) ts) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
199 |
in (fn ts => part [] ts) end |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
200 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
201 |
fun replace_vars tab = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
202 |
let |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
203 |
fun repl v = the_default v (AList.lookup (op aconv) tab v) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
204 |
fun replace (v as Var _) = repl v |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
205 |
| replace (v as Free _) = repl v |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
206 |
| replace t = t |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
207 |
in map (Term.map_aterms replace) end |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
208 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
209 |
fun remove_int_nat_coercions (eqs, defs) = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
210 |
let |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
211 |
fun mk_nat_num t i = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
212 |
(case try HOLogic.dest_number i of |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
213 |
SOME (_, n) => SOME (t, HOLogic.mk_number @{typ nat} n)
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
214 |
| NONE => NONE) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
215 |
fun nat_of (@{const of_nat (int)} $ (t as Var _)) i = mk_nat_num t i
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
216 |
| nat_of (@{const nat} $ i) (t as Var _) = mk_nat_num t i
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
217 |
| nat_of _ _ = NONE |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
218 |
val (nats, eqs') = partition_eqs nat_of eqs |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
219 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
220 |
fun is_coercion t = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
221 |
(case try HOLogic.dest_eq t of |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
222 |
SOME (@{const of_nat (int)}, _) => true
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
223 |
| SOME (@{const nat}, _) => true
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
224 |
| _ => false) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
225 |
in pairself (replace_vars nats) (eqs', filter_out is_coercion defs) end |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
226 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
227 |
fun unfold_funapp (eqs, defs) = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
228 |
let |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
229 |
fun unfold_app (Const (@{const_name SMT2.fun_app}, _) $ f $ t) = f $ t
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
230 |
| unfold_app t = t |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
231 |
fun unfold_eq ((eq as Const (@{const_name HOL.eq}, _)) $ t $ u) =
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
232 |
eq $ unfold_app t $ u |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
233 |
| unfold_eq t = t |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
234 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
235 |
fun is_fun_app t = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
236 |
(case try HOLogic.dest_eq t of |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
237 |
SOME (Const (@{const_name SMT2.fun_app}, _), _) => true
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
238 |
| _ => false) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
239 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
240 |
in (map unfold_eq eqs, filter_out is_fun_app defs) end |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
241 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
242 |
val unfold_eqs = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
243 |
let |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
244 |
val is_ground = not o Term.exists_subterm Term.is_Var |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
245 |
fun is_non_rec (v, t) = not (Term.exists_subterm (equal v) t) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
246 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
247 |
fun rewr_var (l as Var _, r) = if is_ground r then SOME (l, r) else NONE |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
248 |
| rewr_var (r, l as Var _) = if is_ground r then SOME (l, r) else NONE |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
249 |
| rewr_var _ = NONE |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
250 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
251 |
fun rewr_free' e = if is_non_rec e then SOME e else NONE |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
252 |
fun rewr_free (e as (Free _, _)) = rewr_free' e |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
253 |
| rewr_free (e as (_, Free _)) = rewr_free' (swap e) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
254 |
| rewr_free _ = NONE |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
255 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
256 |
fun is_trivial (Const (@{const_name HOL.eq}, _) $ t $ u) = t aconv u
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
257 |
| is_trivial _ = false |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
258 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
259 |
fun replace r = replace_vars [r] #> filter_out is_trivial |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
260 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
261 |
fun unfold_vars (es, ds) = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
262 |
(case first_eq rewr_var es of |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
263 |
SOME (lr, es') => unfold_vars (pairself (replace lr) (es', ds)) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
264 |
| NONE => (es, ds)) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
265 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
266 |
fun unfold_frees ues (es, ds) = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
267 |
(case first_eq rewr_free es of |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
268 |
SOME (lr, es') => |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
269 |
pairself (replace lr) (es', ds) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
270 |
|> unfold_frees (HOLogic.mk_eq lr :: replace lr ues) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
271 |
| NONE => (ues @ es, ds)) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
272 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
273 |
in unfold_vars #> unfold_frees [] end |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
274 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
275 |
fun swap_free ((eq as Const (@{const_name HOL.eq}, _)) $ t $ (u as Free _)) =
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
276 |
eq $ u $ t |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
277 |
| swap_free t = t |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
278 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
279 |
fun frees_for_vars ctxt (eqs, defs) = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
280 |
let |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
281 |
fun fresh_free i T (cx as (frees, ctxt)) = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
282 |
(case Inttab.lookup frees i of |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
283 |
SOME t => (t, cx) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
284 |
| NONE => |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
285 |
let |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
286 |
val (n, ctxt') = yield_singleton Variable.variant_fixes "" ctxt |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
287 |
val t = Free (n, T) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
288 |
in (t, (Inttab.update (i, t) frees, ctxt')) end) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
289 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
290 |
fun repl_var (Var ((_, i), T)) = fresh_free i T |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
291 |
| repl_var (t $ u) = repl_var t ##>> repl_var u #>> op $ |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
292 |
| repl_var (Abs (n, T, t)) = repl_var t #>> (fn t' => Abs (n, T, t')) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
293 |
| repl_var t = pair t |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
294 |
in |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
295 |
(Inttab.empty, ctxt) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
296 |
|> fold_map repl_var eqs |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
297 |
||>> fold_map repl_var defs |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
298 |
|> fst |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
299 |
end |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
300 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
301 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
302 |
(* overall procedure *) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
303 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
304 |
val is_free_constraint = Term.exists_subterm (fn Free _ => true | _ => false) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
305 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
306 |
fun is_free_def (Const (@{const_name HOL.eq}, _) $ Free _ $ _) = true
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
307 |
| is_free_def _ = false |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
308 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
309 |
fun defined tp = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
310 |
try (pairself (fst o HOLogic.dest_eq)) tp |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
311 |
|> the_default false o Option.map (op aconv) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
312 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
313 |
fun add_free_defs free_cs defs = |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
314 |
let val (free_defs, defs') = List.partition is_free_def defs |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
315 |
in (free_cs @ filter_out (member defined free_cs) free_defs, defs') end |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
316 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
317 |
fun is_const_def (Const (@{const_name HOL.eq}, _) $ Const _ $ _) = true
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
318 |
| is_const_def _ = false |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
319 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
320 |
(* TODO: Adapt parser to SMT-LIB 2 format for models *) |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
321 |
fun parse_counterex ctxt ({terms, ...} : SMT2_Translate.replay_data) ls =
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
322 |
read_cex terms ls |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
323 |
|> with_context terms translate |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
324 |
|> apfst flat o split_list |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
325 |
|> remove_int_nat_coercions |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
326 |
|> unfold_funapp |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
327 |
|> unfold_eqs |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
328 |
|>> map swap_free |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
329 |
|>> filter is_free_constraint |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
330 |
|-> add_free_defs |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
331 |
|> frees_for_vars ctxt |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
332 |
||> filter is_const_def |
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
333 |
|
|
db61a0a62b2c
reintroduced old model reconstruction code -- still needs to be ported
blanchet
parents:
diff
changeset
|
334 |
end |