src/HOL/Tools/svc_funcs.ML
author wenzelm
Mon, 08 Oct 2001 12:27:19 +0200
changeset 11707 6c45813c2db1
parent 10892 405b077433a3
child 12262 11ff5f47df6e
permissions -rw-r--r--
replace 0r/1r by 0/1;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
     1
(*  Title:      HOL/Tools/svc_funcs.ML
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
     2
    ID:         $Id$
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
     3
    Author:     Lawrence C Paulson
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
     4
    Copyright   1999  University of Cambridge
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
     5
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
     6
Translation functions for the interface to SVC
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
     7
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
     8
Based upon the work of Søren T. Heilmann
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
     9
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    10
Integers and naturals are translated as follows:
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    11
  In a positive context, replace x<y by x+1<=y
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    12
  In a negative context, replace x<=y by x<y+1
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    13
  In a negative context, replace x=y by x<y+1 & y<x+1
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    14
Biconditionals (if-and-only-iff) are expanded if they require such translations
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    15
  in either operand.
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    16
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    17
For each variable of type nat, an assumption is added that it is non-negative.
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    18
*)
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    19
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    20
structure Svc =
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    21
struct
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    22
 val trace = ref false;
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    23
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    24
 datatype expr =
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    25
     Buildin of string * expr list
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    26
   | Interp of string * expr list
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    27
   | UnInterp of string * expr list
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    28
   | FalseExpr 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    29
   | TrueExpr
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    30
   | Int of int
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    31
   | Rat of int * int;
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    32
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    33
 open BasisLibrary
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    34
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    35
 fun signedInt i = 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    36
     if i < 0 then "-" ^ Int.toString (~i)
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    37
     else Int.toString i;
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    38
	 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    39
 fun is_intnat T = T = HOLogic.intT orelse T = HOLogic.natT;
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    40
 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    41
 fun is_numeric T = is_intnat T orelse T = HOLogic.realT;
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    42
 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    43
 fun is_numeric_op T = is_numeric (domain_type T);
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    44
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    45
 fun toString t =
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    46
     let fun ue (Buildin(s, l)) = 
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    47
	     "(" ^ s ^ (foldl (fn (a, b) => a ^ " " ^ (ue b)) ("", l)) ^ ") "
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    48
	   | ue (Interp(s, l)) = 
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    49
	     "{" ^ s ^ (foldl (fn (a, b) => a ^ " " ^ (ue b)) ("", l)) ^ "} "
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    50
	   | ue (UnInterp(s, l)) = 
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    51
	     "(" ^ s ^ (foldl (fn (a, b) => a ^ " " ^ (ue b)) ("", l)) ^ ") "
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    52
	   | ue (FalseExpr) = "FALSE "
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    53
	   | ue (TrueExpr)  = "TRUE "
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    54
	   | ue (Int i)     = (signedInt i) ^ " "
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    55
	   | ue (Rat(i, j)) = (signedInt i) ^ "|" ^ (signedInt j) ^ " "
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    56
     in
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    57
	 ue t
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    58
     end;
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    59
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    60
 fun valid e = 
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    61
  let val svc_home = getenv "SVC_HOME" 
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    62
      val svc_machine = getenv "SVC_MACHINE"
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    63
      val check_valid = if svc_home = ""
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    64
	                then error "Environment variable SVC_HOME not set"
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    65
			else if svc_machine = ""
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    66
	                then error "Environment variable SVC_MACHINE not set"
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    67
			else svc_home ^ "/" ^ svc_machine ^ "/bin/check_valid"
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    68
      val svc_input = toString e
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    69
      val _ = if !trace then writeln ("Calling SVC:\n" ^ svc_input) else ()
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    70
      val svc_input_file  = File.tmp_path (Path.basic "SVM_in");
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    71
      val svc_output_file = File.tmp_path (Path.basic "SVM_out");
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    72
      val _ = (File.write svc_input_file svc_input;
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    73
	       execute (check_valid ^ " -dump-result " ^ 
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    74
			File.sysify_path svc_output_file ^
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    75
			" " ^ File.sysify_path svc_input_file ^ 
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    76
			"> /dev/null 2>&1"))
10892
405b077433a3 HOLogic.dest_binum;
wenzelm
parents: 7545
diff changeset
    77
      val svc_output =
405b077433a3 HOLogic.dest_binum;
wenzelm
parents: 7545
diff changeset
    78
        (case Library.try File.read svc_output_file of
405b077433a3 HOLogic.dest_binum;
wenzelm
parents: 7545
diff changeset
    79
          Some out => out
405b077433a3 HOLogic.dest_binum;
wenzelm
parents: 7545
diff changeset
    80
        | None => error "SVC returned no output");
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    81
  in
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    82
      if ! trace then writeln ("SVC Returns:\n" ^ svc_output)
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    83
      else (File.rm svc_input_file; File.rm svc_output_file);
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    84
      String.isPrefix "VALID" svc_output
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    85
  end
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    86
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    87
 (*New exception constructor for passing arguments to the oracle*)
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    88
 exception OracleExn of term;
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    89
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    90
 fun apply c args =
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    91
     let val (ts, bs) = ListPair.unzip args
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    92
     in  (list_comb(c,ts), exists I bs)  end;
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    93
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    94
 (*Determining whether the biconditionals must be unfolded: if there are
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    95
   int or nat comparisons below*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    96
 val iff_tag =
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    97
   let fun tag t =
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    98
	 let val (c,ts) = strip_comb t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    99
	 in  case c of
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   100
	     Const("op &", _)   => apply c (map tag ts)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   101
	   | Const("op |", _)   => apply c (map tag ts)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   102
	   | Const("op -->", _) => apply c (map tag ts)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   103
	   | Const("Not", _)    => apply c (map tag ts)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   104
	   | Const("True", _)   => (c, false)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   105
	   | Const("False", _)  => (c, false)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   106
	   | Const("op =", Type ("fun", [T,_])) => 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   107
		 if T = HOLogic.boolT then
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   108
		     (*biconditional: with int/nat comparisons below?*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   109
		     let val [t1,t2] = ts
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   110
			 val (u1,b1) = tag t1
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   111
			 and (u2,b2) = tag t2
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   112
			 val cname = if b1 orelse b2 then "unfold" else "keep"
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   113
		     in 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   114
			(Const ("SVC_Oracle.iff_" ^ cname, dummyT) $ u1 $ u2,
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   115
			 b1 orelse b2)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   116
		     end
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   117
		 else (*might be numeric equality*) (t, is_intnat T)
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   118
	   | Const("op <", Type ("fun", [T,_]))  => (t, is_intnat T)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   119
	   | Const("op <=", Type ("fun", [T,_])) => (t, is_intnat T)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   120
	   | _ => (t, false)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   121
	 end
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   122
   in #1 o tag end;
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   123
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   124
 (*Map expression e to 0<=a --> e, where "a" is the name of a nat variable*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   125
 fun add_nat_var (a, e) = 
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   126
     Buildin("=>", [Buildin("<=", [Int 0, UnInterp (a, [])]),
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   127
		    e]);
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   128
7545
1578f1fd62cf fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents: 7285
diff changeset
   129
 fun param_string [] = ""
1578f1fd62cf fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents: 7285
diff changeset
   130
   | param_string is = "_" ^ space_implode "_" (map string_of_int is)
1578f1fd62cf fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents: 7285
diff changeset
   131
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   132
 (*Translate an Isabelle formula into an SVC expression
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   133
   pos ["positive"]: true if an assumption, false if a goal*)
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   134
 fun expr_of pos t =
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   135
  let
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   136
    val params = rev (rename_wrt_term t (Term.strip_all_vars t))
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   137
    and body   = Term.strip_all_body t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   138
    val nat_vars = ref ([] : string list)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   139
    (*translation of a variable: record all natural numbers*)
7545
1578f1fd62cf fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents: 7285
diff changeset
   140
    fun trans_var (a,T,is) =
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   141
	(if T = HOLogic.natT then nat_vars := (a ins_string (!nat_vars))
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   142
	                     else ();
7545
1578f1fd62cf fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents: 7285
diff changeset
   143
         UnInterp (a ^ param_string is, []))
1578f1fd62cf fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents: 7285
diff changeset
   144
    (*A variable, perhaps applied to a series of parameters*)
1578f1fd62cf fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents: 7285
diff changeset
   145
    fun var (Free(a,T), is)      = trans_var ("F_" ^ a, T, is)
1578f1fd62cf fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents: 7285
diff changeset
   146
      | var (Var((a, 0), T), is) = trans_var (a, T, is)
1578f1fd62cf fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents: 7285
diff changeset
   147
      | var (Bound i, is)        = 
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   148
          let val (a,T) = List.nth (params, i)
7545
1578f1fd62cf fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents: 7285
diff changeset
   149
	  in  trans_var ("B_" ^ a, T, is)  end
1578f1fd62cf fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents: 7285
diff changeset
   150
      | var (t $ Bound i, is)    = var(t,i::is)
1578f1fd62cf fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents: 7285
diff changeset
   151
            (*removing a parameter from a Var: the bound var index will
1578f1fd62cf fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents: 7285
diff changeset
   152
               become part of the Var's name*)
1578f1fd62cf fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents: 7285
diff changeset
   153
      | var (t,_) = raise OracleExn t;
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   154
    (*translation of a literal*)
10892
405b077433a3 HOLogic.dest_binum;
wenzelm
parents: 7545
diff changeset
   155
    fun lit (Const("Numeral.number_of", _) $ w) =
405b077433a3 HOLogic.dest_binum;
wenzelm
parents: 7545
diff changeset
   156
          (HOLogic.dest_binum w handle TERM _ => raise Match)
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   157
      | lit (Const("0", _)) = 0
11707
6c45813c2db1 replace 0r/1r by 0/1;
wenzelm
parents: 10892
diff changeset
   158
      | lit (Const("1", _)) = 1
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   159
    (*translation of a literal expression [no variables]*)
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   160
    fun litExp (Const("op +", T) $ x $ y) = 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   161
	  if is_numeric_op T then (litExp x) + (litExp y)
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   162
          else raise OracleExn t
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   163
      | litExp (Const("op -", T) $ x $ y) = 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   164
	  if is_numeric_op T then (litExp x) - (litExp y)
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   165
          else raise OracleExn t
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   166
      | litExp (Const("op *", T) $ x $ y) = 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   167
	  if is_numeric_op T then (litExp x) * (litExp y)
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   168
          else raise OracleExn t
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   169
      | litExp (Const("uminus", T) $ x)   = 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   170
	  if is_numeric_op T then ~(litExp x)
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   171
          else raise OracleExn t
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   172
      | litExp t = lit t 
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   173
		   handle Match => raise OracleExn t
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   174
    (*translation of a real/rational expression*)
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   175
    fun suc t = Interp("+", [Int 1, t])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   176
    fun tm (Const("Suc", T) $ x) = suc (tm x)
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   177
      | tm (Const("op +", T) $ x $ y) = 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   178
	  if is_numeric_op T then Interp("+", [tm x, tm y])
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   179
          else raise OracleExn t
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   180
      | tm (Const("op -", T) $ x $ y) = 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   181
	  if is_numeric_op T then 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   182
	      Interp("+", [tm x, Interp("*", [Int ~1, tm y])])
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   183
          else raise OracleExn t
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   184
      | tm (Const("op *", T) $ x $ y) = 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   185
	  if is_numeric_op T then Interp("*", [tm x, tm y])
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   186
          else raise OracleExn t
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   187
      | tm (Const("RealDef.rinv", T) $ x) = 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   188
	  if domain_type T = HOLogic.realT then 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   189
	      Rat(1, litExp x)
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   190
          else raise OracleExn t
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   191
      | tm (Const("uminus", T) $ x) = 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   192
	  if is_numeric_op T then Interp("*", [Int ~1, tm x])
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   193
          else raise OracleExn t
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   194
      | tm t = Int (lit t) 
7545
1578f1fd62cf fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents: 7285
diff changeset
   195
	       handle Match => var (t,[])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   196
    (*translation of a formula*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   197
    and fm pos (Const("op &", _) $ p $ q) =  
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   198
	    Buildin("AND", [fm pos p, fm pos q])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   199
      | fm pos (Const("op |", _) $ p $ q) =  
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   200
	    Buildin("OR", [fm pos p, fm pos q])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   201
      | fm pos (Const("op -->", _) $ p $ q) =  
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   202
	    Buildin("=>", [fm (not pos) p, fm pos q])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   203
      | fm pos (Const("Not", _) $ p) =  
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   204
	    Buildin("NOT", [fm (not pos) p])
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   205
      | fm pos (Const("True", _)) = TrueExpr
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   206
      | fm pos (Const("False", _)) = FalseExpr
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   207
      | fm pos (Const("SVC_Oracle.iff_keep", _) $ p $ q) = 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   208
	     (*polarity doesn't matter*)
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   209
	    Buildin("=", [fm pos p, fm pos q]) 
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   210
      | fm pos (Const("SVC_Oracle.iff_unfold", _) $ p $ q) = 
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   211
	    Buildin("AND",   (*unfolding uses both polarities*)
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   212
			 [Buildin("=>", [fm (not pos) p, fm pos q]),
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   213
			  Buildin("=>", [fm (not pos) q, fm pos p])])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   214
      | fm pos (t as Const("op =", Type ("fun", [T,_])) $ x $ y) = 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   215
	    let val tx = tm x and ty = tm y
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   216
		in if pos orelse T = HOLogic.realT then
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   217
		       Buildin("=", [tx, ty])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   218
		   else if is_intnat T then
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   219
		       Buildin("AND", 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   220
				    [Buildin("<", [tx, suc ty]), 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   221
				     Buildin("<", [ty, suc tx])])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   222
		   else raise OracleExn t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   223
	    end
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   224
	(*inequalities: possible types are nat, int, real*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   225
      | fm pos (t as Const("op <",  Type ("fun", [T,_])) $ x $ y) = 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   226
	    if not pos orelse T = HOLogic.realT then
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   227
		Buildin("<", [tm x, tm y])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   228
	    else if is_intnat T then
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   229
		Buildin("<=", [suc (tm x), tm y])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   230
	    else raise OracleExn t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   231
      | fm pos (t as Const("op <=",  Type ("fun", [T,_])) $ x $ y) = 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   232
	    if pos orelse T = HOLogic.realT then
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   233
		Buildin("<=", [tm x, tm y])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   234
	    else if is_intnat T then
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   235
		Buildin("<", [tm x, suc (tm y)])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   236
	    else raise OracleExn t
7545
1578f1fd62cf fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents: 7285
diff changeset
   237
      | fm pos t = var(t,[]);
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   238
      (*entry point, and translation of a meta-formula*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   239
      fun mt pos ((c as Const("Trueprop", _)) $ p) = fm pos (iff_tag p)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   240
	| mt pos ((c as Const("==>", _)) $ p $ q) = 
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   241
	    Buildin("=>", [mt (not pos) p, mt pos q])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   242
	| mt pos t = fm pos (iff_tag t)  (*it might be a formula*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   243
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   244
      val body_e = mt pos body  (*evaluate now to assign into !nat_vars*)
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   245
  in 
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   246
     foldr add_nat_var (!nat_vars, body_e) 
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   247
  end;
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   248
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   249
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   250
 (*The oracle proves the given formula t, if possible*)
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   251
 fun oracle (sign, OracleExn t) = 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   252
   let val dummy = if !trace then writeln ("Subgoal abstracted to\n" ^
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   253
					   Sign.string_of_term sign t)
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   254
                   else ()
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   255
   in
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   256
       if valid (expr_of false t) then t
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   257
       else raise OracleExn t
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   258
   end;
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   259
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   260
end;