author | bulwahn |
Thu, 29 Jul 2010 17:27:56 +0200 | |
changeset 38077 | 46ff55ace18c |
parent 38076 | 2a9c14d9d2ef |
child 38078 | 2afb5f710b84 |
permissions | -rw-r--r-- |
38073 | 1 |
(* Title: HOL/Tools/Predicate_Compile/code_prolog.ML |
2 |
Author: Lukas Bulwahn, TU Muenchen |
|
3 |
||
4 |
Prototype of an code generator for logic programming languages (a.k.a. Prolog) |
|
5 |
*) |
|
6 |
||
7 |
signature CODE_PROLOG = |
|
8 |
sig |
|
9 |
datatype term = Var of string * typ | Cons of string | AppF of string * term list; |
|
10 |
datatype prem = Conj of prem list | NotRel of string * term list | Rel of string * term list | Eq of term * term | NotEq of term * term; |
|
11 |
type clause = ((string * term list) * prem); |
|
12 |
type logic_program = clause list; |
|
13 |
||
14 |
val generate : Proof.context -> string list -> logic_program |
|
15 |
val write_program : logic_program -> string |
|
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
16 |
val run : logic_program -> string -> string list -> int option -> term list |
38073 | 17 |
|
18 |
end; |
|
19 |
||
20 |
structure Code_Prolog : CODE_PROLOG = |
|
21 |
struct |
|
22 |
||
23 |
(* general string functions *) |
|
24 |
||
25 |
val first_upper = implode o nth_map 0 Symbol.to_ascii_upper o explode; |
|
26 |
val first_lower = implode o nth_map 0 Symbol.to_ascii_lower o explode; |
|
27 |
||
28 |
(* internal program representation *) |
|
29 |
||
30 |
datatype term = Var of string * typ | Cons of string | AppF of string * term list; |
|
31 |
||
38075 | 32 |
fun string_of_prol_term (Var (s, T)) = "Var " ^ s |
33 |
| string_of_prol_term (Cons s) = "Cons " ^ s |
|
34 |
| string_of_prol_term (AppF (f, args)) = f ^ "(" ^ commas (map string_of_prol_term args) ^ ")" |
|
35 |
||
38073 | 36 |
datatype prem = Conj of prem list | NotRel of string * term list | Rel of string * term list | Eq of term * term | NotEq of term * term; |
37 |
||
38 |
fun dest_Rel (Rel (c, ts)) = (c, ts) |
|
39 |
||
40 |
type clause = ((string * term list) * prem); |
|
41 |
||
42 |
type logic_program = clause list; |
|
43 |
||
44 |
(* translation from introduction rules to internal representation *) |
|
45 |
||
46 |
(* assuming no clashing *) |
|
47 |
fun translate_const c = Long_Name.base_name c |
|
48 |
||
49 |
fun translate_term ctxt t = |
|
50 |
case strip_comb t of |
|
51 |
(Free (v, T), []) => Var (v, T) |
|
52 |
| (Const (c, _), []) => Cons (translate_const c) |
|
53 |
| (Const (c, _), args) => AppF (translate_const c, map (translate_term ctxt) args) |
|
54 |
| _ => error ("illegal term for translation: " ^ Syntax.string_of_term ctxt t) |
|
55 |
||
56 |
||
57 |
fun translate_literal ctxt t = |
|
58 |
case strip_comb t of |
|
59 |
(Const (@{const_name "op ="}, _), [l, r]) => Eq (pairself (translate_term ctxt) (l, r)) |
|
60 |
| (Const (c, _), args) => Rel (translate_const c, map (translate_term ctxt) args) |
|
61 |
| _ => error ("illegal literal for translation: " ^ Syntax.string_of_term ctxt t) |
|
62 |
||
63 |
fun NegRel_of (Rel lit) = NotRel lit |
|
64 |
| NegRel_of (Eq eq) = NotEq eq |
|
65 |
||
66 |
fun translate_prem ctxt t = |
|
67 |
case try HOLogic.dest_not t of |
|
68 |
SOME t => NegRel_of (translate_literal ctxt t) |
|
69 |
| NONE => translate_literal ctxt t |
|
70 |
||
71 |
fun translate_intros ctxt gr const = |
|
72 |
let |
|
73 |
val intros = Graph.get_node gr const |
|
74 |
val (intros', ctxt') = Variable.import_terms true (map prop_of intros) ctxt |
|
75 |
fun translate_intro intro = |
|
76 |
let |
|
77 |
val head = HOLogic.dest_Trueprop (Logic.strip_imp_concl intro) |
|
78 |
val prems = map HOLogic.dest_Trueprop (Logic.strip_imp_prems intro) |
|
79 |
val prems' = Conj (map (translate_prem ctxt') prems) |
|
80 |
val clause = (dest_Rel (translate_literal ctxt' head), prems') |
|
81 |
in clause end |
|
82 |
in map translate_intro intros' end |
|
83 |
||
84 |
fun generate ctxt const = |
|
85 |
let |
|
86 |
fun strong_conn_of gr keys = |
|
87 |
Graph.strong_conn (Graph.subgraph (member (op =) (Graph.all_succs gr keys)) gr) |
|
88 |
val gr = Predicate_Compile_Core.intros_graph_of ctxt |
|
89 |
val scc = strong_conn_of gr const |
|
90 |
in |
|
91 |
maps (translate_intros ctxt gr) (flat scc) |
|
92 |
end |
|
93 |
||
94 |
(* transform logic program *) |
|
95 |
||
96 |
(** ensure groundness of terms before negation **) |
|
97 |
||
98 |
fun add_vars (Var x) vs = insert (op =) x vs |
|
99 |
| add_vars (Cons c) vs = vs |
|
100 |
| add_vars (AppF (f, args)) vs = fold add_vars args vs |
|
101 |
||
102 |
fun string_of_typ (Type (s, Ts)) = Long_Name.base_name s |
|
103 |
||
104 |
fun mk_groundness_prems ts = |
|
105 |
let |
|
106 |
val vars = fold add_vars ts [] |
|
107 |
fun mk_ground (v, T) = |
|
108 |
Rel ("ground_" ^ string_of_typ T, [Var (v, T)]) |
|
109 |
in |
|
110 |
map mk_ground vars |
|
111 |
end |
|
112 |
||
113 |
fun ensure_groundness_prem (NotRel (c, ts)) = Conj (mk_groundness_prems ts @ [NotRel (c, ts)]) |
|
114 |
| ensure_groundness_prem (NotEq (l, r)) = Conj (mk_groundness_prems [l, r] @ [NotEq (l, r)]) |
|
115 |
| ensure_groundness_prem (Conj ps) = Conj (map ensure_groundness_prem ps) |
|
116 |
| ensure_groundness_prem p = p |
|
117 |
||
118 |
fun ensure_groundness_before_negation p = |
|
119 |
map (apsnd ensure_groundness_prem) p |
|
120 |
||
121 |
(* code printer *) |
|
122 |
||
123 |
fun write_term (Var (v, _)) = first_upper v |
|
124 |
| write_term (Cons c) = first_lower c |
|
125 |
| write_term (AppF (f, args)) = first_lower f ^ "(" ^ space_implode ", " (map write_term args) ^ ")" |
|
126 |
||
127 |
fun write_rel (pred, args) = |
|
128 |
pred ^ "(" ^ space_implode ", " (map write_term args) ^ ")" |
|
129 |
||
130 |
fun write_prem (Conj prems) = space_implode ", " (map write_prem prems) |
|
131 |
| write_prem (Rel p) = write_rel p |
|
132 |
| write_prem (NotRel p) = "not(" ^ write_rel p ^ ")" |
|
133 |
| write_prem (Eq (l, r)) = write_term l ^ " = " ^ write_term r |
|
134 |
| write_prem (NotEq (l, r)) = write_term l ^ " \\= " ^ write_term r |
|
135 |
||
136 |
fun write_clause (head, prem) = |
|
137 |
write_rel head ^ (if prem = Conj [] then "." else " :- " ^ write_prem prem ^ ".") |
|
138 |
||
139 |
fun write_program p = |
|
140 |
cat_lines (map write_clause p) |
|
141 |
||
142 |
fun query_first rel vnames = |
|
143 |
"eval :- once(" ^ rel ^ "(" ^ space_implode ", " vnames ^ ")),\n" ^ |
|
144 |
"writef('" ^ implode (map (fn v => v ^ " = %w\\n") vnames) ^"', [" ^ space_implode ", " vnames ^ "]).\n" |
|
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
145 |
|
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
146 |
fun query_firstn n rel vnames = |
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
147 |
"eval :- findnsols(" ^ string_of_int n ^ ", (" ^ space_implode ", " vnames ^ "), " ^ |
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
148 |
rel ^ "(" ^ space_implode ", " vnames ^ "), Sols), writelist(Sols).\n" ^ |
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
149 |
"writelist([]).\n" ^ |
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
150 |
"writelist([(" ^ space_implode ", " vnames ^ ")|T]) :- " ^ |
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
151 |
"writef('" ^ implode (map (fn v => v ^ " = %w\\n") vnames) ^ |
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
152 |
"', [" ^ space_implode ", " vnames ^ "]), writelist(T).\n" |
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
153 |
|
38073 | 154 |
val prelude = |
155 |
"#!/usr/bin/swipl -q -t main -f\n\n" ^ |
|
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
156 |
":- use_module(library('dialect/ciao/aggregates')).\n" ^ |
38073 | 157 |
":- style_check(-singleton).\n\n" ^ |
158 |
"main :- catch(eval, E, (print_message(error, E), fail)), halt.\n" ^ |
|
159 |
"main :- halt(1).\n" |
|
38075 | 160 |
|
161 |
(* parsing prolog solution *) |
|
162 |
||
163 |
val scan_atom = |
|
164 |
Scan.repeat (Scan.one (fn s => Symbol.is_ascii_lower s orelse Symbol.is_ascii_quasi s)) |
|
165 |
||
166 |
val scan_var = |
|
167 |
Scan.repeat (Scan.one |
|
168 |
(fn s => Symbol.is_ascii_upper s orelse Symbol.is_ascii_digit s orelse Symbol.is_ascii_quasi s)) |
|
169 |
||
38076 | 170 |
val scan_ident = |
171 |
Scan.repeat (Scan.one |
|
172 |
(fn s => Symbol.is_ascii_letter s orelse Symbol.is_ascii_digit s orelse Symbol.is_ascii_quasi s)) |
|
173 |
||
38075 | 174 |
fun dest_Char (Symbol.Char s) = s |
175 |
||
176 |
val string_of = concat o map (dest_Char o Symbol.decode) |
|
177 |
||
38076 | 178 |
val is_atom_ident = forall Symbol.is_ascii_lower |
179 |
||
180 |
val is_var_ident = |
|
181 |
forall (fn s => Symbol.is_ascii_upper s orelse Symbol.is_ascii_digit s orelse Symbol.is_ascii_quasi s) |
|
182 |
||
38075 | 183 |
val scan_term = |
38076 | 184 |
scan_ident >> (fn s => |
185 |
if is_var_ident s then (Var (string_of s, dummyT)) |
|
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
186 |
else if is_atom_ident s then |
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
187 |
else Cons (string_of s) |
38076 | 188 |
else raise Fail "unexpected") |
189 |
||
38075 | 190 |
val parse_term = fst o Scan.finite Symbol.stopper |
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
191 |
(Scan.error (!! (fn _ => raise Fail "parsing prolog output failed")) scan_term) |
38075 | 192 |
o explode |
193 |
||
194 |
fun parse_solution sol = |
|
195 |
let |
|
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
196 |
fun dest_eq s = case space_explode "=" s of |
38075 | 197 |
(l :: r :: []) => parse_term (unprefix " " r) |
198 |
| _ => raise Fail "unexpected equation in prolog output") |
|
199 |
in |
|
200 |
map dest_eq (fst (split_last (space_explode "\n" sol))) |
|
201 |
end |
|
38073 | 202 |
|
203 |
(* calling external interpreter and getting results *) |
|
204 |
||
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
205 |
fun run p query_rel vnames nsols = |
38073 | 206 |
let |
207 |
val cmd = Path.named_root |
|
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
208 |
val query = case nsols of NONE => query_first | SOME n => query_firstn n |
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
209 |
val prog = prelude ^ query query_rel vnames ^ write_program p |
38073 | 210 |
val prolog_file = File.tmp_path (Path.basic "prolog_file") |
211 |
val _ = File.write prolog_file prog |
|
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
212 |
val (solution, _) = bash_output ("/usr/local/bin/swipl -f " ^ File.shell_path prolog_file) |
38075 | 213 |
val ts = parse_solution solution |
38073 | 214 |
in |
38075 | 215 |
ts |
38073 | 216 |
end |
217 |
||
38075 | 218 |
(* values command *) |
219 |
||
220 |
fun mk_term (Var (s, T)) = Free (s, T) |
|
221 |
| mk_term (Cons s) = Const (s, dummyT) |
|
222 |
| mk_term (AppF (f, args)) = list_comb (Const (f, dummyT), map mk_term args) |
|
223 |
||
224 |
fun values ctxt soln t_compr = |
|
225 |
let |
|
226 |
val split = case t_compr of (Const (@{const_name Collect}, _) $ t) => t |
|
227 |
| _ => error ("Not a set comprehension: " ^ Syntax.string_of_term ctxt t_compr); |
|
228 |
val (body, Ts, fp) = HOLogic.strip_psplits split; |
|
229 |
val output_names = Name.variant_list (Term.add_free_names body []) |
|
230 |
(map (fn i => "x" ^ string_of_int i) (1 upto length Ts)) |
|
231 |
val output_frees = map2 (curry Free) output_names (rev Ts) |
|
232 |
val body = subst_bounds (output_frees, body) |
|
233 |
val (pred as Const (name, T), all_args) = |
|
234 |
case strip_comb body of |
|
235 |
(Const (name, T), all_args) => (Const (name, T), all_args) |
|
236 |
| (head, _) => error ("Not a constant: " ^ Syntax.string_of_term ctxt head) |
|
237 |
val vnames = |
|
238 |
case try (map (fst o dest_Free)) all_args of |
|
239 |
SOME vs => vs |
|
240 |
| NONE => error ("Not only free variables in " ^ commas (map (Syntax.string_of_term ctxt) all_args)) |
|
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
241 |
val ts = run (generate ctxt [name]) (translate_const name) (map first_upper vnames) soln |
38075 | 242 |
in |
243 |
HOLogic.mk_tuple (map mk_term ts) |
|
244 |
end |
|
245 |
||
246 |
fun values_cmd print_modes soln raw_t state = |
|
247 |
let |
|
248 |
val ctxt = Toplevel.context_of state |
|
249 |
val t = Syntax.read_term ctxt raw_t |
|
250 |
val t' = values ctxt soln t |
|
251 |
val ty' = Term.type_of t' |
|
252 |
val ctxt' = Variable.auto_fixes t' ctxt |
|
253 |
val p = Print_Mode.with_modes print_modes (fn () => |
|
254 |
Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t'), Pretty.fbrk, |
|
255 |
Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' ty')]) (); |
|
256 |
in Pretty.writeln p end; |
|
257 |
||
258 |
||
259 |
(* renewing the values command for Prolog queries *) |
|
260 |
||
261 |
val opt_print_modes = |
|
262 |
Scan.optional (Parse.$$$ "(" |-- Parse.!!! (Scan.repeat1 Parse.xname --| Parse.$$$ ")")) []; |
|
263 |
||
264 |
val _ = Outer_Syntax.improper_command "values" "enumerate and print comprehensions" Keyword.diag |
|
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
265 |
(opt_print_modes -- Scan.optional (Parse.nat >> SOME) NONE -- Parse.term |
38075 | 266 |
>> (fn ((print_modes, soln), t) => Toplevel.keep |
267 |
(values_cmd print_modes soln t))); |
|
268 |
||
38073 | 269 |
end; |