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