src/HOL/ex/svc_funcs.ML
author krauss
Tue, 28 Sep 2010 09:54:07 +0200
changeset 39754 150f831ce4a3
parent 38864 4abe644fcea5
child 42364 8c674b3b8e44
permissions -rw-r--r--
no longer declare .psimps rules as [simp]. This regularly caused confusion (e.g., they show up in simp traces when the regular simp rules are disabled). In the few places where the rules are used, explicitly mentioning them actually clarifies the proof text.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24584
01e83ffa6c54 fixed title
haftmann
parents: 23881
diff changeset
     1
(*  Title:      HOL/ex/svc_funcs.ML
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
     2
    Author:     Lawrence C Paulson
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
     3
    Copyright   1999  University of Cambridge
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
     4
29276
94b1ffec9201 qualified Term.rename_wrt_term;
wenzelm
parents: 28290
diff changeset
     5
Translation functions for the interface to SVC.
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
     6
19336
fb5e19d26d5e removed some illegal characters: they were crashing SML/NJ
paulson
parents: 19277
diff changeset
     7
Based upon the work of Soren T. Heilmann
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
     8
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
     9
Integers and naturals are translated as follows:
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    10
  In a positive context, replace x<y by x+1<=y
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    11
  In a negative context, replace x<=y by x<y+1
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    12
  In a negative context, replace x=y by x<y+1 & y<x+1
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    13
Biconditionals (if-and-only-iff) are expanded if they require such translations
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    14
  in either operand.
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    15
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    16
For each variable of type nat, an assumption is added that it is non-negative.
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    17
*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    18
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    19
structure Svc =
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    20
struct
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 29276
diff changeset
    21
 val trace = Unsynchronized.ref false;
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    22
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    23
 datatype expr =
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    24
     Buildin of string * expr list
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    25
   | Interp of string * expr list
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    26
   | UnInterp of string * expr list
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    27
   | FalseExpr
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    28
   | TrueExpr
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    29
   | Int of int
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    30
   | Rat of int * int;
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    31
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    32
 fun is_intnat T = T = HOLogic.intT orelse T = HOLogic.natT;
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    33
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    34
 fun is_numeric T = is_intnat T orelse T = HOLogic.realT;
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    35
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    36
 fun is_numeric_op T = is_numeric (domain_type T);
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    37
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    38
 fun toString t =
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    39
     let fun ue (Buildin(s, l)) =
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    40
             "(" ^ s ^ (Library.foldl (fn (a, b) => a ^ " " ^ (ue b)) ("", l)) ^ ") "
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    41
           | ue (Interp(s, l)) =
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    42
             "{" ^ s ^ (Library.foldl (fn (a, b) => a ^ " " ^ (ue b)) ("", l)) ^ "} "
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    43
           | ue (UnInterp(s, l)) =
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    44
             "(" ^ s ^ (Library.foldl (fn (a, b) => a ^ " " ^ (ue b)) ("", l)) ^ ") "
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    45
           | ue (FalseExpr) = "FALSE "
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    46
           | ue (TrueExpr)  = "TRUE "
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    47
           | ue (Int i)     = signed_string_of_int i ^ " "
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    48
           | ue (Rat(i, j)) = signed_string_of_int i ^ "|" ^ signed_string_of_int j ^ " "
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    49
     in
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    50
         ue t
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    51
     end;
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    52
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    53
 fun valid e =
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    54
  let val svc_home = getenv "SVC_HOME"
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    55
      val svc_machine = getenv "SVC_MACHINE"
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    56
      val check_valid = if svc_home = ""
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    57
                        then error "Environment variable SVC_HOME not set"
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    58
                        else if svc_machine = ""
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    59
                        then error "Environment variable SVC_MACHINE not set"
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    60
                        else svc_home ^ "/" ^ svc_machine ^ "/bin/check_valid"
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    61
      val svc_input = toString e
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    62
      val _ = if !trace then tracing ("Calling SVC:\n" ^ svc_input) else ()
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    63
      val svc_input_file  = File.tmp_path (Path.basic "SVM_in");
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    64
      val svc_output_file = File.tmp_path (Path.basic "SVM_out");
35010
d6e492cea6e4 renamed system/system_out to bash/bash_output -- to emphasized that this is really GNU bash, not some undefined POSIX sh;
wenzelm
parents: 34974
diff changeset
    65
      val _ = File.write svc_input_file svc_input;
d6e492cea6e4 renamed system/system_out to bash/bash_output -- to emphasized that this is really GNU bash, not some undefined POSIX sh;
wenzelm
parents: 34974
diff changeset
    66
      val _ =
d6e492cea6e4 renamed system/system_out to bash/bash_output -- to emphasized that this is really GNU bash, not some undefined POSIX sh;
wenzelm
parents: 34974
diff changeset
    67
        bash_output (check_valid ^ " -dump-result " ^
d6e492cea6e4 renamed system/system_out to bash/bash_output -- to emphasized that this is really GNU bash, not some undefined POSIX sh;
wenzelm
parents: 34974
diff changeset
    68
          File.shell_path svc_output_file ^ " " ^ File.shell_path svc_input_file ^
d6e492cea6e4 renamed system/system_out to bash/bash_output -- to emphasized that this is really GNU bash, not some undefined POSIX sh;
wenzelm
parents: 34974
diff changeset
    69
          ">/dev/null 2>&1")
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    70
      val svc_output =
21395
f34ac19659ae moved some fundamental concepts to General/basics.ML;
wenzelm
parents: 21078
diff changeset
    71
        (case try File.read svc_output_file of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14982
diff changeset
    72
          SOME out => out
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14982
diff changeset
    73
        | NONE => error "SVC returned no output");
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    74
  in
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    75
      if ! trace then tracing ("SVC Returns:\n" ^ svc_output)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    76
      else (File.rm svc_input_file; File.rm svc_output_file);
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    77
      String.isPrefix "VALID" svc_output
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    78
  end
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    79
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    80
 fun fail t = raise TERM ("SVC oracle", [t]);
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    81
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    82
 fun apply c args =
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    83
     let val (ts, bs) = ListPair.unzip args
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    84
     in  (list_comb(c,ts), exists I bs)  end;
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    85
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    86
 (*Determining whether the biconditionals must be unfolded: if there are
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    87
   int or nat comparisons below*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    88
 val iff_tag =
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    89
   let fun tag t =
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    90
         let val (c,ts) = strip_comb t
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    91
         in  case c of
38795
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
    92
             Const(@{const_name HOL.conj}, _)   => apply c (map tag ts)
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
    93
           | Const(@{const_name HOL.disj}, _)   => apply c (map tag ts)
38786
e46e7a9cb622 formerly unnamed infix impliciation now named HOL.implies
haftmann
parents: 38558
diff changeset
    94
           | Const(@{const_name HOL.implies}, _) => apply c (map tag ts)
38558
32ad17fe2b9c tuned quotes
haftmann
parents: 38549
diff changeset
    95
           | Const(@{const_name Not}, _)    => apply c (map tag ts)
32ad17fe2b9c tuned quotes
haftmann
parents: 38549
diff changeset
    96
           | Const(@{const_name True}, _)   => (c, false)
32ad17fe2b9c tuned quotes
haftmann
parents: 38549
diff changeset
    97
           | Const(@{const_name False}, _)  => (c, false)
38864
4abe644fcea5 formerly unnamed infix equality now named HOL.eq
haftmann
parents: 38795
diff changeset
    98
           | Const(@{const_name HOL.eq}, Type ("fun", [T,_])) =>
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
    99
                 if T = HOLogic.boolT then
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   100
                     (*biconditional: with int/nat comparisons below?*)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   101
                     let val [t1,t2] = ts
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   102
                         val (u1,b1) = tag t1
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   103
                         and (u2,b2) = tag t2
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   104
                         val cname = if b1 orelse b2 then "unfold" else "keep"
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   105
                     in
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   106
                        (Const ("SVC_Oracle.iff_" ^ cname, dummyT) $ u1 $ u2,
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   107
                         b1 orelse b2)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   108
                     end
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   109
                 else (*might be numeric equality*) (t, is_intnat T)
35092
cfe605c54e50 moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
haftmann
parents: 35084
diff changeset
   110
           | Const(@{const_name Orderings.less}, Type ("fun", [T,_]))  => (t, is_intnat T)
cfe605c54e50 moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
haftmann
parents: 35084
diff changeset
   111
           | Const(@{const_name Orderings.less_eq}, Type ("fun", [T,_])) => (t, is_intnat T)
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   112
           | _ => (t, false)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   113
         end
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   114
   in #1 o tag end;
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   115
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   116
 (*Map expression e to 0<=a --> e, where "a" is the name of a nat variable*)
21078
101aefd61aac slight cleanup
haftmann
parents: 20853
diff changeset
   117
 fun add_nat_var a e =
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   118
     Buildin("=>", [Buildin("<=", [Int 0, UnInterp (a, [])]),
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   119
                    e]);
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   120
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   121
 fun param_string [] = ""
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   122
   | param_string is = "_" ^ space_implode "_" (map string_of_int is)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   123
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   124
 (*Translate an Isabelle formula into an SVC expression
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   125
   pos ["positive"]: true if an assumption, false if a goal*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   126
 fun expr_of pos t =
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   127
  let
29276
94b1ffec9201 qualified Term.rename_wrt_term;
wenzelm
parents: 28290
diff changeset
   128
    val params = rev (Term.rename_wrt_term t (Term.strip_all_vars t))
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   129
    and body   = Term.strip_all_body t
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 29276
diff changeset
   130
    val nat_vars = Unsynchronized.ref ([] : string list)
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   131
    (*translation of a variable: record all natural numbers*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   132
    fun trans_var (a,T,is) =
20853
3ff5a2e05810 *** empty log message ***
haftmann
parents: 19336
diff changeset
   133
        (if T = HOLogic.natT then nat_vars := (insert (op =) a (!nat_vars))
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   134
                             else ();
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   135
         UnInterp (a ^ param_string is, []))
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   136
    (*A variable, perhaps applied to a series of parameters*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   137
    fun var (Free(a,T), is)      = trans_var ("F_" ^ a, T, is)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   138
      | var (Var((a, 0), T), is) = trans_var (a, T, is)
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   139
      | var (Bound i, is)        =
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   140
          let val (a,T) = List.nth (params, i)
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   141
          in  trans_var ("B_" ^ a, T, is)  end
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   142
      | var (t $ Bound i, is)    = var(t,i::is)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   143
            (*removing a parameter from a Var: the bound var index will
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   144
               become part of the Var's name*)
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   145
      | var (t,_) = fail t;
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   146
    (*translation of a literal*)
21820
2f2b6a965ccc introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents: 21395
diff changeset
   147
    val lit = snd o HOLogic.dest_number;
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   148
    (*translation of a literal expression [no variables]*)
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   149
    fun litExp (Const(@{const_name Groups.plus}, T) $ x $ y) =
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   150
          if is_numeric_op T then (litExp x) + (litExp y)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   151
          else fail t
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   152
      | litExp (Const(@{const_name Groups.minus}, T) $ x $ y) =
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   153
          if is_numeric_op T then (litExp x) - (litExp y)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   154
          else fail t
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   155
      | litExp (Const(@{const_name Groups.times}, T) $ x $ y) =
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   156
          if is_numeric_op T then (litExp x) * (litExp y)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   157
          else fail t
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   158
      | litExp (Const(@{const_name Groups.uminus}, T) $ x)   =
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   159
          if is_numeric_op T then ~(litExp x)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   160
          else fail t
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   161
      | litExp t = lit t
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   162
                   handle Match => fail t
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   163
    (*translation of a real/rational expression*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   164
    fun suc t = Interp("+", [Int 1, t])
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 21962
diff changeset
   165
    fun tm (Const(@{const_name Suc}, T) $ x) = suc (tm x)
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   166
      | tm (Const(@{const_name Groups.plus}, T) $ x $ y) =
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   167
          if is_numeric_op T then Interp("+", [tm x, tm y])
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   168
          else fail t
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   169
      | tm (Const(@{const_name Groups.minus}, T) $ x $ y) =
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   170
          if is_numeric_op T then
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   171
              Interp("+", [tm x, Interp("*", [Int ~1, tm y])])
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   172
          else fail t
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   173
      | tm (Const(@{const_name Groups.times}, T) $ x $ y) =
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   174
          if is_numeric_op T then Interp("*", [tm x, tm y])
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   175
          else fail t
35084
e25eedfc15ce moved constants inverse and divide to Ring.thy
haftmann
parents: 35010
diff changeset
   176
      | tm (Const(@{const_name Rings.inverse}, T) $ x) =
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   177
          if domain_type T = HOLogic.realT then
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   178
              Rat(1, litExp x)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   179
          else fail t
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35092
diff changeset
   180
      | tm (Const(@{const_name Groups.uminus}, T) $ x) =
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   181
          if is_numeric_op T then Interp("*", [Int ~1, tm x])
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   182
          else fail t
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   183
      | tm t = Int (lit t)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   184
               handle Match => var (t,[])
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   185
    (*translation of a formula*)
38795
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
   186
    and fm pos (Const(@{const_name HOL.conj}, _) $ p $ q) =
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   187
            Buildin("AND", [fm pos p, fm pos q])
38795
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
   188
      | fm pos (Const(@{const_name HOL.disj}, _) $ p $ q) =
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   189
            Buildin("OR", [fm pos p, fm pos q])
38786
e46e7a9cb622 formerly unnamed infix impliciation now named HOL.implies
haftmann
parents: 38558
diff changeset
   190
      | fm pos (Const(@{const_name HOL.implies}, _) $ p $ q) =
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   191
            Buildin("=>", [fm (not pos) p, fm pos q])
38558
32ad17fe2b9c tuned quotes
haftmann
parents: 38549
diff changeset
   192
      | fm pos (Const(@{const_name Not}, _) $ p) =
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   193
            Buildin("NOT", [fm (not pos) p])
38558
32ad17fe2b9c tuned quotes
haftmann
parents: 38549
diff changeset
   194
      | fm pos (Const(@{const_name True}, _)) = TrueExpr
32ad17fe2b9c tuned quotes
haftmann
parents: 38549
diff changeset
   195
      | fm pos (Const(@{const_name False}, _)) = FalseExpr
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   196
      | fm pos (Const("SVC_Oracle.iff_keep", _) $ p $ q) =
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   197
             (*polarity doesn't matter*)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   198
            Buildin("=", [fm pos p, fm pos q])
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   199
      | fm pos (Const("SVC_Oracle.iff_unfold", _) $ p $ q) =
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   200
            Buildin("AND",   (*unfolding uses both polarities*)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   201
                         [Buildin("=>", [fm (not pos) p, fm pos q]),
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   202
                          Buildin("=>", [fm (not pos) q, fm pos p])])
38864
4abe644fcea5 formerly unnamed infix equality now named HOL.eq
haftmann
parents: 38795
diff changeset
   203
      | fm pos (t as Const(@{const_name HOL.eq}, Type ("fun", [T,_])) $ x $ y) =
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   204
            let val tx = tm x and ty = tm y
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   205
                in if pos orelse T = HOLogic.realT then
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   206
                       Buildin("=", [tx, ty])
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   207
                   else if is_intnat T then
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   208
                       Buildin("AND",
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   209
                                    [Buildin("<", [tx, suc ty]),
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   210
                                     Buildin("<", [ty, suc tx])])
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   211
                   else fail t
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   212
            end
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   213
        (*inequalities: possible types are nat, int, real*)
35092
cfe605c54e50 moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
haftmann
parents: 35084
diff changeset
   214
      | fm pos (t as Const(@{const_name Orderings.less},  Type ("fun", [T,_])) $ x $ y) =
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   215
            if not pos orelse T = HOLogic.realT then
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   216
                Buildin("<", [tm x, tm y])
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   217
            else if is_intnat T then
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   218
                Buildin("<=", [suc (tm x), tm y])
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   219
            else fail t
35092
cfe605c54e50 moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
haftmann
parents: 35084
diff changeset
   220
      | fm pos (t as Const(@{const_name Orderings.less_eq},  Type ("fun", [T,_])) $ x $ y) =
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   221
            if pos orelse T = HOLogic.realT then
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   222
                Buildin("<=", [tm x, tm y])
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   223
            else if is_intnat T then
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   224
                Buildin("<", [tm x, suc (tm y)])
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   225
            else fail t
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   226
      | fm pos t = var(t,[]);
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   227
      (*entry point, and translation of a meta-formula*)
38558
32ad17fe2b9c tuned quotes
haftmann
parents: 38549
diff changeset
   228
      fun mt pos ((c as Const(@{const_name Trueprop}, _)) $ p) = fm pos (iff_tag p)
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   229
        | mt pos ((c as Const("==>", _)) $ p $ q) =
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   230
            Buildin("=>", [mt (not pos) p, mt pos q])
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   231
        | mt pos t = fm pos (iff_tag t)  (*it might be a formula*)
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   232
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   233
      val body_e = mt pos body  (*evaluate now to assign into !nat_vars*)
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 16258
diff changeset
   234
  in
21078
101aefd61aac slight cleanup
haftmann
parents: 20853
diff changeset
   235
     fold_rev add_nat_var (!nat_vars) body_e
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   236
  end;
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   237
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   238
28290
4cc2b6046258 simplified oracle interface;
wenzelm
parents: 26939
diff changeset
   239
 (*The oracle proves the given formula, if possible*)
4cc2b6046258 simplified oracle interface;
wenzelm
parents: 26939
diff changeset
   240
  fun oracle ct =
4cc2b6046258 simplified oracle interface;
wenzelm
parents: 26939
diff changeset
   241
    let
4cc2b6046258 simplified oracle interface;
wenzelm
parents: 26939
diff changeset
   242
      val thy = Thm.theory_of_cterm ct;
4cc2b6046258 simplified oracle interface;
wenzelm
parents: 26939
diff changeset
   243
      val t = Thm.term_of ct;
4cc2b6046258 simplified oracle interface;
wenzelm
parents: 26939
diff changeset
   244
      val _ =
4cc2b6046258 simplified oracle interface;
wenzelm
parents: 26939
diff changeset
   245
        if ! trace then tracing ("SVC oracle: problem is\n" ^ Syntax.string_of_term_global thy t)
4cc2b6046258 simplified oracle interface;
wenzelm
parents: 26939
diff changeset
   246
       else ();
4cc2b6046258 simplified oracle interface;
wenzelm
parents: 26939
diff changeset
   247
    in if valid (expr_of false t) then ct else fail t end;
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   248
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   249
end;