author  haftmann 
Thu, 19 Aug 2010 11:02:14 +0200  
changeset 38549  d0385f2764d8 
parent 38504  76965c356d2a 
child 38558  32ad17fe2b9c 
permissions  rwrr 
38073  1 
(* Title: HOL/Tools/Predicate_Compile/code_prolog.ML 
2 
Author: Lukas Bulwahn, TU Muenchen 

3 

4 
Prototype of an code generator for logic programming languages (a.k.a. Prolog) 

5 
*) 

6 

7 
signature CODE_PROLOG = 

8 
sig 

38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

9 
datatype arith_op = Plus  Minus 
38112
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset

10 
datatype prol_term = Var of string  Cons of string  AppF of string * prol_term list 
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

11 
 Number of int  ArithOp of arith_op * prol_term list; 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

12 
datatype prem = Conj of prem list 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

13 
 Rel of string * prol_term list  NotRel of string * prol_term list 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

14 
 Eq of prol_term * prol_term  NotEq of prol_term * prol_term 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

15 
 ArithEq of prol_term * prol_term  NotArithEq of prol_term * prol_term; 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

16 

38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

17 
type clause = ((string * prol_term list) * prem); 
38073  18 
type logic_program = clause list; 
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

19 
type constant_table = (string * string) list 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

20 

7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

21 
val generate : Proof.context > string list > (logic_program * constant_table) 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

22 
val write_program : logic_program > string 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

23 
val run : logic_program > string > string list > int option > prol_term list list 
38073  24 

38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

25 
val trace : bool Unsynchronized.ref 
38073  26 
end; 
27 

28 
structure Code_Prolog : CODE_PROLOG = 

29 
struct 

30 

38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

31 
(* diagnostic tracing *) 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

32 

7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

33 
val trace = Unsynchronized.ref false 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

34 

7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

35 
fun tracing s = if !trace then Output.tracing s else () 
38073  36 
(* general string functions *) 
37 

38 
val first_upper = implode o nth_map 0 Symbol.to_ascii_upper o explode; 

39 
val first_lower = implode o nth_map 0 Symbol.to_ascii_lower o explode; 

40 

41 
(* internal program representation *) 

42 

38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

43 
datatype arith_op = Plus  Minus 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

44 

38112
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset

45 
datatype prol_term = Var of string  Cons of string  AppF of string * prol_term list 
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

46 
 Number of int  ArithOp of arith_op * prol_term list; 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

47 

81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

48 
fun is_Var (Var _) = true 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

49 
 is_Var _ = false 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

50 

81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

51 
fun is_arith_term (Var _) = true 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

52 
 is_arith_term (Number _) = true 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

53 
 is_arith_term (ArithOp (_, operands)) = forall is_arith_term operands 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

54 
 is_arith_term _ = false 
38073  55 

38081
8b02ce312893
removing pointless type information in internal prolog terms
bulwahn
parents:
38080
diff
changeset

56 
fun string_of_prol_term (Var s) = "Var " ^ s 
38075  57 
 string_of_prol_term (Cons s) = "Cons " ^ s 
58 
 string_of_prol_term (AppF (f, args)) = f ^ "(" ^ commas (map string_of_prol_term args) ^ ")" 

38112
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset

59 
 string_of_prol_term (Number n) = "Number " ^ string_of_int n 
38075  60 

38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

61 
datatype prem = Conj of prem list 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

62 
 Rel of string * prol_term list  NotRel of string * prol_term list 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

63 
 Eq of prol_term * prol_term  NotEq of prol_term * prol_term 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

64 
 ArithEq of prol_term * prol_term  NotArithEq of prol_term * prol_term; 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

65 

38073  66 
fun dest_Rel (Rel (c, ts)) = (c, ts) 
67 

38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

68 
type clause = ((string * prol_term list) * prem); 
38073  69 

70 
type logic_program = clause list; 

71 

72 
(* translation from introduction rules to internal representation *) 

73 

38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

74 
(** constant table **) 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

75 

7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

76 
type constant_table = (string * string) list 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

77 

38073  78 
(* assuming no clashing *) 
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

79 
fun mk_constant_table consts = 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

80 
AList.make (first_lower o Long_Name.base_name) consts 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

81 

7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

82 
fun declare_consts consts constant_table = 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

83 
fold (fn c => AList.update (op =) (c, first_lower (Long_Name.base_name c))) consts constant_table 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

84 

7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

85 
fun translate_const constant_table c = 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

86 
case AList.lookup (op =) constant_table c of 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

87 
SOME c' => c' 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

88 
 NONE => error ("No such constant: " ^ c) 
38073  89 

38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

90 
fun inv_lookup _ [] _ = NONE 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

91 
 inv_lookup eq ((key, value)::xs) value' = 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

92 
if eq (value', value) then SOME key 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

93 
else inv_lookup eq xs value'; 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

94 

7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

95 
fun restore_const constant_table c = 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

96 
case inv_lookup (op =) constant_table c of 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

97 
SOME c' => c' 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

98 
 NONE => error ("No constant corresponding to " ^ c) 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

99 

7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

100 
(** translation of terms, literals, premises, and clauses **) 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

101 

38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

102 
fun translate_arith_const @{const_name "Groups.plus_class.plus"} = SOME Plus 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

103 
 translate_arith_const @{const_name "Groups.minus_class.minus"} = SOME Minus 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

104 
 translate_arith_const _ = NONE 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

105 

38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

106 
fun translate_term ctxt constant_table t = 
38112
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset

107 
case try HOLogic.dest_number t of 
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset

108 
SOME (@{typ "int"}, n) => Number n 
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset

109 
 NONE => 
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset

110 
(case strip_comb t of 
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset

111 
(Free (v, T), []) => Var v 
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset

112 
 (Const (c, _), []) => Cons (translate_const constant_table c) 
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset

113 
 (Const (c, _), args) => 
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

114 
(case translate_arith_const c of 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

115 
SOME aop => ArithOp (aop, map (translate_term ctxt constant_table) args) 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

116 
 NONE => 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

117 
AppF (translate_const constant_table c, map (translate_term ctxt constant_table) args)) 
38112
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset

118 
 _ => error ("illegal term for translation: " ^ Syntax.string_of_term ctxt t)) 
38073  119 

38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

120 
fun translate_literal ctxt constant_table t = 
38073  121 
case strip_comb t of 
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

122 
(Const (@{const_name "op ="}, _), [l, r]) => 
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

123 
let 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

124 
val l' = translate_term ctxt constant_table l 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

125 
val r' = translate_term ctxt constant_table r 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

126 
in 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

127 
(if is_Var l' andalso is_arith_term r' then ArithEq else Eq) (l', r') 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

128 
end 
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

129 
 (Const (c, _), args) => 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

130 
Rel (translate_const constant_table c, map (translate_term ctxt constant_table) args) 
38073  131 
 _ => error ("illegal literal for translation: " ^ Syntax.string_of_term ctxt t) 
132 

133 
fun NegRel_of (Rel lit) = NotRel lit 

134 
 NegRel_of (Eq eq) = NotEq eq 

38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

135 
 NegRel_of (ArithEq eq) = NotArithEq eq 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

136 

38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

137 
fun translate_prem ctxt constant_table t = 
38073  138 
case try HOLogic.dest_not t of 
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

139 
SOME t => NegRel_of (translate_literal ctxt constant_table t) 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

140 
 NONE => translate_literal ctxt constant_table t 
38073  141 

38114
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset

142 

0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset

143 
fun imp_prems_conv cv ct = 
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset

144 
case Thm.term_of ct of 
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset

145 
Const ("==>", _) $ _ $ _ => Conv.combination_conv (Conv.arg_conv cv) (imp_prems_conv cv) ct 
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset

146 
 _ => Conv.all_conv ct 
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset

147 

0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset

148 
fun Trueprop_conv cv ct = 
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset

149 
case Thm.term_of ct of 
38549
d0385f2764d8
use antiquotations for remaining unqualified constants in HOL
haftmann
parents:
38504
diff
changeset

150 
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

151 
 _ => raise Fail "Trueprop_conv" 
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset

152 

0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset

153 
fun preprocess_intro thy rule = 
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset

154 
Conv.fconv_rule 
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset

155 
(imp_prems_conv 
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset

156 
(Trueprop_conv (Conv.try_conv (Conv.rewr_conv @{thm Predicate.eq_is_eq})))) 
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset

157 
(Thm.transfer thy rule) 
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset

158 

38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

159 
fun translate_intros ctxt gr const constant_table = 
38073  160 
let 
38114
0f06e3cc04a6
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents:
38113
diff
changeset

161 
val intros = map (preprocess_intro (ProofContext.theory_of ctxt)) (Graph.get_node gr const) 
38073  162 
val (intros', ctxt') = Variable.import_terms true (map prop_of intros) ctxt 
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

163 
val constant_table' = declare_consts (fold Term.add_const_names intros' []) constant_table 
38073  164 
fun translate_intro intro = 
165 
let 

166 
val head = HOLogic.dest_Trueprop (Logic.strip_imp_concl intro) 

167 
val prems = map HOLogic.dest_Trueprop (Logic.strip_imp_prems intro) 

38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

168 
val prems' = Conj (map (translate_prem ctxt' constant_table') prems) 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

169 
val clause = (dest_Rel (translate_literal ctxt' constant_table' head), prems') 
38073  170 
in clause end 
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

171 
in (map translate_intro intros', constant_table') end 
38073  172 

173 
fun generate ctxt const = 

174 
let 

175 
fun strong_conn_of gr keys = 

176 
Graph.strong_conn (Graph.subgraph (member (op =) (Graph.all_succs gr keys)) gr) 

177 
val gr = Predicate_Compile_Core.intros_graph_of ctxt 

178 
val scc = strong_conn_of gr const 

38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

179 
val constant_table = mk_constant_table (flat scc) 
38073  180 
in 
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

181 
apfst flat (fold_map (translate_intros ctxt gr) (flat scc) constant_table) 
38073  182 
end 
183 

184 
(* transform logic program *) 

185 

186 
(** ensure groundness of terms before negation **) 

187 

188 
fun add_vars (Var x) vs = insert (op =) x vs 

189 
 add_vars (Cons c) vs = vs 

190 
 add_vars (AppF (f, args)) vs = fold add_vars args vs 

191 

192 
fun string_of_typ (Type (s, Ts)) = Long_Name.base_name s 

193 

194 
fun mk_groundness_prems ts = 

195 
let 

196 
val vars = fold add_vars ts [] 

38081
8b02ce312893
removing pointless type information in internal prolog terms
bulwahn
parents:
38080
diff
changeset

197 
fun mk_ground v = 
8b02ce312893
removing pointless type information in internal prolog terms
bulwahn
parents:
38080
diff
changeset

198 
Rel ("ground", [Var v]) 
38073  199 
in 
200 
map mk_ground vars 

201 
end 

202 

203 
fun ensure_groundness_prem (NotRel (c, ts)) = Conj (mk_groundness_prems ts @ [NotRel (c, ts)]) 

204 
 ensure_groundness_prem (NotEq (l, r)) = Conj (mk_groundness_prems [l, r] @ [NotEq (l, r)]) 

205 
 ensure_groundness_prem (Conj ps) = Conj (map ensure_groundness_prem ps) 

206 
 ensure_groundness_prem p = p 

207 

208 
fun ensure_groundness_before_negation p = 

209 
map (apsnd ensure_groundness_prem) p 

210 

211 
(* code printer *) 

212 

38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

213 
fun write_arith_op Plus = "+" 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

214 
 write_arith_op Minus = "" 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

215 

38081
8b02ce312893
removing pointless type information in internal prolog terms
bulwahn
parents:
38080
diff
changeset

216 
fun write_term (Var v) = first_upper v 
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

217 
 write_term (Cons c) = c 
38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

218 
 write_term (AppF (f, args)) = f ^ "(" ^ space_implode ", " (map write_term args) ^ ")" 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

219 
 write_term (ArithOp (oper, [a1, a2])) = write_term a1 ^ " " ^ write_arith_op oper ^ " " ^ write_term a2 
38112
cf08f4780938
adding numbers as basic term in prolog code generation
bulwahn
parents:
38082
diff
changeset

220 
 write_term (Number n) = string_of_int n 
38073  221 

222 
fun write_rel (pred, args) = 

223 
pred ^ "(" ^ space_implode ", " (map write_term args) ^ ")" 

224 

225 
fun write_prem (Conj prems) = space_implode ", " (map write_prem prems) 

226 
 write_prem (Rel p) = write_rel p 

227 
 write_prem (NotRel p) = "not(" ^ write_rel p ^ ")" 

228 
 write_prem (Eq (l, r)) = write_term l ^ " = " ^ write_term r 

229 
 write_prem (NotEq (l, r)) = write_term l ^ " \\= " ^ write_term r 

38113
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

230 
 write_prem (ArithEq (l, r)) = write_term l ^ " is " ^ write_term r 
81f08bbb3be7
adding basic arithmetic support for prolog code generation
bulwahn
parents:
38112
diff
changeset

231 
 write_prem (NotArithEq (l, r)) = write_term l ^ " =\\= " ^ write_term r 
38073  232 

233 
fun write_clause (head, prem) = 

234 
write_rel head ^ (if prem = Conj [] then "." else " : " ^ write_prem prem ^ ".") 

235 

236 
fun write_program p = 

237 
cat_lines (map write_clause p) 

238 

38078  239 
(** query templates **) 
240 

38073  241 
fun query_first rel vnames = 
242 
"eval : once(" ^ rel ^ "(" ^ space_implode ", " vnames ^ ")),\n" ^ 

38082  243 
"writef('" ^ space_implode ";" (map (fn v => v ^ " = %w") vnames) ^ 
244 
"\\n', [" ^ space_implode ", " vnames ^ "]).\n" 

38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset

245 

46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset

246 
fun query_firstn n rel vnames = 
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset

247 
"eval : findnsols(" ^ string_of_int n ^ ", (" ^ space_implode ", " vnames ^ "), " ^ 
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset

248 
rel ^ "(" ^ space_implode ", " vnames ^ "), Sols), writelist(Sols).\n" ^ 
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset

249 
"writelist([]).\n" ^ 
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset

250 
"writelist([(" ^ space_implode ", " vnames ^ ")T]) : " ^ 
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

251 
"writef('" ^ space_implode ";" (map (fn v => v ^ " = %w") vnames) ^ 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

252 
"\\n', [" ^ space_implode ", " vnames ^ "]), writelist(T).\n" 
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset

253 

38073  254 
val prelude = 
255 
"#!/usr/bin/swipl q t main f\n\n" ^ 

38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset

256 
": use_module(library('dialect/ciao/aggregates')).\n" ^ 
38073  257 
": style_check(singleton).\n\n" ^ 
258 
"main : catch(eval, E, (print_message(error, E), fail)), halt.\n" ^ 

259 
"main : halt(1).\n" 

38075  260 

261 
(* parsing prolog solution *) 

38115
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset

262 
val scan_number = 
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset

263 
Scan.many1 Symbol.is_ascii_digit 
38075  264 

265 
val scan_atom = 

38078  266 
Scan.many1 (fn s => Symbol.is_ascii_lower s orelse Symbol.is_ascii_quasi s) 
38075  267 

268 
val scan_var = 

38078  269 
Scan.many1 
270 
(fn s => Symbol.is_ascii_upper s orelse Symbol.is_ascii_digit s orelse Symbol.is_ascii_quasi s) 

38075  271 

38076  272 
val scan_ident = 
273 
Scan.repeat (Scan.one 

274 
(fn s => Symbol.is_ascii_letter s orelse Symbol.is_ascii_digit s orelse Symbol.is_ascii_quasi s)) 

275 

38075  276 
fun dest_Char (Symbol.Char s) = s 
277 

278 
val string_of = concat o map (dest_Char o Symbol.decode) 

279 

38076  280 
val is_atom_ident = forall Symbol.is_ascii_lower 
281 

282 
val is_var_ident = 

283 
forall (fn s => Symbol.is_ascii_upper s orelse Symbol.is_ascii_digit s orelse Symbol.is_ascii_quasi s) 

38078  284 

38115
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset

285 
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

286 

38078  287 
fun scan_terms xs = (((scan_term  $$ ",") ::: scan_terms) 
288 
 (scan_term >> single)) xs 

289 
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

290 
((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

291 
 (scan_var >> (Var o string_of)) 
38078  292 
 ((scan_atom  ($$ "("  scan_terms  $$ ")")) 
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

293 
>> (fn (f, ts) => AppF (string_of f, ts))) 
38078  294 
 (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

295 

38075  296 
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

297 
(Scan.error (!! (fn _ => raise Fail "parsing prolog output failed")) scan_term) 
38075  298 
o explode 
299 

38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

300 
fun parse_solutions sol = 
38075  301 
let 
38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset

302 
fun dest_eq s = case space_explode "=" s of 
38075  303 
(l :: r :: []) => parse_term (unprefix " " r) 
38078  304 
 _ => 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

305 
fun parse_solution s = map dest_eq (space_explode ";" s) 
38075  306 
in 
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

307 
map parse_solution (fst (split_last (space_explode "\n" sol))) 
38075  308 
end 
38073  309 

310 
(* calling external interpreter and getting results *) 

311 

38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset

312 
fun run p query_rel vnames nsols = 
38073  313 
let 
314 
val cmd = Path.named_root 

38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset

315 
val query = case nsols of NONE => query_first  SOME n => query_firstn n 
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset

316 
val prog = prelude ^ query query_rel vnames ^ write_program p 
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

317 
val _ = tracing ("Generated prolog program:\n" ^ prog) 
38073  318 
val prolog_file = File.tmp_path (Path.basic "prolog_file") 
319 
val _ = File.write prolog_file prog 

38077
46ff55ace18c
querying for multiple solutions in values command for prolog execution
bulwahn
parents:
38076
diff
changeset

320 
val (solution, _) = bash_output ("/usr/local/bin/swipl f " ^ File.shell_path prolog_file) 
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

321 
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

322 
val tss = parse_solutions solution 
38073  323 
in 
38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

324 
tss 
38073  325 
end 
326 

38075  327 
(* values command *) 
328 

38081
8b02ce312893
removing pointless type information in internal prolog terms
bulwahn
parents:
38080
diff
changeset

329 
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

330 
 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

331 
 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

332 
 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

333 
 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

334 
let 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

335 
val thy = ProofContext.theory_of ctxt 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

336 
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

337 
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

338 
val (argsT, resT) = strip_type cT 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

339 
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

340 
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

341 
in 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

342 
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

343 
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

344 
end 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

345 

38075  346 
fun values ctxt soln t_compr = 
347 
let 

348 
val split = case t_compr of (Const (@{const_name Collect}, _) $ t) => t 

349 
 _ => error ("Not a set comprehension: " ^ Syntax.string_of_term ctxt t_compr); 

350 
val (body, Ts, fp) = HOLogic.strip_psplits split; 

351 
val output_names = Name.variant_list (Term.add_free_names body []) 

352 
(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

353 
val output_frees = rev (map2 (curry Free) output_names Ts) 
38075  354 
val body = subst_bounds (output_frees, body) 
355 
val (pred as Const (name, T), all_args) = 

356 
case strip_comb body of 

357 
(Const (name, T), all_args) => (Const (name, T), all_args) 

358 
 (head, _) => error ("Not a constant: " ^ Syntax.string_of_term ctxt head) 

359 
val vnames = 

360 
case try (map (fst o dest_Free)) all_args of 

361 
SOME vs => vs 

362 
 NONE => error ("Not only free variables in " ^ commas (map (Syntax.string_of_term ctxt) all_args)) 

38079
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

363 
val _ = tracing "Generating prolog program..." 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

364 
val (p, constant_table) = generate ctxt [name] 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

365 
val _ = tracing "Running prolog program..." 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

366 
val tss = run p (translate_const constant_table name) (map first_upper vnames) soln 
7fb011dd51de
improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents:
38078
diff
changeset

367 
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

368 
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

369 
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

370 
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

371 
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

372 
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

373 
(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

374 
 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

375 
let 
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset

376 
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

377 
in 
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset

378 
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

379 
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

380 
else 
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset

381 
let 
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset

382 
val uu as (uuN, uuT) = singleton (Variable.variant_frees ctxt [t]) ("uu", fastype_of t) 
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset

383 
val set_compr = 
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset

384 
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

385 
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

386 
in 
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset

387 
set_compr :: (if null in_insert then xs else (fold mk_insert in_insert empty) :: xs) 
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset

388 
end 
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset

389 
end 
38075  390 
in 
38115
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset

391 
foldl1 (HOLogic.mk_binop @{const_name sup}) (mk_set_compr [] 
987edb27f449
adding parsing of numbers; improving output of solution without free variables in prolog code generation
bulwahn
parents:
38114
diff
changeset

392 
(map (fn ts => HOLogic.mk_tuple (map (restore_term ctxt constant_table) (ts ~~ Ts))) tss) []) 
38075  393 
end 
394 

395 
fun values_cmd print_modes soln raw_t state = 

396 
let 

397 
val ctxt = Toplevel.context_of state 

398 
val t = Syntax.read_term ctxt raw_t 

399 
val t' = values ctxt soln t 

400 
val ty' = Term.type_of t' 

401 
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

402 
val _ = tracing "Printing terms..." 
38075  403 
val p = Print_Mode.with_modes print_modes (fn () => 
404 
Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t'), Pretty.fbrk, 

405 
Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' ty')]) (); 

406 
in Pretty.writeln p end; 

407 

408 

409 
(* renewing the values command for Prolog queries *) 

410 

411 
val opt_print_modes = 

412 
Scan.optional (Parse.$$$ "("  Parse.!!! (Scan.repeat1 Parse.xname  Parse.$$$ ")")) []; 

413 

414 
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

415 
(opt_print_modes  Scan.optional (Parse.nat >> SOME) NONE  Parse.term 
38075  416 
>> (fn ((print_modes, soln), t) => Toplevel.keep 
38504
76965c356d2a
removed Code_Prolog: modifies global environment setup nonconservatively
haftmann
parents:
38115
diff
changeset

417 
(values_cmd print_modes soln t))); (*FIXME does not preserve the previous functionality*) 
38075  418 

38073  419 
end; 