src/HOL/Tools/Predicate_Compile/code_prolog.ML
author bulwahn
Sun, 01 Aug 2010 10:15:43 +0200
changeset 38114 0f06e3cc04a6
parent 38113 81f08bbb3be7
child 38115 987edb27f449
permissions -rw-r--r--
adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
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
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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    26
end;
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
structure Code_Prolog : CODE_PROLOG =
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    29
struct
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    36
(* general string functions *)
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
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
    39
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
    40
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    41
(* internal program representation *)
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    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
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
    57
  | string_of_prol_term (Cons s) = "Cons " ^ s
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
    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
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
    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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    66
fun dest_Rel (Rel (c, ts)) = (c, ts)
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    69
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    70
type logic_program = clause list;
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    71
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    72
(* translation from introduction rules to internal representation *)
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
    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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   131
  | _ => error ("illegal literal for translation: " ^ Syntax.string_of_term ctxt t)
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   132
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   133
fun NegRel_of (Rel lit) = NotRel lit
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   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
0f06e3cc04a6 adding preprocessing of introduction rules to replace the constant Predicate.eq in the prolog code generation
bulwahn
parents: 38113
diff changeset
   150
    Const ("Trueprop", _) $ _ => Conv.arg_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
   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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   164
    fun translate_intro intro =
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   165
      let
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   166
        val head = HOLogic.dest_Trueprop (Logic.strip_imp_concl intro)
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   172
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   173
fun generate ctxt const =
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   174
  let 
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   175
     fun strong_conn_of gr keys =
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   176
      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
   177
    val gr = Predicate_Compile_Core.intros_graph_of ctxt
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   182
  end
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   183
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   184
(* transform logic program *)
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   185
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   186
(** ensure groundness of terms before negation **)
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   187
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   188
fun add_vars (Var x) vs = insert (op =) x vs
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   189
  | add_vars (Cons c) vs = vs
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   190
  | add_vars (AppF (f, args)) vs = fold add_vars args vs
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   191
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   192
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
   193
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   194
fun mk_groundness_prems ts =
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   195
  let
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   199
  in
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   200
    map mk_ground vars
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   201
  end
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
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
   204
  | 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
   205
  | ensure_groundness_prem (Conj ps) = Conj (map ensure_groundness_prem ps)
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   206
  | ensure_groundness_prem p = p
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   207
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   208
fun ensure_groundness_before_negation p =
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   209
  map (apsnd ensure_groundness_prem) p
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   210
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   211
(* code printer *)
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   221
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   222
fun write_rel (pred, args) =
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   223
  pred ^ "(" ^ space_implode ", " (map write_term args) ^ ")" 
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   224
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   225
fun write_prem (Conj prems) = space_implode ", " (map write_prem prems)
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   226
  | write_prem (Rel p) = write_rel p  
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   227
  | write_prem (NotRel p) = "not(" ^ write_rel p ^ ")"
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   228
  | write_prem (Eq (l, r)) = write_term l ^ " = " ^ write_term r
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   232
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   233
fun write_clause (head, prem) =
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   234
  write_rel head ^ (if prem = Conj [] then "." else " :- " ^ write_prem prem ^ ".")
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   235
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   236
fun write_program p =
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   237
  cat_lines (map write_clause p) 
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   238
38078
2afb5f710b84 working on parser for prolog reponse
bulwahn
parents: 38077
diff changeset
   239
(** query templates **)
2afb5f710b84 working on parser for prolog reponse
bulwahn
parents: 38077
diff changeset
   240
38073
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   241
fun query_first rel vnames =
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   242
  "eval :- once("  ^ rel ^ "(" ^ space_implode ", " vnames ^ ")),\n" ^
38082
61280b97b761 adapting output for first solution
bulwahn
parents: 38081
diff changeset
   243
  "writef('" ^ space_implode ";" (map (fn v => v ^ " = %w") vnames) ^
61280b97b761 adapting output for first solution
bulwahn
parents: 38081
diff changeset
   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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   254
val prelude =
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   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
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   257
  ":- style_check(-singleton).\n\n" ^
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   258
  "main :- catch(eval, E, (print_message(error, E), fail)), halt.\n" ^
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   259
  "main :- halt(1).\n"
38075
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
(* parsing prolog solution *)
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   262
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   263
val scan_atom =
38078
2afb5f710b84 working on parser for prolog reponse
bulwahn
parents: 38077
diff changeset
   264
  Scan.many1 (fn s => Symbol.is_ascii_lower s orelse Symbol.is_ascii_quasi s)
38075
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   265
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   266
val scan_var =
38078
2afb5f710b84 working on parser for prolog reponse
bulwahn
parents: 38077
diff changeset
   267
  Scan.many1
2afb5f710b84 working on parser for prolog reponse
bulwahn
parents: 38077
diff changeset
   268
    (fn s => Symbol.is_ascii_upper s orelse Symbol.is_ascii_digit s orelse Symbol.is_ascii_quasi s)
38075
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   269
38076
2a9c14d9d2ef correcting scanning
bulwahn
parents: 38075
diff changeset
   270
val scan_ident =
2a9c14d9d2ef correcting scanning
bulwahn
parents: 38075
diff changeset
   271
  Scan.repeat (Scan.one
2a9c14d9d2ef correcting scanning
bulwahn
parents: 38075
diff changeset
   272
    (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
   273
38075
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   274
fun dest_Char (Symbol.Char s) = s
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   275
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   276
val string_of = concat o map (dest_Char o Symbol.decode)
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   277
38076
2a9c14d9d2ef correcting scanning
bulwahn
parents: 38075
diff changeset
   278
val is_atom_ident = forall Symbol.is_ascii_lower
2a9c14d9d2ef correcting scanning
bulwahn
parents: 38075
diff changeset
   279
2a9c14d9d2ef correcting scanning
bulwahn
parents: 38075
diff changeset
   280
val is_var_ident =
2a9c14d9d2ef correcting scanning
bulwahn
parents: 38075
diff changeset
   281
  forall (fn s => Symbol.is_ascii_upper s orelse Symbol.is_ascii_digit s orelse Symbol.is_ascii_quasi s)
38078
2afb5f710b84 working on parser for prolog reponse
bulwahn
parents: 38077
diff changeset
   282
2afb5f710b84 working on parser for prolog reponse
bulwahn
parents: 38077
diff changeset
   283
fun scan_terms xs = (((scan_term --| $$ ",") ::: scan_terms)
2afb5f710b84 working on parser for prolog reponse
bulwahn
parents: 38077
diff changeset
   284
  || (scan_term >> single)) xs
2afb5f710b84 working on parser for prolog reponse
bulwahn
parents: 38077
diff changeset
   285
and scan_term xs =
38081
8b02ce312893 removing pointless type information in internal prolog terms
bulwahn
parents: 38080
diff changeset
   286
  ((scan_var >> (Var o string_of))
38078
2afb5f710b84 working on parser for prolog reponse
bulwahn
parents: 38077
diff changeset
   287
  || ((scan_atom -- ($$ "(" |-- scan_terms --| $$ ")"))
38079
7fb011dd51de improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents: 38078
diff changeset
   288
    >> (fn (f, ts) => AppF (string_of f, ts)))
38078
2afb5f710b84 working on parser for prolog reponse
bulwahn
parents: 38077
diff changeset
   289
  || (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
   290
38075
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   291
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
   292
    (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
   293
  o explode
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   294
  
38079
7fb011dd51de improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents: 38078
diff changeset
   295
fun parse_solutions sol =
38075
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   296
  let
38077
46ff55ace18c querying for multiple solutions in values command for prolog execution
bulwahn
parents: 38076
diff changeset
   297
    fun dest_eq s = case space_explode "=" s of
38075
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   298
        (l :: r :: []) => parse_term (unprefix " " r)
38078
2afb5f710b84 working on parser for prolog reponse
bulwahn
parents: 38077
diff changeset
   299
      | _ => 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
   300
    fun parse_solution s = map dest_eq (space_explode ";" s)
38075
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   301
  in
38079
7fb011dd51de improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents: 38078
diff changeset
   302
    map parse_solution (fst (split_last (space_explode "\n" sol)))
38075
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   303
  end 
38073
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   304
  
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   305
(* calling external interpreter and getting results *)
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   306
38077
46ff55ace18c querying for multiple solutions in values command for prolog execution
bulwahn
parents: 38076
diff changeset
   307
fun run p query_rel vnames nsols =
38073
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   308
  let
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   309
    val cmd = Path.named_root
38077
46ff55ace18c querying for multiple solutions in values command for prolog execution
bulwahn
parents: 38076
diff changeset
   310
    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
   311
    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
   312
    val _ = tracing ("Generated prolog program:\n" ^ prog)
38073
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   313
    val prolog_file = File.tmp_path (Path.basic "prolog_file")
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   314
    val _ = File.write prolog_file prog
38077
46ff55ace18c querying for multiple solutions in values command for prolog execution
bulwahn
parents: 38076
diff changeset
   315
    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
   316
    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
   317
    val tss = parse_solutions solution
38073
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   318
  in
38079
7fb011dd51de improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents: 38078
diff changeset
   319
    tss
38073
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   320
  end
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   321
38075
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   322
(* values command *)
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   323
38081
8b02ce312893 removing pointless type information in internal prolog terms
bulwahn
parents: 38080
diff changeset
   324
fun restore_term ctxt constant_table (Var s, T) = Free (s, T)
38079
7fb011dd51de improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents: 38078
diff changeset
   325
  | 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
   326
  | 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
   327
    let
7fb011dd51de improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents: 38078
diff changeset
   328
      val thy = ProofContext.theory_of ctxt
7fb011dd51de improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents: 38078
diff changeset
   329
      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
   330
      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
   331
      val (argsT, resT) = strip_type cT
7fb011dd51de improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents: 38078
diff changeset
   332
      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
   333
      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
   334
    in
7fb011dd51de improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents: 38078
diff changeset
   335
      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
   336
        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
   337
    end
7fb011dd51de improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents: 38078
diff changeset
   338
38075
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   339
fun values ctxt soln t_compr =
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   340
  let
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   341
    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
   342
      | _ => 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
   343
    val (body, Ts, fp) = HOLogic.strip_psplits split;
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   344
    val output_names = Name.variant_list (Term.add_free_names body [])
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   345
      (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
   346
    val output_frees = rev (map2 (curry Free) output_names Ts)
38075
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   347
    val body = subst_bounds (output_frees, body)
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   348
    val (pred as Const (name, T), all_args) =
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   349
      case strip_comb body of
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   350
        (Const (name, T), all_args) => (Const (name, T), all_args)
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   351
      | (head, _) => error ("Not a constant: " ^ Syntax.string_of_term ctxt head)
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   352
    val vnames =
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   353
      case try (map (fst o dest_Free)) all_args of
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   354
        SOME vs => vs
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   355
      | 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
   356
    val _ = tracing "Generating prolog program..."
7fb011dd51de improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents: 38078
diff changeset
   357
    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
   358
    val _ = tracing "Running prolog program..."
7fb011dd51de improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents: 38078
diff changeset
   359
    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
   360
    val _ = tracing "Restoring terms..."
7fb011dd51de improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents: 38078
diff changeset
   361
    fun mk_set_comprehension t =
7fb011dd51de improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents: 38078
diff changeset
   362
      let
7fb011dd51de improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents: 38078
diff changeset
   363
        val frees = Term.add_frees t []
7fb011dd51de improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents: 38078
diff changeset
   364
        val uu as (uuN, uuT) = singleton (Variable.variant_frees ctxt [t]) ("uu", fastype_of t)
7fb011dd51de improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents: 38078
diff changeset
   365
      in HOLogic.mk_Collect (uuN, uuT, fold (fn (s, T) => fn t => HOLogic.mk_exists (s, T, t))
7fb011dd51de improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents: 38078
diff changeset
   366
        frees (HOLogic.mk_conj (HOLogic.mk_eq (Free uu, t), @{term "True"}))) end
7fb011dd51de improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents: 38078
diff changeset
   367
    val set_comprs = map (fn ts =>
7fb011dd51de improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents: 38078
diff changeset
   368
      mk_set_comprehension (HOLogic.mk_tuple (map (restore_term ctxt constant_table) (ts ~~ Ts)))) tss
38075
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   369
  in
38079
7fb011dd51de improving translation to prolog; restoring terms from prolog output; adding tracing support
bulwahn
parents: 38078
diff changeset
   370
    foldl1 (HOLogic.mk_binop @{const_name sup}) (set_comprs @ [Free ("...", fastype_of t_compr)])
38075
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   371
  end
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   372
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   373
fun values_cmd print_modes soln raw_t state =
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   374
  let
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   375
    val ctxt = Toplevel.context_of state
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   376
    val t = Syntax.read_term ctxt raw_t
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   377
    val t' = values ctxt soln t
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   378
    val ty' = Term.type_of t'
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   379
    val ctxt' = Variable.auto_fixes t' ctxt
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   380
    val p = Print_Mode.with_modes print_modes (fn () =>
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   381
      Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t'), Pretty.fbrk,
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   382
        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
   383
  in Pretty.writeln p end;
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   384
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   385
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   386
(* renewing the values command for Prolog queries *)
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   387
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   388
val opt_print_modes =
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   389
  Scan.optional (Parse.$$$ "(" |-- Parse.!!! (Scan.repeat1 Parse.xname --| Parse.$$$ ")")) [];
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   390
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   391
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
   392
  (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
   393
   >> (fn ((print_modes, soln), t) => Toplevel.keep
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   394
        (values_cmd print_modes soln t)));
3d5e2b7d1374 adding values command and parsing prolog output
bulwahn
parents: 38073
diff changeset
   395
38073
64062d56ad3c adding a mockup version for prolog code generation
bulwahn
parents:
diff changeset
   396
end;