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