author | bulwahn |
Sun, 01 Aug 2010 10:15:43 +0200 | |
changeset 38114 | 0f06e3cc04a6 |
parent 38113 | 81f08bbb3be7 |
child 38115 | 987edb27f449 |
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 |
|
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
9 |
datatype arith_op = Plus | Minus |
38112
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset
|
10 |
datatype prol_term = Var of string | Cons of string | AppF of string * prol_term list |
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
11 |
| Number of int | ArithOp of arith_op * prol_term list; |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
12 |
datatype prem = Conj of prem list |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
13 |
| Rel of string * prol_term list | NotRel of string * prol_term list |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
14 |
| Eq of prol_term * prol_term | NotEq of prol_term * prol_term |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
15 |
| ArithEq of prol_term * prol_term | NotArithEq of prol_term * prol_term; |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
16 |
|
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
17 |
type clause = ((string * prol_term list) * prem); |
38073 | 18 |
type logic_program = clause list; |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
19 |
type constant_table = (string * string) list |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
20 |
|
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
21 |
val generate : Proof.context -> string list -> (logic_program * constant_table) |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
22 |
val write_program : logic_program -> string |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
23 |
val run : logic_program -> string -> string list -> int option -> prol_term list list |
38073 | 24 |
|
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
25 |
val trace : bool Unsynchronized.ref |
38073 | 26 |
end; |
27 |
||
28 |
structure Code_Prolog : CODE_PROLOG = |
|
29 |
struct |
|
30 |
||
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
31 |
(* diagnostic tracing *) |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
32 |
|
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
33 |
val trace = Unsynchronized.ref false |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
34 |
|
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
35 |
fun tracing s = if !trace then Output.tracing s else () |
38073 | 36 |
(* general string functions *) |
37 |
||
38 |
val first_upper = implode o nth_map 0 Symbol.to_ascii_upper o explode; |
|
39 |
val first_lower = implode o nth_map 0 Symbol.to_ascii_lower o explode; |
|
40 |
||
41 |
(* internal program representation *) |
|
42 |
||
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
43 |
datatype arith_op = Plus | Minus |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
44 |
|
38112
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset
|
45 |
datatype prol_term = Var of string | Cons of string | AppF of string * prol_term list |
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
46 |
| Number of int | ArithOp of arith_op * prol_term list; |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
47 |
|
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
48 |
fun is_Var (Var _) = true |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
49 |
| is_Var _ = false |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
50 |
|
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
51 |
fun is_arith_term (Var _) = true |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
52 |
| is_arith_term (Number _) = true |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
53 |
| is_arith_term (ArithOp (_, operands)) = forall is_arith_term operands |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
54 |
| is_arith_term _ = false |
38073 | 55 |
|
38081
8b02ce312893
removing pointless type information in internal prolog terms
bulwahn
parents:
38080
diff
changeset
|
56 |
fun string_of_prol_term (Var s) = "Var " ^ s |
38075 | 57 |
| string_of_prol_term (Cons s) = "Cons " ^ s |
58 |
| string_of_prol_term (AppF (f, args)) = f ^ "(" ^ commas (map string_of_prol_term args) ^ ")" |
|
38112
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset
|
59 |
| string_of_prol_term (Number n) = "Number " ^ string_of_int n |
38075 | 60 |
|
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
61 |
datatype prem = Conj of prem list |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
62 |
| Rel of string * prol_term list | NotRel of string * prol_term list |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
63 |
| Eq of prol_term * prol_term | NotEq of prol_term * prol_term |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
64 |
| ArithEq of prol_term * prol_term | NotArithEq of prol_term * prol_term; |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
65 |
|
38073 | 66 |
fun dest_Rel (Rel (c, ts)) = (c, ts) |
67 |
||
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
68 |
type clause = ((string * prol_term list) * prem); |
38073 | 69 |
|
70 |
type logic_program = clause list; |
|
71 |
||
72 |
(* translation from introduction rules to internal representation *) |
|
73 |
||
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
74 |
(** constant table **) |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
75 |
|
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
76 |
type constant_table = (string * string) list |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
77 |
|
38073 | 78 |
(* assuming no clashing *) |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
79 |
fun mk_constant_table consts = |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
80 |
AList.make (first_lower o Long_Name.base_name) consts |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
81 |
|
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
82 |
fun declare_consts consts constant_table = |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
83 |
fold (fn c => AList.update (op =) (c, first_lower (Long_Name.base_name c))) consts constant_table |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
84 |
|
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
85 |
fun translate_const constant_table c = |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
86 |
case AList.lookup (op =) constant_table c of |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
87 |
SOME c' => c' |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
88 |
| NONE => error ("No such constant: " ^ c) |
38073 | 89 |
|
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
90 |
fun inv_lookup _ [] _ = NONE |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
91 |
| inv_lookup eq ((key, value)::xs) value' = |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
92 |
if eq (value', value) then SOME key |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
93 |
else inv_lookup eq xs value'; |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
94 |
|
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
95 |
fun restore_const constant_table c = |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
96 |
case inv_lookup (op =) constant_table c of |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
97 |
SOME c' => c' |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
98 |
| NONE => error ("No constant corresponding to " ^ c) |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
99 |
|
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
100 |
(** translation of terms, literals, premises, and clauses **) |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
101 |
|
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
102 |
fun translate_arith_const @{const_name "Groups.plus_class.plus"} = SOME Plus |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
103 |
| translate_arith_const @{const_name "Groups.minus_class.minus"} = SOME Minus |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
104 |
| translate_arith_const _ = NONE |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
105 |
|
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
106 |
fun translate_term ctxt constant_table t = |
38112
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset
|
107 |
case try HOLogic.dest_number t of |
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset
|
108 |
SOME (@{typ "int"}, n) => Number n |
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset
|
109 |
| NONE => |
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset
|
110 |
(case strip_comb t of |
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset
|
111 |
(Free (v, T), []) => Var v |
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset
|
112 |
| (Const (c, _), []) => Cons (translate_const constant_table c) |
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset
|
113 |
| (Const (c, _), args) => |
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
114 |
(case translate_arith_const c of |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
115 |
SOME aop => ArithOp (aop, map (translate_term ctxt constant_table) args) |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
116 |
| NONE => |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
117 |
AppF (translate_const constant_table c, map (translate_term ctxt constant_table) args)) |
38112
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset
|
118 |
| _ => error ("illegal term for translation: " ^ Syntax.string_of_term ctxt t)) |
38073 | 119 |
|
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
120 |
fun translate_literal ctxt constant_table t = |
38073 | 121 |
case strip_comb t of |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
122 |
(Const (@{const_name "op ="}, _), [l, r]) => |
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
123 |
let |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
124 |
val l' = translate_term ctxt constant_table l |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
125 |
val r' = translate_term ctxt constant_table r |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
126 |
in |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
127 |
(if is_Var l' andalso is_arith_term r' then ArithEq else Eq) (l', r') |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
128 |
end |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
129 |
| (Const (c, _), args) => |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
130 |
Rel (translate_const constant_table c, map (translate_term ctxt constant_table) args) |
38073 | 131 |
| _ => error ("illegal literal for translation: " ^ Syntax.string_of_term ctxt t) |
132 |
||
133 |
fun NegRel_of (Rel lit) = NotRel lit |
|
134 |
| NegRel_of (Eq eq) = NotEq eq |
|
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
135 |
| NegRel_of (ArithEq eq) = NotArithEq eq |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
136 |
|
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
137 |
fun translate_prem ctxt constant_table t = |
38073 | 138 |
case try HOLogic.dest_not t of |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
139 |
SOME t => NegRel_of (translate_literal ctxt constant_table t) |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
140 |
| NONE => translate_literal ctxt constant_table t |
38073 | 141 |
|
38114
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset
|
142 |
|
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset
|
143 |
fun imp_prems_conv cv ct = |
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset
|
144 |
case Thm.term_of ct of |
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset
|
145 |
Const ("==>", _) $ _ $ _ => Conv.combination_conv (Conv.arg_conv cv) (imp_prems_conv cv) ct |
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset
|
146 |
| _ => Conv.all_conv ct |
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset
|
147 |
|
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset
|
148 |
fun Trueprop_conv cv ct = |
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset
|
149 |
case Thm.term_of ct of |
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset
|
150 |
Const ("Trueprop", _) $ _ => Conv.arg_conv cv ct |
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset
|
151 |
| _ => raise Fail "Trueprop_conv" |
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset
|
152 |
|
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset
|
153 |
fun preprocess_intro thy rule = |
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset
|
154 |
Conv.fconv_rule |
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset
|
155 |
(imp_prems_conv |
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset
|
156 |
(Trueprop_conv (Conv.try_conv (Conv.rewr_conv @{thm Predicate.eq_is_eq})))) |
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset
|
157 |
(Thm.transfer thy rule) |
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset
|
158 |
|
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
159 |
fun translate_intros ctxt gr const constant_table = |
38073 | 160 |
let |
38114
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset
|
161 |
val intros = map (preprocess_intro (ProofContext.theory_of ctxt)) (Graph.get_node gr const) |
38073 | 162 |
val (intros', ctxt') = Variable.import_terms true (map prop_of intros) ctxt |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
163 |
val constant_table' = declare_consts (fold Term.add_const_names intros' []) constant_table |
38073 | 164 |
fun translate_intro intro = |
165 |
let |
|
166 |
val head = HOLogic.dest_Trueprop (Logic.strip_imp_concl intro) |
|
167 |
val prems = map HOLogic.dest_Trueprop (Logic.strip_imp_prems intro) |
|
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
168 |
val prems' = Conj (map (translate_prem ctxt' constant_table') prems) |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
169 |
val clause = (dest_Rel (translate_literal ctxt' constant_table' head), prems') |
38073 | 170 |
in clause end |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
171 |
in (map translate_intro intros', constant_table') end |
38073 | 172 |
|
173 |
fun generate ctxt const = |
|
174 |
let |
|
175 |
fun strong_conn_of gr keys = |
|
176 |
Graph.strong_conn (Graph.subgraph (member (op =) (Graph.all_succs gr keys)) gr) |
|
177 |
val gr = Predicate_Compile_Core.intros_graph_of ctxt |
|
178 |
val scc = strong_conn_of gr const |
|
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
179 |
val constant_table = mk_constant_table (flat scc) |
38073 | 180 |
in |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
181 |
apfst flat (fold_map (translate_intros ctxt gr) (flat scc) constant_table) |
38073 | 182 |
end |
183 |
||
184 |
(* transform logic program *) |
|
185 |
||
186 |
(** ensure groundness of terms before negation **) |
|
187 |
||
188 |
fun add_vars (Var x) vs = insert (op =) x vs |
|
189 |
| add_vars (Cons c) vs = vs |
|
190 |
| add_vars (AppF (f, args)) vs = fold add_vars args vs |
|
191 |
||
192 |
fun string_of_typ (Type (s, Ts)) = Long_Name.base_name s |
|
193 |
||
194 |
fun mk_groundness_prems ts = |
|
195 |
let |
|
196 |
val vars = fold add_vars ts [] |
|
38081
8b02ce312893
removing pointless type information in internal prolog terms
bulwahn
parents:
38080
diff
changeset
|
197 |
fun mk_ground v = |
8b02ce312893
removing pointless type information in internal prolog terms
bulwahn
parents:
38080
diff
changeset
|
198 |
Rel ("ground", [Var v]) |
38073 | 199 |
in |
200 |
map mk_ground vars |
|
201 |
end |
|
202 |
||
203 |
fun ensure_groundness_prem (NotRel (c, ts)) = Conj (mk_groundness_prems ts @ [NotRel (c, ts)]) |
|
204 |
| ensure_groundness_prem (NotEq (l, r)) = Conj (mk_groundness_prems [l, r] @ [NotEq (l, r)]) |
|
205 |
| ensure_groundness_prem (Conj ps) = Conj (map ensure_groundness_prem ps) |
|
206 |
| ensure_groundness_prem p = p |
|
207 |
||
208 |
fun ensure_groundness_before_negation p = |
|
209 |
map (apsnd ensure_groundness_prem) p |
|
210 |
||
211 |
(* code printer *) |
|
212 |
||
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
213 |
fun write_arith_op Plus = "+" |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
214 |
| write_arith_op Minus = "-" |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
215 |
|
38081
8b02ce312893
removing pointless type information in internal prolog terms
bulwahn
parents:
38080
diff
changeset
|
216 |
fun write_term (Var v) = first_upper v |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
217 |
| write_term (Cons c) = c |
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
218 |
| write_term (AppF (f, args)) = f ^ "(" ^ space_implode ", " (map write_term args) ^ ")" |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
219 |
| write_term (ArithOp (oper, [a1, a2])) = write_term a1 ^ " " ^ write_arith_op oper ^ " " ^ write_term a2 |
38112
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset
|
220 |
| write_term (Number n) = string_of_int n |
38073 | 221 |
|
222 |
fun write_rel (pred, args) = |
|
223 |
pred ^ "(" ^ space_implode ", " (map write_term args) ^ ")" |
|
224 |
||
225 |
fun write_prem (Conj prems) = space_implode ", " (map write_prem prems) |
|
226 |
| write_prem (Rel p) = write_rel p |
|
227 |
| write_prem (NotRel p) = "not(" ^ write_rel p ^ ")" |
|
228 |
| write_prem (Eq (l, r)) = write_term l ^ " = " ^ write_term r |
|
229 |
| write_prem (NotEq (l, r)) = write_term l ^ " \\= " ^ write_term r |
|
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
230 |
| write_prem (ArithEq (l, r)) = write_term l ^ " is " ^ write_term r |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
231 |
| write_prem (NotArithEq (l, r)) = write_term l ^ " =\\= " ^ write_term r |
38073 | 232 |
|
233 |
fun write_clause (head, prem) = |
|
234 |
write_rel head ^ (if prem = Conj [] then "." else " :- " ^ write_prem prem ^ ".") |
|
235 |
||
236 |
fun write_program p = |
|
237 |
cat_lines (map write_clause p) |
|
238 |
||
38078 | 239 |
(** query templates **) |
240 |
||
38073 | 241 |
fun query_first rel vnames = |
242 |
"eval :- once(" ^ rel ^ "(" ^ space_implode ", " vnames ^ ")),\n" ^ |
|
38082 | 243 |
"writef('" ^ space_implode ";" (map (fn v => v ^ " = %w") vnames) ^ |
244 |
"\\n', [" ^ space_implode ", " vnames ^ "]).\n" |
|
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
245 |
|
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
246 |
fun query_firstn n rel vnames = |
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
247 |
"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
|
248 |
rel ^ "(" ^ space_implode ", " vnames ^ "), Sols), writelist(Sols).\n" ^ |
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
249 |
"writelist([]).\n" ^ |
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
250 |
"writelist([(" ^ space_implode ", " vnames ^ ")|T]) :- " ^ |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
251 |
"writef('" ^ space_implode ";" (map (fn v => v ^ " = %w") vnames) ^ |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
252 |
"\\n', [" ^ space_implode ", " vnames ^ "]), writelist(T).\n" |
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
253 |
|
38073 | 254 |
val prelude = |
255 |
"#!/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
|
256 |
":- use_module(library('dialect/ciao/aggregates')).\n" ^ |
38073 | 257 |
":- style_check(-singleton).\n\n" ^ |
258 |
"main :- catch(eval, E, (print_message(error, E), fail)), halt.\n" ^ |
|
259 |
"main :- halt(1).\n" |
|
38075 | 260 |
|
261 |
(* parsing prolog solution *) |
|
262 |
||
263 |
val scan_atom = |
|
38078 | 264 |
Scan.many1 (fn s => Symbol.is_ascii_lower s orelse Symbol.is_ascii_quasi s) |
38075 | 265 |
|
266 |
val scan_var = |
|
38078 | 267 |
Scan.many1 |
268 |
(fn s => Symbol.is_ascii_upper s orelse Symbol.is_ascii_digit s orelse Symbol.is_ascii_quasi s) |
|
38075 | 269 |
|
38076 | 270 |
val scan_ident = |
271 |
Scan.repeat (Scan.one |
|
272 |
(fn s => Symbol.is_ascii_letter s orelse Symbol.is_ascii_digit s orelse Symbol.is_ascii_quasi s)) |
|
273 |
||
38075 | 274 |
fun dest_Char (Symbol.Char s) = s |
275 |
||
276 |
val string_of = concat o map (dest_Char o Symbol.decode) |
|
277 |
||
38076 | 278 |
val is_atom_ident = forall Symbol.is_ascii_lower |
279 |
||
280 |
val is_var_ident = |
|
281 |
forall (fn s => Symbol.is_ascii_upper s orelse Symbol.is_ascii_digit s orelse Symbol.is_ascii_quasi s) |
|
38078 | 282 |
|
283 |
fun scan_terms xs = (((scan_term --| $$ ",") ::: scan_terms) |
|
284 |
|| (scan_term >> single)) xs |
|
285 |
and scan_term xs = |
|
38081
8b02ce312893
removing pointless type information in internal prolog terms
bulwahn
parents:
38080
diff
changeset
|
286 |
((scan_var >> (Var o string_of)) |
38078 | 287 |
|| ((scan_atom -- ($$ "(" |-- scan_terms --| $$ ")")) |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
288 |
>> (fn (f, ts) => AppF (string_of f, ts))) |
38078 | 289 |
|| (scan_atom >> (Cons o string_of))) xs |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
290 |
|
38075 | 291 |
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
|
292 |
(Scan.error (!! (fn _ => raise Fail "parsing prolog output failed")) scan_term) |
38075 | 293 |
o explode |
294 |
||
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
295 |
fun parse_solutions sol = |
38075 | 296 |
let |
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
297 |
fun dest_eq s = case space_explode "=" s of |
38075 | 298 |
(l :: r :: []) => parse_term (unprefix " " r) |
38078 | 299 |
| _ => raise Fail "unexpected equation in prolog output" |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
300 |
fun parse_solution s = map dest_eq (space_explode ";" s) |
38075 | 301 |
in |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
302 |
map parse_solution (fst (split_last (space_explode "\n" sol))) |
38075 | 303 |
end |
38073 | 304 |
|
305 |
(* calling external interpreter and getting results *) |
|
306 |
||
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
307 |
fun run p query_rel vnames nsols = |
38073 | 308 |
let |
309 |
val cmd = Path.named_root |
|
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
310 |
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
|
311 |
val prog = prelude ^ query query_rel vnames ^ write_program p |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
312 |
val _ = tracing ("Generated prolog program:\n" ^ prog) |
38073 | 313 |
val prolog_file = File.tmp_path (Path.basic "prolog_file") |
314 |
val _ = File.write prolog_file prog |
|
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
315 |
val (solution, _) = bash_output ("/usr/local/bin/swipl -f " ^ File.shell_path prolog_file) |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
316 |
val _ = tracing ("Prolog returned solution(s):\n" ^ solution) |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
317 |
val tss = parse_solutions solution |
38073 | 318 |
in |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
319 |
tss |
38073 | 320 |
end |
321 |
||
38075 | 322 |
(* values command *) |
323 |
||
38081
8b02ce312893
removing pointless type information in internal prolog terms
bulwahn
parents:
38080
diff
changeset
|
324 |
fun restore_term ctxt constant_table (Var s, T) = Free (s, T) |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
325 |
| restore_term ctxt constant_table (Cons s, T) = Const (restore_const constant_table s, T) |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
326 |
| restore_term ctxt constant_table (AppF (f, args), T) = |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
327 |
let |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
328 |
val thy = ProofContext.theory_of ctxt |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
329 |
val c = restore_const constant_table f |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
330 |
val cT = Sign.the_const_type thy c |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
331 |
val (argsT, resT) = strip_type cT |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
332 |
val subst = Sign.typ_match thy (resT, T) Vartab.empty |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
333 |
val argsT' = map (Envir.subst_type subst) argsT |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
334 |
in |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
335 |
list_comb (Const (c, Envir.subst_type subst cT), |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
336 |
map (restore_term ctxt constant_table) (args ~~ argsT')) |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
337 |
end |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
338 |
|
38075 | 339 |
fun values ctxt soln t_compr = |
340 |
let |
|
341 |
val split = case t_compr of (Const (@{const_name Collect}, _) $ t) => t |
|
342 |
| _ => error ("Not a set comprehension: " ^ Syntax.string_of_term ctxt t_compr); |
|
343 |
val (body, Ts, fp) = HOLogic.strip_psplits split; |
|
344 |
val output_names = Name.variant_list (Term.add_free_names body []) |
|
345 |
(map (fn i => "x" ^ string_of_int i) (1 upto length Ts)) |
|
38080
8c20eb9a388d
cleaning example file; more natural ordering of variable names
bulwahn
parents:
38079
diff
changeset
|
346 |
val output_frees = rev (map2 (curry Free) output_names Ts) |
38075 | 347 |
val body = subst_bounds (output_frees, body) |
348 |
val (pred as Const (name, T), all_args) = |
|
349 |
case strip_comb body of |
|
350 |
(Const (name, T), all_args) => (Const (name, T), all_args) |
|
351 |
| (head, _) => error ("Not a constant: " ^ Syntax.string_of_term ctxt head) |
|
352 |
val vnames = |
|
353 |
case try (map (fst o dest_Free)) all_args of |
|
354 |
SOME vs => vs |
|
355 |
| NONE => error ("Not only free variables in " ^ commas (map (Syntax.string_of_term ctxt) all_args)) |
|
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
356 |
val _ = tracing "Generating prolog program..." |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
357 |
val (p, constant_table) = generate ctxt [name] |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
358 |
val _ = tracing "Running prolog program..." |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
359 |
val tss = run p (translate_const constant_table name) (map first_upper vnames) soln |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
360 |
val _ = tracing "Restoring terms..." |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
361 |
fun mk_set_comprehension t = |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
362 |
let |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
363 |
val frees = Term.add_frees t [] |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
364 |
val uu as (uuN, uuT) = singleton (Variable.variant_frees ctxt [t]) ("uu", fastype_of t) |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
365 |
in HOLogic.mk_Collect (uuN, uuT, fold (fn (s, T) => fn t => HOLogic.mk_exists (s, T, t)) |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
366 |
frees (HOLogic.mk_conj (HOLogic.mk_eq (Free uu, t), @{term "True"}))) end |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
367 |
val set_comprs = map (fn ts => |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
368 |
mk_set_comprehension (HOLogic.mk_tuple (map (restore_term ctxt constant_table) (ts ~~ Ts)))) tss |
38075 | 369 |
in |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
370 |
foldl1 (HOLogic.mk_binop @{const_name sup}) (set_comprs @ [Free ("...", fastype_of t_compr)]) |
38075 | 371 |
end |
372 |
||
373 |
fun values_cmd print_modes soln raw_t state = |
|
374 |
let |
|
375 |
val ctxt = Toplevel.context_of state |
|
376 |
val t = Syntax.read_term ctxt raw_t |
|
377 |
val t' = values ctxt soln t |
|
378 |
val ty' = Term.type_of t' |
|
379 |
val ctxt' = Variable.auto_fixes t' ctxt |
|
380 |
val p = Print_Mode.with_modes print_modes (fn () => |
|
381 |
Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t'), Pretty.fbrk, |
|
382 |
Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' ty')]) (); |
|
383 |
in Pretty.writeln p end; |
|
384 |
||
385 |
||
386 |
(* renewing the values command for Prolog queries *) |
|
387 |
||
388 |
val opt_print_modes = |
|
389 |
Scan.optional (Parse.$$$ "(" |-- Parse.!!! (Scan.repeat1 Parse.xname --| Parse.$$$ ")")) []; |
|
390 |
||
391 |
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
|
392 |
(opt_print_modes -- Scan.optional (Parse.nat >> SOME) NONE -- Parse.term |
38075 | 393 |
>> (fn ((print_modes, soln), t) => Toplevel.keep |
394 |
(values_cmd print_modes soln t))); |
|
395 |
||
38073 | 396 |
end; |