author | bulwahn |
Tue, 31 Aug 2010 08:00:51 +0200 | |
changeset 38948 | c4e6afaa8dcd |
parent 38947 | 6ed1cffd9d4e |
child 38950 | 62578950e748 |
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 |
|
38792
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
9 |
datatype prolog_system = SWI_PROLOG | YAP |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
10 |
type code_options = |
38947
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
11 |
{ensure_groundness : bool, |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
12 |
limited_types : (typ * int) list, |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
13 |
limited_predicates : (string * int) list, |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
14 |
replacing : ((string * string) * string) list, |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
15 |
prolog_system : prolog_system} |
38948 | 16 |
val options : code_options Unsynchronized.ref |
38727
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
17 |
|
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
18 |
datatype arith_op = Plus | Minus |
38112
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset
|
19 |
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
|
20 |
| Number of int | ArithOp of arith_op * prol_term list; |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
21 |
datatype prem = Conj of prem list |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
22 |
| 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
|
23 |
| 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
|
24 |
| 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
|
25 |
| Ground of string * typ; |
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
26 |
|
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
27 |
type clause = ((string * prol_term list) * prem); |
38073 | 28 |
type logic_program = clause list; |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
29 |
type constant_table = (string * string) list |
38792
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
30 |
|
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
31 |
val generate : bool -> Proof.context -> string -> (logic_program * constant_table) |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
32 |
val write_program : logic_program -> string |
38792
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
33 |
val run : prolog_system -> logic_program -> string -> string list -> int option -> prol_term list list |
38073 | 34 |
|
38733
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
35 |
val quickcheck : Proof.context -> bool -> term -> int -> term list option * (bool list * bool) |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
36 |
|
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
37 |
val trace : bool Unsynchronized.ref |
38947
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
38 |
|
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
39 |
val make_depth_limited : logic_program -> logic_program |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
40 |
val replace : ((string * string) * string) -> logic_program -> logic_program |
38073 | 41 |
end; |
42 |
||
43 |
structure Code_Prolog : CODE_PROLOG = |
|
44 |
struct |
|
45 |
||
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
46 |
(* diagnostic tracing *) |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
47 |
|
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
48 |
val trace = Unsynchronized.ref false |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
49 |
|
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
50 |
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
|
51 |
|
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
52 |
(* code generation options *) |
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
53 |
|
38792
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
54 |
datatype prolog_system = SWI_PROLOG | YAP |
38727
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
55 |
|
38792
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
56 |
type code_options = |
38947
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
57 |
{ensure_groundness : bool, |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
58 |
limited_types : (typ * int) list, |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
59 |
limited_predicates : (string * int) list, |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
60 |
replacing : ((string * string) * string) list, |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
61 |
prolog_system : prolog_system} |
38792
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
62 |
|
38947
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
63 |
val options = Unsynchronized.ref {ensure_groundness = false, |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
64 |
limited_types = [], limited_predicates = [], replacing = [], |
38792
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
65 |
prolog_system = SWI_PROLOG}; |
38727
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
66 |
|
38073 | 67 |
(* general string functions *) |
68 |
||
69 |
val first_upper = implode o nth_map 0 Symbol.to_ascii_upper o explode; |
|
70 |
val first_lower = implode o nth_map 0 Symbol.to_ascii_lower o explode; |
|
71 |
||
72 |
(* internal program representation *) |
|
73 |
||
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
74 |
datatype arith_op = Plus | Minus |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
75 |
|
38112
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset
|
76 |
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
|
77 |
| Number of int | ArithOp of arith_op * prol_term list; |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
78 |
|
38735 | 79 |
fun dest_Var (Var v) = v |
80 |
||
81 |
fun add_vars (Var v) = insert (op =) v |
|
82 |
| add_vars (ArithOp (_, ts)) = fold add_vars ts |
|
83 |
| add_vars (AppF (_, ts)) = fold add_vars ts |
|
84 |
| add_vars _ = I |
|
85 |
||
86 |
fun map_vars f (Var v) = Var (f v) |
|
87 |
| map_vars f (ArithOp (opr, ts)) = ArithOp (opr, map (map_vars f) ts) |
|
88 |
| map_vars f (AppF (fs, ts)) = AppF (fs, map (map_vars f) ts) |
|
89 |
| map_vars f t = t |
|
90 |
||
38728
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
91 |
fun maybe_AppF (c, []) = Cons c |
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
92 |
| maybe_AppF (c, xs) = AppF (c, xs) |
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
93 |
|
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
94 |
fun is_Var (Var _) = true |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
95 |
| is_Var _ = false |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
96 |
|
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
97 |
fun is_arith_term (Var _) = true |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
98 |
| is_arith_term (Number _) = true |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
99 |
| is_arith_term (ArithOp (_, operands)) = forall is_arith_term operands |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
100 |
| is_arith_term _ = false |
38073 | 101 |
|
38081
8b02ce312893
removing pointless type information in internal prolog terms
bulwahn
parents:
38080
diff
changeset
|
102 |
fun string_of_prol_term (Var s) = "Var " ^ s |
38075 | 103 |
| string_of_prol_term (Cons s) = "Cons " ^ s |
104 |
| 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
|
105 |
| string_of_prol_term (Number n) = "Number " ^ string_of_int n |
38075 | 106 |
|
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
107 |
datatype prem = Conj of prem list |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
108 |
| 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
|
109 |
| 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
|
110 |
| 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
|
111 |
| Ground of string * typ; |
38735 | 112 |
|
38073 | 113 |
fun dest_Rel (Rel (c, ts)) = (c, ts) |
38735 | 114 |
|
115 |
fun map_term_prem f (Conj prems) = Conj (map (map_term_prem f) prems) |
|
116 |
| map_term_prem f (Rel (r, ts)) = Rel (r, map f ts) |
|
117 |
| map_term_prem f (NotRel (r, ts)) = NotRel (r, map f ts) |
|
118 |
| map_term_prem f (Eq (l, r)) = Eq (f l, f r) |
|
119 |
| map_term_prem f (NotEq (l, r)) = NotEq (f l, f r) |
|
120 |
| map_term_prem f (ArithEq (l, r)) = ArithEq (f l, f r) |
|
121 |
| map_term_prem f (NotArithEq (l, r)) = NotArithEq (f l, f r) |
|
122 |
| map_term_prem f (Ground (v, T)) = Ground (dest_Var (f (Var v)), T) |
|
123 |
||
124 |
fun fold_prem_terms f (Conj prems) = fold (fold_prem_terms f) prems |
|
125 |
| fold_prem_terms f (Rel (_, ts)) = fold f ts |
|
126 |
| fold_prem_terms f (NotRel (_, ts)) = fold f ts |
|
127 |
| fold_prem_terms f (Eq (l, r)) = f l #> f r |
|
128 |
| fold_prem_terms f (NotEq (l, r)) = f l #> f r |
|
129 |
| fold_prem_terms f (ArithEq (l, r)) = f l #> f r |
|
130 |
| fold_prem_terms f (NotArithEq (l, r)) = f l #> f r |
|
131 |
| fold_prem_terms f (Ground (v, T)) = f (Var v) |
|
132 |
||
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
133 |
type clause = ((string * prol_term list) * prem); |
38073 | 134 |
|
135 |
type logic_program = clause list; |
|
136 |
||
137 |
(* translation from introduction rules to internal representation *) |
|
138 |
||
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
139 |
(** constant table **) |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
140 |
|
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
141 |
type constant_table = (string * string) list |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
142 |
|
38073 | 143 |
(* assuming no clashing *) |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
144 |
fun mk_constant_table consts = |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
145 |
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
|
146 |
|
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
147 |
fun declare_consts consts constant_table = |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
148 |
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
|
149 |
|
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
150 |
fun translate_const constant_table c = |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
151 |
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
|
152 |
SOME c' => c' |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
153 |
| NONE => error ("No such constant: " ^ c) |
38073 | 154 |
|
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
155 |
fun inv_lookup _ [] _ = NONE |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
156 |
| 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
|
157 |
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
|
158 |
else inv_lookup eq xs value'; |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
159 |
|
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
160 |
fun restore_const constant_table c = |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
161 |
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
|
162 |
SOME c' => c' |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
163 |
| NONE => error ("No constant corresponding to " ^ c) |
38727
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
164 |
|
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
165 |
(** 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
|
166 |
|
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
167 |
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
|
168 |
| 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
|
169 |
| translate_arith_const _ = NONE |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
170 |
|
38734
e5508a74b11f
changing hotel trace definition; adding simple handling of numerals on natural numbers
bulwahn
parents:
38733
diff
changeset
|
171 |
fun mk_nat_term constant_table n = |
e5508a74b11f
changing hotel trace definition; adding simple handling of numerals on natural numbers
bulwahn
parents:
38733
diff
changeset
|
172 |
let |
e5508a74b11f
changing hotel trace definition; adding simple handling of numerals on natural numbers
bulwahn
parents:
38733
diff
changeset
|
173 |
val zero = translate_const constant_table @{const_name "Groups.zero_class.zero"} |
e5508a74b11f
changing hotel trace definition; adding simple handling of numerals on natural numbers
bulwahn
parents:
38733
diff
changeset
|
174 |
val Suc = translate_const constant_table @{const_name "Suc"} |
e5508a74b11f
changing hotel trace definition; adding simple handling of numerals on natural numbers
bulwahn
parents:
38733
diff
changeset
|
175 |
in funpow n (fn t => AppF (Suc, [t])) (Cons zero) end |
e5508a74b11f
changing hotel trace definition; adding simple handling of numerals on natural numbers
bulwahn
parents:
38733
diff
changeset
|
176 |
|
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
177 |
fun translate_term ctxt constant_table t = |
38112
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset
|
178 |
case try HOLogic.dest_number t of |
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset
|
179 |
SOME (@{typ "int"}, n) => Number n |
38734
e5508a74b11f
changing hotel trace definition; adding simple handling of numerals on natural numbers
bulwahn
parents:
38733
diff
changeset
|
180 |
| SOME (@{typ "nat"}, n) => mk_nat_term constant_table n |
38112
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset
|
181 |
| NONE => |
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset
|
182 |
(case strip_comb t of |
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset
|
183 |
(Free (v, T), []) => Var v |
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset
|
184 |
| (Const (c, _), []) => Cons (translate_const constant_table c) |
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset
|
185 |
| (Const (c, _), args) => |
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
186 |
(case translate_arith_const c of |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
187 |
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
|
188 |
| NONE => |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
189 |
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
|
190 |
| _ => error ("illegal term for translation: " ^ Syntax.string_of_term ctxt t)) |
38073 | 191 |
|
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
192 |
fun translate_literal ctxt constant_table t = |
38073 | 193 |
case strip_comb t of |
38864
4abe644fcea5
formerly unnamed infix equality now named HOL.eq
haftmann
parents:
38797
diff
changeset
|
194 |
(Const (@{const_name HOL.eq}, _), [l, r]) => |
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
195 |
let |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
196 |
val l' = translate_term ctxt constant_table l |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
197 |
val r' = translate_term ctxt constant_table r |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
198 |
in |
38728
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
199 |
(if is_Var l' andalso is_arith_term r' andalso not (is_Var r') then ArithEq else Eq) (l', r') |
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
200 |
end |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
201 |
| (Const (c, _), args) => |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
202 |
Rel (translate_const constant_table c, map (translate_term ctxt constant_table) args) |
38073 | 203 |
| _ => error ("illegal literal for translation: " ^ Syntax.string_of_term ctxt t) |
204 |
||
205 |
fun NegRel_of (Rel lit) = NotRel lit |
|
206 |
| NegRel_of (Eq eq) = NotEq eq |
|
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
207 |
| NegRel_of (ArithEq eq) = NotArithEq eq |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
208 |
|
38727
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
209 |
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
|
210 |
|
38792
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
211 |
fun translate_prem ensure_groundness ctxt constant_table t = |
38073 | 212 |
case try HOLogic.dest_not t of |
38727
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
213 |
SOME t => |
38792
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
214 |
if ensure_groundness then |
38727
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
215 |
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
|
216 |
else |
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
217 |
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
|
218 |
| 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
|
219 |
|
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset
|
220 |
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
|
221 |
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
|
222 |
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
|
223 |
| _ => 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
|
224 |
|
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset
|
225 |
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
|
226 |
case Thm.term_of ct of |
38558 | 227 |
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
|
228 |
| _ => 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
|
229 |
|
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset
|
230 |
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
|
231 |
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
|
232 |
(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
|
233 |
(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
|
234 |
(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
|
235 |
|
38792
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
236 |
fun translate_intros ensure_groundness ctxt gr const constant_table = |
38073 | 237 |
let |
38114
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset
|
238 |
val intros = map (preprocess_intro (ProofContext.theory_of ctxt)) (Graph.get_node gr const) |
38073 | 239 |
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
|
240 |
val constant_table' = declare_consts (fold Term.add_const_names intros' []) constant_table |
38734
e5508a74b11f
changing hotel trace definition; adding simple handling of numerals on natural numbers
bulwahn
parents:
38733
diff
changeset
|
241 |
|> declare_consts [@{const_name "Groups.zero_class.zero"}, @{const_name "Suc"}] |
38073 | 242 |
fun translate_intro intro = |
243 |
let |
|
244 |
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
|
245 |
val prems = map HOLogic.dest_Trueprop (Logic.strip_imp_prems intro) |
38792
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
246 |
val prems' = Conj (map (translate_prem ensure_groundness ctxt' constant_table') prems) |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
247 |
val clause = (dest_Rel (translate_literal ctxt' constant_table' head), prems') |
38073 | 248 |
in clause end |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
249 |
in (map translate_intro intros', constant_table') end |
38073 | 250 |
|
38731
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
251 |
fun depending_preds_of (key, intros) = |
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
252 |
fold Term.add_const_names (map Thm.prop_of intros) [] |
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
253 |
|
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
254 |
fun add_edges edges_of key G = |
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
255 |
let |
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
256 |
fun extend' key (G, visited) = |
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
257 |
case try (Graph.get_node G) key of |
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
258 |
SOME v => |
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
259 |
let |
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
260 |
val new_edges = filter (fn k => is_some (try (Graph.get_node G) k)) (edges_of (key, v)) |
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
261 |
val (G', visited') = fold extend' |
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
262 |
(subtract (op =) (key :: visited) new_edges) (G, key :: visited) |
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
263 |
in |
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
264 |
(fold (Graph.add_edge o (pair key)) new_edges G', visited') |
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
265 |
end |
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
266 |
| NONE => (G, visited) |
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
267 |
in |
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
268 |
fst (extend' key (G, [])) |
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
269 |
end |
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
270 |
|
38792
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
271 |
fun generate ensure_groundness ctxt const = |
38073 | 272 |
let |
38731
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
273 |
fun strong_conn_of gr keys = |
38073 | 274 |
Graph.strong_conn (Graph.subgraph (member (op =) (Graph.all_succs gr keys)) gr) |
38732
3371dbc806ae
moving preprocessing to values in prolog generation
bulwahn
parents:
38731
diff
changeset
|
275 |
val gr = Predicate_Compile_Core.intros_graph_of ctxt |
38731
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
276 |
val gr' = add_edges depending_preds_of const gr |
2c8a595af43e
invocation of values for prolog execution does not require invocation of code_pred anymore
bulwahn
parents:
38729
diff
changeset
|
277 |
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
|
278 |
val constant_table = mk_constant_table (flat scc) |
38073 | 279 |
in |
38792
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
280 |
apfst flat (fold_map (translate_intros ensure_groundness ctxt gr) (flat scc) constant_table) |
38073 | 281 |
end |
38727
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
282 |
|
38789
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
283 |
(* implementation for fully enumerating predicates and |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
284 |
for size-limited predicates for enumerating the values of a datatype upto a specific size *) |
38073 | 285 |
|
38727
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
286 |
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
|
287 |
| add_ground_typ (Ground (_, T)) = insert (op =) T |
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
288 |
| add_ground_typ _ = I |
38073 | 289 |
|
38728
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
290 |
fun mk_relname (Type (Tcon, Targs)) = |
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
291 |
first_lower (Long_Name.base_name Tcon) ^ space_implode "_" (map mk_relname Targs) |
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
292 |
| mk_relname _ = raise Fail "unexpected type" |
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
293 |
|
38789
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
294 |
fun mk_lim_relname T = "lim_" ^ mk_relname T |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
295 |
|
38728
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
296 |
(* This is copied from "pat_completeness.ML" *) |
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
297 |
fun inst_constrs_of thy (T as Type (name, _)) = |
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
298 |
map (fn (Cn,CT) => |
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
299 |
Envir.subst_term_types (Sign.typ_match thy (body_type CT, T) Vartab.empty) (Const (Cn, CT))) |
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
300 |
(the (Datatype.get_constrs thy name)) |
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
301 |
| inst_constrs_of thy T = raise TYPE ("inst_constrs_of", [T], []) |
38789
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
302 |
|
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
303 |
fun is_recursive_constr T (Const (constr_name, T')) = member (op =) (binder_types T') T |
38728
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
304 |
|
38789
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
305 |
fun mk_ground_impl ctxt limited_types (T as Type (Tcon, Targs)) (seen, constant_table) = |
38728
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
306 |
if member (op =) seen T then ([], (seen, constant_table)) |
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
307 |
else |
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
308 |
let |
38789
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
309 |
val (limited, size) = case AList.lookup (op =) limited_types T of |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
310 |
SOME s => (true, s) |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
311 |
| NONE => (false, 0) |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
312 |
val rel_name = (if limited then mk_lim_relname else mk_relname) T |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
313 |
fun mk_impl (Const (constr_name, cT), recursive) (seen, constant_table) = |
38727
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
314 |
let |
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
315 |
val constant_table' = declare_consts [constr_name] constant_table |
38789
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
316 |
val Ts = binder_types cT |
38728
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
317 |
val (rec_clauses, (seen', constant_table'')) = |
38789
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
318 |
fold_map (mk_ground_impl ctxt limited_types) Ts (seen, constant_table') |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
319 |
val vars = map (fn i => Var ("x" ^ string_of_int i)) (1 upto (length Ts)) |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
320 |
val lim_var = |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
321 |
if limited then |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
322 |
if recursive then [AppF ("suc", [Var "Lim"])] |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
323 |
else [Var "Lim"] |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
324 |
else [] |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
325 |
fun mk_prem v T' = |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
326 |
if limited andalso T' = T then Rel (mk_lim_relname T', [Var "Lim", v]) |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
327 |
else Rel (mk_relname T', [v]) |
38728
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
328 |
val clause = |
38789
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
329 |
((rel_name, lim_var @ [maybe_AppF (translate_const constant_table'' constr_name, vars)]), |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
330 |
Conj (map2 mk_prem vars Ts)) |
38727
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
331 |
in |
38728
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
332 |
(clause :: flat rec_clauses, (seen', constant_table'')) |
38727
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
333 |
end |
38728
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
334 |
val constrs = inst_constrs_of (ProofContext.theory_of ctxt) T |
38789
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
335 |
val constrs' = (constrs ~~ map (is_recursive_constr T) constrs) |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
336 |
|> (fn cs => filter_out snd cs @ filter snd cs) |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
337 |
val (clauses, constant_table') = |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
338 |
apfst flat (fold_map mk_impl constrs' (T :: seen, constant_table)) |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
339 |
val size_term = funpow size (fn t => AppF ("suc", [t])) (Cons "zero") |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
340 |
in |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
341 |
((if limited then |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
342 |
cons ((mk_relname T, [Var "x"]), Rel (mk_lim_relname T, [size_term, Var "x"])) |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
343 |
else I) clauses, constant_table') |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
344 |
end |
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
345 |
| mk_ground_impl ctxt _ T (seen, constant_table) = |
38728
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
346 |
raise Fail ("unexpected type :" ^ Syntax.string_of_typ ctxt T) |
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
347 |
|
38727
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
348 |
fun replace_ground (Conj prems) = Conj (map replace_ground prems) |
38728
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
349 |
| replace_ground (Ground (x, T)) = |
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
350 |
Rel (mk_relname T, [Var x]) |
38727
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
351 |
| replace_ground p = p |
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
352 |
|
38789
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
353 |
fun add_ground_predicates ctxt limited_types (p, constant_table) = |
38727
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
354 |
let |
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
355 |
val ground_typs = fold (add_ground_typ o snd) p [] |
38789
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
356 |
val (grs, (_, constant_table')) = fold_map (mk_ground_impl ctxt limited_types) ground_typs ([], constant_table) |
38727
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
357 |
val p' = map (apsnd replace_ground) p |
38073 | 358 |
in |
38727
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
359 |
((flat grs) @ p', constant_table') |
38073 | 360 |
end |
38789
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
361 |
|
38947
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
362 |
(* make depth-limited version of predicate *) |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
363 |
|
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
364 |
fun mk_lim_rel_name rel_name = "lim_" ^ rel_name |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
365 |
|
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
366 |
fun mk_depth_limited ((rel_name, ts), prem) = |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
367 |
let |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
368 |
fun has_positive_recursive_prems (Conj prems) = exists has_positive_recursive_prems prems |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
369 |
| has_positive_recursive_prems (Rel (rel, ts)) = (rel = rel_name) |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
370 |
| has_positive_recursive_prems _ = false |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
371 |
fun mk_lim_prem (Conj prems) = Conj (map mk_lim_prem prems) |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
372 |
| mk_lim_prem (p as Rel (rel, ts)) = |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
373 |
if rel = rel_name then Rel (mk_lim_rel_name rel, Var "Lim" :: ts) else p |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
374 |
| mk_lim_prem p = p |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
375 |
in |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
376 |
if has_positive_recursive_prems prem then |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
377 |
((mk_lim_rel_name rel_name, (AppF ("suc", [Var "Lim"])) :: ts), mk_lim_prem prem) |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
378 |
else |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
379 |
((mk_lim_rel_name rel_name, (Var "Lim") :: ts), prem) |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
380 |
end |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
381 |
|
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
382 |
fun make_depth_limited clauses = map mk_depth_limited clauses |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
383 |
|
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
384 |
fun add_limited_predicates limited_predicates = |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
385 |
let |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
386 |
fun add (rel_name, limit) (p, constant_table) = |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
387 |
let |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
388 |
val clauses = filter (fn ((rel, args), prems) => rel = rel_name) p |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
389 |
val clauses' = make_depth_limited clauses |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
390 |
val nargs = length (snd (fst (hd clauses))) |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
391 |
val vars = map (fn i => Var ("x" ^ string_of_int i)) (1 upto nargs) |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
392 |
fun nat_term_of n = funpow n (fn t => AppF ("suc", [t])) (Cons "zero") |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
393 |
val entry_clause = |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
394 |
(("limited_" ^ rel_name, vars), Rel ("lim_" ^ rel_name, nat_term_of limit :: vars)) |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
395 |
in (p @ entry_clause :: clauses', constant_table) end |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
396 |
in |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
397 |
fold add limited_predicates |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
398 |
end |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
399 |
|
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
400 |
|
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
401 |
(* replace predicates in clauses *) |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
402 |
|
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
403 |
(* replace (A, B, C) p = replace A by B in clauses of C *) |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
404 |
fun replace ((from, to), location) p = |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
405 |
let |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
406 |
fun replace_prem (Conj prems) = Conj (map replace_prem prems) |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
407 |
| replace_prem (r as Rel (rel, ts)) = |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
408 |
if rel = from then Rel (to, ts) else r |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
409 |
| replace_prem r = r |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
410 |
in |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
411 |
map (fn ((rel, args), prem) => ((rel, args), (if rel = location then replace_prem else I) prem)) p |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
412 |
end |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
413 |
|
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
414 |
|
38735 | 415 |
(* rename variables to prolog-friendly names *) |
416 |
||
417 |
fun rename_vars_term renaming = map_vars (fn v => the (AList.lookup (op =) renaming v)) |
|
418 |
||
419 |
fun rename_vars_prem renaming = map_term_prem (rename_vars_term renaming) |
|
420 |
||
421 |
fun dest_Char (Symbol.Char c) = c |
|
422 |
||
423 |
fun is_prolog_conform v = |
|
424 |
forall (fn s => Symbol.is_ascii_letter s orelse Symbol.is_ascii_digit s) (Symbol.explode v) |
|
425 |
||
426 |
fun mk_conform avoid v = |
|
427 |
let |
|
428 |
val v' = space_implode "" (map (dest_Char o Symbol.decode) |
|
429 |
(filter (fn s => Symbol.is_ascii_letter s orelse Symbol.is_ascii_digit s) |
|
430 |
(Symbol.explode v))) |
|
431 |
val v' = if v' = "" then "var" else v' |
|
432 |
in Name.variant avoid (first_upper v') end |
|
433 |
||
434 |
fun mk_renaming v renaming = |
|
435 |
(v, mk_conform (map snd renaming) v) :: renaming |
|
436 |
||
437 |
fun rename_vars_clause ((rel, args), prem) = |
|
438 |
let |
|
439 |
val vars = fold_prem_terms add_vars prem (fold add_vars args []) |
|
440 |
val renaming = fold mk_renaming vars [] |
|
441 |
in ((rel, map (rename_vars_term renaming) args), rename_vars_prem renaming prem) end |
|
442 |
||
443 |
val rename_vars_program = map rename_vars_clause |
|
444 |
||
38073 | 445 |
(* code printer *) |
446 |
||
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
447 |
fun write_arith_op Plus = "+" |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
448 |
| write_arith_op Minus = "-" |
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
449 |
|
38735 | 450 |
fun write_term (Var v) = v |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
451 |
| write_term (Cons c) = c |
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset
|
452 |
| 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
|
453 |
| 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
|
454 |
| write_term (Number n) = string_of_int n |
38073 | 455 |
|
456 |
fun write_rel (pred, args) = |
|
457 |
pred ^ "(" ^ space_implode ", " (map write_term args) ^ ")" |
|
458 |
||
459 |
fun write_prem (Conj prems) = space_implode ", " (map write_prem prems) |
|
460 |
| write_prem (Rel p) = write_rel p |
|
461 |
| write_prem (NotRel p) = "not(" ^ write_rel p ^ ")" |
|
462 |
| write_prem (Eq (l, r)) = write_term l ^ " = " ^ write_term r |
|
463 |
| 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
|
464 |
| 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
|
465 |
| write_prem (NotArithEq (l, r)) = write_term l ^ " =\\= " ^ write_term r |
38073 | 466 |
|
467 |
fun write_clause (head, prem) = |
|
468 |
write_rel head ^ (if prem = Conj [] then "." else " :- " ^ write_prem prem ^ ".") |
|
469 |
||
470 |
fun write_program p = |
|
471 |
cat_lines (map write_clause p) |
|
472 |
||
38790 | 473 |
(* query templates *) |
38078 | 474 |
|
38792
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
475 |
(** query and prelude for swi-prolog **) |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
476 |
|
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
477 |
fun swi_prolog_query_first rel vnames = |
38073 | 478 |
"eval :- once(" ^ rel ^ "(" ^ space_implode ", " vnames ^ ")),\n" ^ |
38082 | 479 |
"writef('" ^ space_implode ";" (map (fn v => v ^ " = %w") vnames) ^ |
480 |
"\\n', [" ^ space_implode ", " vnames ^ "]).\n" |
|
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
481 |
|
38792
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
482 |
fun swi_prolog_query_firstn n rel vnames = |
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
483 |
"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
|
484 |
rel ^ "(" ^ space_implode ", " vnames ^ "), Sols), writelist(Sols).\n" ^ |
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
485 |
"writelist([]).\n" ^ |
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
486 |
"writelist([(" ^ space_implode ", " vnames ^ ")|T]) :- " ^ |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
487 |
"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
|
488 |
"\\n', [" ^ space_implode ", " vnames ^ "]), writelist(T).\n" |
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
489 |
|
38792
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
490 |
val swi_prolog_prelude = |
38073 | 491 |
"#!/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
|
492 |
":- use_module(library('dialect/ciao/aggregates')).\n" ^ |
38729
9c9d14827380
improving output of set comprehensions; adding style_check flags
bulwahn
parents:
38728
diff
changeset
|
493 |
":- style_check(-singleton).\n" ^ |
9c9d14827380
improving output of set comprehensions; adding style_check flags
bulwahn
parents:
38728
diff
changeset
|
494 |
":- style_check(-discontiguous).\n" ^ |
9c9d14827380
improving output of set comprehensions; adding style_check flags
bulwahn
parents:
38728
diff
changeset
|
495 |
":- style_check(-atom).\n\n" ^ |
38073 | 496 |
"main :- catch(eval, E, (print_message(error, E), fail)), halt.\n" ^ |
497 |
"main :- halt(1).\n" |
|
38075 | 498 |
|
38792
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
499 |
(** query and prelude for yap **) |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
500 |
|
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
501 |
fun yap_query_first rel vnames = |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
502 |
"eval :- once(" ^ rel ^ "(" ^ space_implode ", " vnames ^ ")),\n" ^ |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
503 |
"format('" ^ space_implode ";" (map (fn v => v ^ " = ~w") vnames) ^ |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
504 |
"\\n', [" ^ space_implode ", " vnames ^ "]).\n" |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
505 |
|
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
506 |
val yap_prelude = |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
507 |
"#!/usr/bin/yap -L\n\n" ^ |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
508 |
":- initialization(eval).\n" |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
509 |
|
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
510 |
(* system-dependent query, prelude and invocation *) |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
511 |
|
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
512 |
fun query system nsols = |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
513 |
case system of |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
514 |
SWI_PROLOG => |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
515 |
(case nsols of NONE => swi_prolog_query_first | SOME n => swi_prolog_query_firstn n) |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
516 |
| YAP => |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
517 |
case nsols of NONE => yap_query_first | SOME n => |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
518 |
error "No support for querying multiple solutions in the prolog system yap" |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
519 |
|
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
520 |
fun prelude system = |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
521 |
case system of SWI_PROLOG => swi_prolog_prelude | YAP => yap_prelude |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
522 |
|
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
523 |
fun invoke system file_name = |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
524 |
let |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
525 |
val cmd = |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
526 |
case system of SWI_PROLOG => "/usr/local/bin/swipl -f " | YAP => "/usr/local/bin/yap -L " |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
527 |
in bash_output (cmd ^ file_name) end |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
528 |
|
38075 | 529 |
(* parsing prolog solution *) |
38790 | 530 |
|
38115
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset
|
531 |
val scan_number = |
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset
|
532 |
Scan.many1 Symbol.is_ascii_digit |
38075 | 533 |
|
534 |
val scan_atom = |
|
38728
182b180e9804
improving ensure_groundness in prolog generation; added further example
bulwahn
parents:
38727
diff
changeset
|
535 |
Scan.many1 (fn s => Symbol.is_ascii_lower s orelse Symbol.is_ascii_digit s orelse Symbol.is_ascii_quasi s) |
38075 | 536 |
|
537 |
val scan_var = |
|
38078 | 538 |
Scan.many1 |
539 |
(fn s => Symbol.is_ascii_upper s orelse Symbol.is_ascii_digit s orelse Symbol.is_ascii_quasi s) |
|
38075 | 540 |
|
38076 | 541 |
val scan_ident = |
542 |
Scan.repeat (Scan.one |
|
543 |
(fn s => Symbol.is_ascii_letter s orelse Symbol.is_ascii_digit s orelse Symbol.is_ascii_quasi s)) |
|
544 |
||
38075 | 545 |
fun dest_Char (Symbol.Char s) = s |
546 |
||
547 |
val string_of = concat o map (dest_Char o Symbol.decode) |
|
548 |
||
38076 | 549 |
val is_atom_ident = forall Symbol.is_ascii_lower |
550 |
||
551 |
val is_var_ident = |
|
552 |
forall (fn s => Symbol.is_ascii_upper s orelse Symbol.is_ascii_digit s orelse Symbol.is_ascii_quasi s) |
|
38078 | 553 |
|
38115
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset
|
554 |
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
|
555 |
|
38078 | 556 |
fun scan_terms xs = (((scan_term --| $$ ",") ::: scan_terms) |
557 |
|| (scan_term >> single)) xs |
|
558 |
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
|
559 |
((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
|
560 |
|| (scan_var >> (Var o string_of)) |
38078 | 561 |
|| ((scan_atom -- ($$ "(" |-- scan_terms --| $$ ")")) |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
562 |
>> (fn (f, ts) => AppF (string_of f, ts))) |
38078 | 563 |
|| (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
|
564 |
|
38075 | 565 |
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
|
566 |
(Scan.error (!! (fn _ => raise Fail "parsing prolog output failed")) scan_term) |
38075 | 567 |
o explode |
568 |
||
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
569 |
fun parse_solutions sol = |
38075 | 570 |
let |
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset
|
571 |
fun dest_eq s = case space_explode "=" s of |
38075 | 572 |
(l :: r :: []) => parse_term (unprefix " " r) |
38078 | 573 |
| _ => 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
|
574 |
fun parse_solution s = map dest_eq (space_explode ";" s) |
38075 | 575 |
in |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
576 |
map parse_solution (fst (split_last (space_explode "\n" sol))) |
38075 | 577 |
end |
38073 | 578 |
|
579 |
(* calling external interpreter and getting results *) |
|
580 |
||
38792
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
581 |
fun run system p query_rel vnames nsols = |
38073 | 582 |
let |
583 |
val cmd = Path.named_root |
|
38735 | 584 |
val p' = rename_vars_program p |
585 |
val _ = tracing "Renaming variable names..." |
|
586 |
val renaming = fold mk_renaming vnames [] |
|
587 |
val vnames' = map (fn v => the (AList.lookup (op =) renaming v)) vnames |
|
38792
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
588 |
val prog = prelude system ^ query system nsols 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
|
589 |
val _ = tracing ("Generated prolog program:\n" ^ prog) |
38073 | 590 |
val prolog_file = File.tmp_path (Path.basic "prolog_file") |
591 |
val _ = File.write prolog_file prog |
|
38792
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
592 |
val (solution, _) = invoke system (File.shell_path prolog_file) |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
593 |
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
|
594 |
val tss = parse_solutions solution |
38073 | 595 |
in |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
596 |
tss |
38073 | 597 |
end |
598 |
||
38790 | 599 |
(* restoring types in terms *) |
38075 | 600 |
|
38081
8b02ce312893
removing pointless type information in internal prolog terms
bulwahn
parents:
38080
diff
changeset
|
601 |
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
|
602 |
| 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
|
603 |
| 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
|
604 |
| 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
|
605 |
| 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
|
606 |
let |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
607 |
val thy = ProofContext.theory_of ctxt |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
608 |
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
|
609 |
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
|
610 |
val (argsT, resT) = strip_type cT |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
611 |
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
|
612 |
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
|
613 |
in |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
614 |
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
|
615 |
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
|
616 |
end |
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
617 |
|
38790 | 618 |
(* values command *) |
619 |
||
620 |
val preprocess_options = Predicate_Compile_Aux.Options { |
|
621 |
expected_modes = NONE, |
|
622 |
proposed_modes = NONE, |
|
623 |
proposed_names = [], |
|
624 |
show_steps = false, |
|
625 |
show_intermediate_results = false, |
|
626 |
show_proof_trace = false, |
|
627 |
show_modes = false, |
|
628 |
show_mode_inference = false, |
|
629 |
show_compilation = false, |
|
630 |
show_caught_failures = false, |
|
631 |
skip_proof = true, |
|
632 |
no_topmost_reordering = false, |
|
633 |
function_flattening = true, |
|
634 |
specialise = false, |
|
635 |
fail_safe_function_flattening = false, |
|
636 |
no_higher_order_predicate = [], |
|
637 |
inductify = false, |
|
638 |
detect_switches = true, |
|
639 |
compilation = Predicate_Compile_Aux.Pred |
|
640 |
} |
|
641 |
||
38075 | 642 |
fun values ctxt soln t_compr = |
643 |
let |
|
38727
c7f5f0b7dc7f
adding very basic transformation to ensure groundness before negations
bulwahn
parents:
38558
diff
changeset
|
644 |
val options = !options |
38075 | 645 |
val split = case t_compr of (Const (@{const_name Collect}, _) $ t) => t |
646 |
| _ => error ("Not a set comprehension: " ^ Syntax.string_of_term ctxt t_compr); |
|
647 |
val (body, Ts, fp) = HOLogic.strip_psplits split; |
|
648 |
val output_names = Name.variant_list (Term.add_free_names body []) |
|
649 |
(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
|
650 |
val output_frees = rev (map2 (curry Free) output_names Ts) |
38075 | 651 |
val body = subst_bounds (output_frees, body) |
652 |
val (pred as Const (name, T), all_args) = |
|
653 |
case strip_comb body of |
|
654 |
(Const (name, T), all_args) => (Const (name, T), all_args) |
|
655 |
| (head, _) => error ("Not a constant: " ^ Syntax.string_of_term ctxt head) |
|
656 |
val vnames = |
|
657 |
case try (map (fst o dest_Free)) all_args of |
|
658 |
SOME vs => vs |
|
659 |
| NONE => error ("Not only free variables in " ^ commas (map (Syntax.string_of_term ctxt) all_args)) |
|
38732
3371dbc806ae
moving preprocessing to values in prolog generation
bulwahn
parents:
38731
diff
changeset
|
660 |
val _ = tracing "Preprocessing specification..." |
3371dbc806ae
moving preprocessing to values in prolog generation
bulwahn
parents:
38731
diff
changeset
|
661 |
val T = Sign.the_const_type (ProofContext.theory_of ctxt) name |
3371dbc806ae
moving preprocessing to values in prolog generation
bulwahn
parents:
38731
diff
changeset
|
662 |
val t = Const (name, T) |
38755
a37d39fe32f8
standardized Context.copy_thy to Theory.copy alias, with slightly more direct way of using it;
wenzelm
parents:
38735
diff
changeset
|
663 |
val thy' = |
a37d39fe32f8
standardized Context.copy_thy to Theory.copy alias, with slightly more direct way of using it;
wenzelm
parents:
38735
diff
changeset
|
664 |
Theory.copy (ProofContext.theory_of ctxt) |
a37d39fe32f8
standardized Context.copy_thy to Theory.copy alias, with slightly more direct way of using it;
wenzelm
parents:
38735
diff
changeset
|
665 |
|> Predicate_Compile.preprocess preprocess_options t |
a37d39fe32f8
standardized Context.copy_thy to Theory.copy alias, with slightly more direct way of using it;
wenzelm
parents:
38735
diff
changeset
|
666 |
val ctxt' = ProofContext.init_global thy' |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
667 |
val _ = tracing "Generating prolog program..." |
38797
abe92b33ac9f
merged, resolving some minor conflicts in src/HOL/Tools/Predicate_Compile/code_prolog.ML;
wenzelm
diff
changeset
|
668 |
val (p, constant_table) = generate (#ensure_groundness options) ctxt' name |
38789
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
669 |
|> (if #ensure_groundness options then |
38797
abe92b33ac9f
merged, resolving some minor conflicts in src/HOL/Tools/Predicate_Compile/code_prolog.ML;
wenzelm
diff
changeset
|
670 |
add_ground_predicates ctxt' (#limited_types options) |
38789
d171840881fd
added generation of predicates for size-limited enumeration of values
bulwahn
parents:
38735
diff
changeset
|
671 |
else I) |
38947
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
672 |
|> add_limited_predicates (#limited_predicates options) |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
673 |
|> apfst (fold replace (#replacing options)) |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
674 |
val _ = tracing "Running prolog program..." |
38792
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
675 |
val tss = run (#prolog_system options) |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
676 |
p (translate_const constant_table name) (map first_upper vnames) soln |
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset
|
677 |
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
|
678 |
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
|
679 |
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
|
680 |
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
|
681 |
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
|
682 |
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
|
683 |
(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
|
684 |
| 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
|
685 |
let |
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset
|
686 |
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
|
687 |
in |
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset
|
688 |
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
|
689 |
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
|
690 |
else |
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset
|
691 |
let |
38755
a37d39fe32f8
standardized Context.copy_thy to Theory.copy alias, with slightly more direct way of using it;
wenzelm
parents:
38735
diff
changeset
|
692 |
val uu as (uuN, uuT) = singleton (Variable.variant_frees ctxt' [t]) ("uu", fastype_of t) |
38115
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset
|
693 |
val set_compr = |
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset
|
694 |
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
|
695 |
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
|
696 |
in |
38729
9c9d14827380
improving output of set comprehensions; adding style_check flags
bulwahn
parents:
38728
diff
changeset
|
697 |
mk_set_compr [] ts |
9c9d14827380
improving output of set comprehensions; adding style_check flags
bulwahn
parents:
38728
diff
changeset
|
698 |
(set_compr :: (if null in_insert then xs else (fold mk_insert in_insert empty) :: xs)) |
38115
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset
|
699 |
end |
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset
|
700 |
end |
38075 | 701 |
in |
38115
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset
|
702 |
foldl1 (HOLogic.mk_binop @{const_name sup}) (mk_set_compr [] |
38755
a37d39fe32f8
standardized Context.copy_thy to Theory.copy alias, with slightly more direct way of using it;
wenzelm
parents:
38735
diff
changeset
|
703 |
(map (fn ts => HOLogic.mk_tuple (map (restore_term ctxt' constant_table) (ts ~~ Ts))) tss) []) |
38075 | 704 |
end |
705 |
||
706 |
fun values_cmd print_modes soln raw_t state = |
|
707 |
let |
|
708 |
val ctxt = Toplevel.context_of state |
|
709 |
val t = Syntax.read_term ctxt raw_t |
|
710 |
val t' = values ctxt soln t |
|
711 |
val ty' = Term.type_of t' |
|
712 |
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
|
713 |
val _ = tracing "Printing terms..." |
38075 | 714 |
val p = Print_Mode.with_modes print_modes (fn () => |
715 |
Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t'), Pretty.fbrk, |
|
716 |
Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' ty')]) (); |
|
717 |
in Pretty.writeln p end; |
|
718 |
||
719 |
||
720 |
(* renewing the values command for Prolog queries *) |
|
721 |
||
722 |
val opt_print_modes = |
|
723 |
Scan.optional (Parse.$$$ "(" |-- Parse.!!! (Scan.repeat1 Parse.xname --| Parse.$$$ ")")) []; |
|
724 |
||
725 |
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
|
726 |
(opt_print_modes -- Scan.optional (Parse.nat >> SOME) NONE -- Parse.term |
38075 | 727 |
>> (fn ((print_modes, soln), t) => Toplevel.keep |
38504
76965c356d2a
removed Code_Prolog: modifies global environment setup non-conservatively
haftmann
parents:
38115
diff
changeset
|
728 |
(values_cmd print_modes soln t))); (*FIXME does not preserve the previous functionality*) |
38075 | 729 |
|
38733
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
730 |
(* quickcheck generator *) |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
731 |
|
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
732 |
(* FIXME: large copy of Predicate_Compile_Quickcheck - refactor out commons *) |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
733 |
|
38786
e46e7a9cb622
formerly unnamed infix impliciation now named HOL.implies
haftmann
parents:
38735
diff
changeset
|
734 |
fun strip_imp_prems (Const(@{const_name HOL.implies}, _) $ A $ B) = A :: strip_imp_prems B |
38733
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
735 |
| strip_imp_prems _ = []; |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
736 |
|
38786
e46e7a9cb622
formerly unnamed infix impliciation now named HOL.implies
haftmann
parents:
38735
diff
changeset
|
737 |
fun strip_imp_concl (Const(@{const_name HOL.implies}, _) $ A $ B) = strip_imp_concl B |
38733
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
738 |
| strip_imp_concl A = A : term; |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
739 |
|
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
740 |
fun strip_horn A = (strip_imp_prems A, strip_imp_concl A); |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
741 |
|
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
742 |
fun quickcheck ctxt report t size = |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
743 |
let |
38755
a37d39fe32f8
standardized Context.copy_thy to Theory.copy alias, with slightly more direct way of using it;
wenzelm
parents:
38735
diff
changeset
|
744 |
val thy = Theory.copy (ProofContext.theory_of ctxt) |
38733
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
745 |
val (vs, t') = strip_abs t |
38755
a37d39fe32f8
standardized Context.copy_thy to Theory.copy alias, with slightly more direct way of using it;
wenzelm
parents:
38735
diff
changeset
|
746 |
val vs' = Variable.variant_frees ctxt [] vs |
38733
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
747 |
val Ts = map snd vs' |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
748 |
val t'' = subst_bounds (map Free (rev vs'), t') |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
749 |
val (prems, concl) = strip_horn t'' |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
750 |
val constname = "quickcheck" |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
751 |
val full_constname = Sign.full_bname thy constname |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
752 |
val constT = Ts ---> @{typ bool} |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
753 |
val thy1 = Sign.add_consts_i [(Binding.name constname, constT, NoSyn)] thy |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
754 |
val const = Const (full_constname, constT) |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
755 |
val t = Logic.list_implies |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
756 |
(map HOLogic.mk_Trueprop (prems @ [HOLogic.mk_not concl]), |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
757 |
HOLogic.mk_Trueprop (list_comb (Const (full_constname, constT), map Free vs'))) |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
758 |
val tac = fn _ => Skip_Proof.cheat_tac thy1 |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
759 |
val intro = Goal.prove (ProofContext.init_global thy1) (map fst vs') [] t tac |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
760 |
val thy2 = Context.theory_map (Predicate_Compile_Alternative_Defs.add_thm intro) thy1 |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
761 |
val thy3 = Predicate_Compile.preprocess preprocess_options const thy2 |
38755
a37d39fe32f8
standardized Context.copy_thy to Theory.copy alias, with slightly more direct way of using it;
wenzelm
parents:
38735
diff
changeset
|
762 |
val ctxt' = ProofContext.init_global thy3 |
38733
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
763 |
val _ = tracing "Generating prolog program..." |
38797
abe92b33ac9f
merged, resolving some minor conflicts in src/HOL/Tools/Predicate_Compile/code_prolog.ML;
wenzelm
diff
changeset
|
764 |
val (p, constant_table) = generate true ctxt' full_constname |
abe92b33ac9f
merged, resolving some minor conflicts in src/HOL/Tools/Predicate_Compile/code_prolog.ML;
wenzelm
diff
changeset
|
765 |
|> add_ground_predicates ctxt' (#limited_types (!options)) |
38947
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
766 |
|> add_limited_predicates (#limited_predicates (!options)) |
6ed1cffd9d4e
added quite adhoc logic program transformations limited_predicates and replacements of predicates
bulwahn
parents:
38864
diff
changeset
|
767 |
|> apfst (fold replace (#replacing (!options))) |
38733
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
768 |
val _ = tracing "Running prolog program..." |
38792
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
769 |
val [ts] = run (#prolog_system (!options)) |
970508a5119f
added support for yet another prolog system (yap); generate has only one option ensure_groundness; added one example of yap invocation in example theory
bulwahn
parents:
38790
diff
changeset
|
770 |
p (translate_const constant_table full_constname) (map fst vs') (SOME 1) |
38733
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
771 |
val _ = tracing "Restoring terms..." |
38755
a37d39fe32f8
standardized Context.copy_thy to Theory.copy alias, with slightly more direct way of using it;
wenzelm
parents:
38735
diff
changeset
|
772 |
val res = SOME (map (restore_term ctxt' constant_table) (ts ~~ Ts)) |
38733
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
773 |
val empty_report = ([], false) |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
774 |
in |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
775 |
(res, empty_report) |
4b8fd91ea59a
added quickcheck generator for prolog generation; first example of counterexample search with prolog for hotel key card system
bulwahn
parents:
38732
diff
changeset
|
776 |
end; |
38732
3371dbc806ae
moving preprocessing to values in prolog generation
bulwahn
parents:
38731
diff
changeset
|
777 |
|
38073 | 778 |
end; |