src/HOL/Tools/Predicate_Compile/code_prolog.ML
author bulwahn
Thu, 29 Jul 2010 17:27:55 +0200
changeset 38075 3d5e2b7d1374
parent 38073 64062d56ad3c
child 38076 2a9c14d9d2ef
permissions -rw-r--r--
adding values command and parsing prolog output
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
38075
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
    16
  val run : logic_program -> string -> string list -> 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
    val _ = tracing (commas (flat scc))
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    91
  in
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    92
    maps (translate_intros ctxt gr) (flat scc)
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    93
  end
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    94
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    95
(* transform logic program *)
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    96
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    97
(** ensure groundness of terms before negation **)
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    98
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    99
fun add_vars (Var x) vs = insert (op =) x vs
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   100
  | add_vars (Cons c) vs = vs
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   101
  | add_vars (AppF (f, args)) vs = fold add_vars args vs
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   102
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   103
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
   104
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   105
fun mk_groundness_prems ts =
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   106
  let
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   107
    val vars = fold add_vars ts []
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   108
    fun mk_ground (v, T) =
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   109
      Rel ("ground_" ^ string_of_typ T, [Var (v, T)])
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   110
  in
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   111
    map mk_ground vars
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   112
  end
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   113
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   114
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
   115
  | 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
   116
  | ensure_groundness_prem (Conj ps) = Conj (map ensure_groundness_prem ps)
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   117
  | ensure_groundness_prem p = p
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   118
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   119
fun ensure_groundness_before_negation p =
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   120
  map (apsnd ensure_groundness_prem) p
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   121
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   122
(* code printer *)
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   123
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   124
fun write_term (Var (v, _)) = first_upper v
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   125
  | write_term (Cons c) = first_lower c
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   126
  | 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
   127
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   128
fun write_rel (pred, args) =
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   129
  pred ^ "(" ^ space_implode ", " (map write_term args) ^ ")" 
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   130
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   131
fun write_prem (Conj prems) = space_implode ", " (map write_prem prems)
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   132
  | write_prem (Rel p) = write_rel p  
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   133
  | write_prem (NotRel p) = "not(" ^ write_rel p ^ ")"
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   134
  | write_prem (Eq (l, r)) = write_term l ^ " = " ^ write_term r
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   135
  | write_prem (NotEq (l, r)) = write_term l ^ " \\= " ^ write_term r
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   136
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   137
fun write_clause (head, prem) =
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   138
  write_rel head ^ (if prem = Conj [] then "." else " :- " ^ write_prem prem ^ ".")
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   139
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   140
fun write_program p =
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   141
  cat_lines (map write_clause p) 
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   142
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   143
fun query_first rel vnames =
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   144
  "eval :- once("  ^ rel ^ "(" ^ space_implode ", " vnames ^ ")),\n" ^
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   145
  "writef('" ^ implode (map (fn v => v ^ " = %w\\n") vnames) ^"', [" ^ space_implode ", " vnames ^ "]).\n"
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   146
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   147
val prelude =
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   148
  "#!/usr/bin/swipl -q -t main -f\n\n" ^
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   149
  ":- style_check(-singleton).\n\n" ^
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   150
  "main :- catch(eval, E, (print_message(error, E), fail)), halt.\n" ^
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   151
  "main :- halt(1).\n"
38075
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   152
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   153
(* parsing prolog solution *)
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   154
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   155
val scan_atom =
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   156
  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
   157
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   158
val scan_var =
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   159
  Scan.repeat (Scan.one
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   160
    (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
   161
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   162
fun dest_Char (Symbol.Char s) = s
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   163
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   164
val string_of = concat o map (dest_Char o Symbol.decode)
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_term =
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   167
  scan_atom >> (Cons o string_of) || scan_var >> (Var o rpair dummyT o string_of)
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   168
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   169
val parse_term = fst o Scan.finite Symbol.stopper
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   170
            (Scan.error (!! (fn _ => raise Fail "parsing prolog output failed"))
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   171
                            scan_term)
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   172
  o explode
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   173
  
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   174
fun parse_solution sol =
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   175
  let
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   176
    fun dest_eq s = (tracing s; tracing(commas (space_explode "=" s)); case space_explode "=" s of
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   177
        (l :: r :: []) => parse_term (unprefix " " r)
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   178
      | _ => raise Fail "unexpected equation in prolog output")
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   179
  in
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   180
    map dest_eq (fst (split_last (space_explode "\n" sol)))
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   181
  end 
38073
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   182
  
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   183
(* calling external interpreter and getting results *)
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   184
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   185
fun run p query_rel vnames =
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   186
  let
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   187
    val cmd = Path.named_root
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   188
    val prog = prelude ^ query_first query_rel vnames ^ write_program p
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   189
    val prolog_file = File.tmp_path (Path.basic "prolog_file")
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   190
    val _ = File.write prolog_file prog
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   191
    val (solution, _) = bash_output ("/usr/bin/swipl -f " ^ File.shell_path prolog_file)
38075
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   192
    val ts = parse_solution solution
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   193
    val _ = tracing (commas (map string_of_prol_term ts)) 
38073
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   194
  in
38075
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   195
    ts
38073
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   196
  end
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   197
38075
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   198
(* values command *)
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   199
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   200
fun mk_term (Var (s, T)) = Free (s, T)
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   201
  | mk_term (Cons s) = Const (s, dummyT)
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   202
  | 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
   203
  
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   204
fun values ctxt soln t_compr =
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   205
  let
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   206
    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
   207
      | _ => 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
   208
    val (body, Ts, fp) = HOLogic.strip_psplits split;
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   209
    val output_names = Name.variant_list (Term.add_free_names body [])
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   210
      (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
   211
    val output_frees = map2 (curry Free) output_names (rev Ts)
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   212
    val body = subst_bounds (output_frees, body)
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   213
    val (pred as Const (name, T), all_args) =
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   214
      case strip_comb body of
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   215
        (Const (name, T), all_args) => (Const (name, T), all_args)
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   216
      | (head, _) => error ("Not a constant: " ^ Syntax.string_of_term ctxt head)
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   217
    val vnames =
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   218
      case try (map (fst o dest_Free)) all_args of
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   219
        SOME vs => vs
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   220
      | NONE => error ("Not only free variables in " ^ commas (map (Syntax.string_of_term ctxt) all_args))
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   221
    val ts = run (generate ctxt [name]) (translate_const name) (map first_upper vnames)
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   222
  in
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   223
    HOLogic.mk_tuple (map mk_term ts)
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   224
  end
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   225
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   226
fun values_cmd print_modes soln raw_t state =
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   227
  let
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   228
    val ctxt = Toplevel.context_of state
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   229
    val t = Syntax.read_term ctxt raw_t
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   230
    val t' = values ctxt soln t
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   231
    val ty' = Term.type_of t'
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   232
    val ctxt' = Variable.auto_fixes t' ctxt
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   233
    val p = Print_Mode.with_modes print_modes (fn () =>
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   234
      Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t'), Pretty.fbrk,
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   235
        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
   236
  in Pretty.writeln p end;
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   237
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   238
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   239
(* renewing the values command for Prolog queries *)
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   240
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   241
val opt_print_modes =
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   242
  Scan.optional (Parse.$$$ "(" |-- Parse.!!! (Scan.repeat1 Parse.xname --| Parse.$$$ ")")) [];
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   243
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   244
val _ = Outer_Syntax.improper_command "values" "enumerate and print comprehensions" Keyword.diag
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   245
  (opt_print_modes -- Scan.optional Parse.nat ~1 -- Parse.term
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   246
   >> (fn ((print_modes, soln), t) => Toplevel.keep
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   247
        (values_cmd print_modes soln t)));
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   248
38073
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   249
end;