author | wenzelm |
Tue, 22 Jul 2025 12:02:53 +0200 | |
changeset 82894 | a8e47bd31965 |
parent 69593 | 3dda49e08b9d |
permissions | -rw-r--r-- |
66646 | 1 |
(* Title: HOL/Tools/Nunchaku/nunchaku_translate.ML |
66614
1f1c5d85d232
moved Nunchaku to Main; the goal is to move Nitpick out in the next 1-2 years
blanchet
parents:
64411
diff
changeset
|
2 |
Author: Jasmin Blanchette, VU Amsterdam |
1f1c5d85d232
moved Nunchaku to Main; the goal is to move Nitpick out in the next 1-2 years
blanchet
parents:
64411
diff
changeset
|
3 |
Copyright 2015, 2016, 2017 |
64389 | 4 |
|
5 |
Translation of Isabelle/HOL problems to Nunchaku. |
|
6 |
*) |
|
7 |
||
8 |
signature NUNCHAKU_TRANSLATE = |
|
9 |
sig |
|
10 |
type isa_problem = Nunchaku_Collect.isa_problem |
|
11 |
type ty = Nunchaku_Problem.ty |
|
12 |
type nun_problem = Nunchaku_Problem.nun_problem |
|
13 |
||
14 |
val flip_quote: string -> string |
|
15 |
val lowlevel_str_of_ty: ty -> string |
|
16 |
||
17 |
val nun_problem_of_isa: Proof.context -> isa_problem -> nun_problem |
|
18 |
end; |
|
19 |
||
20 |
structure Nunchaku_Translate : NUNCHAKU_TRANSLATE = |
|
21 |
struct |
|
22 |
||
23 |
open Nunchaku_Util; |
|
24 |
open Nunchaku_Collect; |
|
25 |
open Nunchaku_Problem; |
|
26 |
||
27 |
fun flip_quote s = |
|
28 |
(case try (unprefix "'") s of |
|
29 |
SOME s' => s' |
|
30 |
| NONE => prefix "'" s); |
|
31 |
||
32 |
fun lowlevel_str_of_ty (NType (id, tys)) = |
|
33 |
(if null tys then "" else encode_args (map lowlevel_str_of_ty tys)) ^ id; |
|
34 |
||
35 |
fun strip_nun_abs 0 tm = ([], tm) |
|
36 |
| strip_nun_abs n (NAbs (var, body)) = |
|
37 |
strip_nun_abs (n - 1) body |
|
38 |
|>> cons var; |
|
39 |
||
40 |
val strip_nun_comb = |
|
41 |
let |
|
42 |
fun strip args (NApp (func, arg)) = strip (arg :: args) func |
|
43 |
| strip args tm = (tm, args); |
|
44 |
in |
|
45 |
strip [] |
|
46 |
end; |
|
47 |
||
48 |
fun ty_of_isa (Type (s, Ts)) = |
|
49 |
let val tys = map ty_of_isa Ts in |
|
50 |
(case s of |
|
69593 | 51 |
\<^type_name>\<open>bool\<close> => prop_ty |
52 |
| \<^type_name>\<open>fun\<close> => NType (nun_arrow, tys) |
|
64389 | 53 |
| _ => |
54 |
let |
|
55 |
val args = map lowlevel_str_of_ty tys; |
|
56 |
val id = nun_tconst_of_str args s; |
|
57 |
in |
|
58 |
NType (id, []) |
|
59 |
end) |
|
60 |
end |
|
61 |
| ty_of_isa (TFree (s, _)) = NType (nun_tfree_of_str (flip_quote s), []) |
|
62 |
| ty_of_isa (TVar _) = raise Fail "unexpected TVar"; |
|
63 |
||
64 |
fun gen_tm_of_isa in_prop ctxt t = |
|
65 |
let |
|
66 |
val thy = Proof_Context.theory_of ctxt; |
|
67 |
||
68 |
fun id_of_const (x as (s, _)) = |
|
69 |
let val args = map (lowlevel_str_of_ty o ty_of_isa) (Sign.const_typargs thy x) in |
|
70 |
nun_const_of_str args s |
|
71 |
end; |
|
72 |
||
73 |
fun tm_of_branch ctr_id var_count f_arg_tm = |
|
74 |
let val (vars, body) = strip_nun_abs var_count f_arg_tm in |
|
75 |
(ctr_id, vars, body) |
|
76 |
end; |
|
77 |
||
78 |
fun tm_of bounds (Const (x as (s, T))) = |
|
79 |
(case try (dest_co_datatype_case ctxt) x of |
|
80 |
SOME ctrs => |
|
81 |
let |
|
82 |
val num_f_args = length ctrs; |
|
83 |
val min_args = num_f_args + 1; |
|
84 |
val var_counts = map (num_binder_types o snd) ctrs; |
|
85 |
||
86 |
val dummy_free = Free (Name.uu, T); |
|
87 |
val tm = tm_of bounds dummy_free; |
|
88 |
val tm' = eta_expandN_tm min_args tm; |
|
89 |
val (vars, body) = strip_nun_abs min_args tm'; |
|
90 |
val (_, (f_args, obj :: other_args)) = strip_nun_comb body ||> chop num_f_args; |
|
91 |
val f_args' = map2 eta_expandN_tm var_counts f_args; |
|
92 |
||
93 |
val ctr_ids = map id_of_const ctrs; |
|
94 |
in |
|
95 |
NMatch (obj, @{map 3} tm_of_branch ctr_ids var_counts f_args') |
|
96 |
|> rcomb_tms other_args |
|
97 |
|> abs_tms vars |
|
98 |
end |
|
99 |
| NONE => |
|
69593 | 100 |
if s = \<^const_name>\<open>unreachable\<close> andalso in_prop then |
64389 | 101 |
let val ty = ty_of_isa T in |
102 |
napps (NConst (nun_asserting, [ty], mk_arrows_ty ([ty, prop_ty], ty)), |
|
103 |
[NConst (id_of_const x, [], ty), NConst (nun_false, [], prop_ty)]) |
|
104 |
end |
|
105 |
else |
|
106 |
let |
|
107 |
val id = |
|
108 |
(case s of |
|
69593 | 109 |
\<^const_name>\<open>All\<close> => nun_forall |
110 |
| \<^const_name>\<open>conj\<close> => nun_conj |
|
111 |
| \<^const_name>\<open>disj\<close> => nun_disj |
|
112 |
| \<^const_name>\<open>HOL.eq\<close> => nun_equals |
|
113 |
| \<^const_name>\<open>Eps\<close> => nun_choice |
|
114 |
| \<^const_name>\<open>Ex\<close> => nun_exists |
|
115 |
| \<^const_name>\<open>False\<close> => nun_false |
|
116 |
| \<^const_name>\<open>If\<close> => nun_if |
|
117 |
| \<^const_name>\<open>implies\<close> => nun_implies |
|
118 |
| \<^const_name>\<open>Not\<close> => nun_not |
|
119 |
| \<^const_name>\<open>The\<close> => nun_unique |
|
120 |
| \<^const_name>\<open>The_unsafe\<close> => nun_unique_unsafe |
|
121 |
| \<^const_name>\<open>True\<close> => nun_true |
|
64389 | 122 |
| _ => id_of_const x); |
123 |
in |
|
124 |
NConst (id, [], ty_of_isa T) |
|
125 |
end) |
|
126 |
| tm_of _ (Free (s, T)) = NConst (nun_free_of_str s, [], ty_of_isa T) |
|
127 |
| tm_of _ (Var ((s, _), T)) = NConst (nun_var_of_str s, [], ty_of_isa T) |
|
128 |
| tm_of bounds (Abs (s, T, t)) = |
|
129 |
let |
|
130 |
val (s', bounds') = Name.variant s bounds; |
|
131 |
val x = Var ((s', 0), T); |
|
132 |
in |
|
133 |
NAbs (tm_of bounds' x, tm_of bounds' (subst_bound (x, t))) |
|
134 |
end |
|
135 |
| tm_of bounds (t $ u) = NApp (tm_of bounds t, tm_of bounds u) |
|
136 |
| tm_of _ (Bound _) = raise Fail "unexpected Bound"; |
|
137 |
in |
|
138 |
t |
|
139 |
|> tm_of Name.context |
|
140 |
|> eta_expand_builtin_tm |
|
141 |
end; |
|
142 |
||
143 |
val tm_of_isa = gen_tm_of_isa false; |
|
144 |
val prop_of_isa = gen_tm_of_isa true; |
|
145 |
||
64411
0af9926e1303
adapted Nunchaku's input syntax to new design decisions
blanchet
parents:
64389
diff
changeset
|
146 |
fun nun_copy_spec_of_isa_typedef ctxt {abs_typ, rep_typ, wrt, abs, rep} = |
0af9926e1303
adapted Nunchaku's input syntax to new design decisions
blanchet
parents:
64389
diff
changeset
|
147 |
{abs_ty = ty_of_isa abs_typ, rep_ty = ty_of_isa rep_typ, subset = SOME (tm_of_isa ctxt wrt), |
0af9926e1303
adapted Nunchaku's input syntax to new design decisions
blanchet
parents:
64389
diff
changeset
|
148 |
quotient = NONE, abs = tm_of_isa ctxt abs, rep = tm_of_isa ctxt rep}; |
0af9926e1303
adapted Nunchaku's input syntax to new design decisions
blanchet
parents:
64389
diff
changeset
|
149 |
|
0af9926e1303
adapted Nunchaku's input syntax to new design decisions
blanchet
parents:
64389
diff
changeset
|
150 |
fun nun_copy_spec_of_isa_quotient ctxt {abs_typ, rep_typ, wrt, abs, rep} = |
0af9926e1303
adapted Nunchaku's input syntax to new design decisions
blanchet
parents:
64389
diff
changeset
|
151 |
{abs_ty = ty_of_isa abs_typ, rep_ty = ty_of_isa rep_typ, subset = NONE, |
0af9926e1303
adapted Nunchaku's input syntax to new design decisions
blanchet
parents:
64389
diff
changeset
|
152 |
quotient = SOME (tm_of_isa ctxt wrt), abs = tm_of_isa ctxt abs, rep = tm_of_isa ctxt rep}; |
64389 | 153 |
|
154 |
fun nun_ctr_of_isa ctxt ctr = |
|
155 |
{ctr = tm_of_isa ctxt ctr, arg_tys = map ty_of_isa (binder_types (fastype_of ctr))}; |
|
156 |
||
157 |
fun nun_co_data_spec_of_isa ctxt {typ, ctrs} = |
|
158 |
{ty = ty_of_isa typ, ctrs = map (nun_ctr_of_isa ctxt) ctrs}; |
|
159 |
||
160 |
fun nun_const_spec_of_isa ctxt {const, props} = |
|
161 |
{const = tm_of_isa ctxt const, props = map (prop_of_isa ctxt) props}; |
|
162 |
||
163 |
fun nun_rec_spec_of_isa ctxt {const, props, ...} = |
|
164 |
{const = tm_of_isa ctxt const, props = map (prop_of_isa ctxt) props}; |
|
165 |
||
166 |
fun nun_consts_spec_of_isa ctxt {consts, props} = |
|
167 |
{consts = map (tm_of_isa ctxt) consts, props = map (prop_of_isa ctxt) props}; |
|
168 |
||
169 |
fun nun_problem_of_isa ctxt {commandss, sound, complete} = |
|
170 |
let |
|
171 |
fun cmd_of cmd = |
|
172 |
(case cmd of |
|
173 |
ITVal (T, cards) => NTVal (ty_of_isa T, cards) |
|
64411
0af9926e1303
adapted Nunchaku's input syntax to new design decisions
blanchet
parents:
64389
diff
changeset
|
174 |
| ITypedef spec => NCopy (nun_copy_spec_of_isa_typedef ctxt spec) |
0af9926e1303
adapted Nunchaku's input syntax to new design decisions
blanchet
parents:
64389
diff
changeset
|
175 |
| IQuotient spec => NCopy (nun_copy_spec_of_isa_quotient ctxt spec) |
64389 | 176 |
| ICoData (fp, specs) => |
177 |
BNF_Util.case_fp fp NData NCodata (map (nun_co_data_spec_of_isa ctxt) specs) |
|
178 |
| IVal t => NVal (tm_of_isa ctxt t, ty_of_isa (fastype_of t)) |
|
179 |
| ICoPred (fp, wf, specs) => |
|
180 |
(if wf then curry NPred true |
|
181 |
else if fp = BNF_Util.Least_FP then curry NPred false |
|
182 |
else NCopred) (map (nun_const_spec_of_isa ctxt) specs) |
|
183 |
| IRec specs => NRec (map (nun_rec_spec_of_isa ctxt) specs) |
|
184 |
| ISpec spec => NSpec (nun_consts_spec_of_isa ctxt spec) |
|
185 |
| IAxiom prop => NAxiom (prop_of_isa ctxt prop) |
|
186 |
| IGoal prop => NGoal (prop_of_isa ctxt prop) |
|
187 |
| IEval t => NEval (tm_of_isa ctxt t)); |
|
188 |
in |
|
189 |
{commandss = map (map cmd_of) commandss, sound = sound, complete = complete} |
|
190 |
end; |
|
191 |
||
192 |
end; |