src/HOL/Tools/svc_funcs.ML
author paulson
Tue, 21 Sep 1999 10:39:33 +0200
changeset 7545 1578f1fd62cf
parent 7285 52ea6848b908
child 10892 405b077433a3
permissions -rw-r--r--
fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
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"))
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    77
      val svc_output = File.read svc_output_file
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    78
	               handle _ => error "SVC returned no output"
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    79
  in
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    80
      if ! trace then writeln ("SVC Returns:\n" ^ svc_output)
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    81
      else (File.rm svc_input_file; File.rm svc_output_file);
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    82
      String.isPrefix "VALID" svc_output
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    83
  end
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    84
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    85
 (*New exception constructor for passing arguments to the oracle*)
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    86
 exception OracleExn of term;
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    87
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    88
 fun apply c args =
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    89
     let val (ts, bs) = ListPair.unzip args
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    90
     in  (list_comb(c,ts), exists I bs)  end;
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    91
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
    92
 (*Determining whether the biconditionals must be unfolded: if there are
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    93
   int or nat comparisons below*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    94
 val iff_tag =
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    95
   let fun tag t =
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    96
	 let val (c,ts) = strip_comb t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    97
	 in  case c of
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    98
	     Const("op &", _)   => apply c (map tag ts)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    99
	   | Const("op |", _)   => apply c (map tag ts)
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("Not", _)    => apply c (map tag ts)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   102
	   | Const("True", _)   => (c, false)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   103
	   | Const("False", _)  => (c, false)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   104
	   | Const("op =", Type ("fun", [T,_])) => 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   105
		 if T = HOLogic.boolT then
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   106
		     (*biconditional: with int/nat comparisons below?*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   107
		     let val [t1,t2] = ts
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   108
			 val (u1,b1) = tag t1
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   109
			 and (u2,b2) = tag t2
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   110
			 val cname = if b1 orelse b2 then "unfold" else "keep"
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   111
		     in 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   112
			(Const ("SVC_Oracle.iff_" ^ cname, dummyT) $ u1 $ u2,
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   113
			 b1 orelse b2)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   114
		     end
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   115
		 else (*might be numeric equality*) (t, is_intnat T)
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   116
	   | Const("op <", Type ("fun", [T,_]))  => (t, is_intnat T)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   117
	   | Const("op <=", Type ("fun", [T,_])) => (t, is_intnat T)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   118
	   | _ => (t, false)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   119
	 end
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   120
   in #1 o tag end;
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   121
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   122
 (*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
   123
 fun add_nat_var (a, e) = 
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   124
     Buildin("=>", [Buildin("<=", [Int 0, UnInterp (a, [])]),
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   125
		    e]);
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   126
7545
1578f1fd62cf fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents: 7285
diff changeset
   127
 fun param_string [] = ""
1578f1fd62cf fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents: 7285
diff changeset
   128
   | 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
   129
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   130
 (*Translate an Isabelle formula into an SVC expression
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   131
   pos ["positive"]: true if an assumption, false if a goal*)
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   132
 fun expr_of pos t =
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   133
  let
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   134
    val params = rev (rename_wrt_term t (Term.strip_all_vars t))
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   135
    and body   = Term.strip_all_body t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   136
    val nat_vars = ref ([] : string list)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   137
    (*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
   138
    fun trans_var (a,T,is) =
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   139
	(if T = HOLogic.natT then nat_vars := (a ins_string (!nat_vars))
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   140
	                     else ();
7545
1578f1fd62cf fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents: 7285
diff changeset
   141
         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
   142
    (*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
   143
    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
   144
      | 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
   145
      | var (Bound i, is)        = 
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   146
          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
   147
	  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
   148
      | 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
   149
            (*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
   150
               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
   151
      | var (t,_) = raise OracleExn t;
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   152
    (*translation of a literal*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   153
    fun lit (Const("Numeral.number_of", _) $ w) = NumeralSyntax.dest_bin w
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   154
      | lit (Const("0", _)) = 0
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   155
      | lit (Const("RealDef.0r", _)) = 0
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   156
      | lit (Const("RealDef.1r", _)) = 1
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   157
    (*translation of a literal expression [no variables]*)
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   158
    fun litExp (Const("op +", T) $ x $ y) = 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   159
	  if is_numeric_op T then (litExp x) + (litExp y)
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   160
          else raise OracleExn t
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   161
      | litExp (Const("op -", T) $ x $ y) = 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   162
	  if is_numeric_op T then (litExp x) - (litExp y)
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   163
          else raise OracleExn t
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   164
      | litExp (Const("op *", T) $ x $ y) = 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   165
	  if is_numeric_op T then (litExp x) * (litExp y)
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   166
          else raise OracleExn t
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   167
      | litExp (Const("uminus", T) $ x)   = 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   168
	  if is_numeric_op T then ~(litExp x)
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   169
          else raise OracleExn t
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   170
      | litExp t = lit t 
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   171
		   handle Match => raise OracleExn t
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   172
    (*translation of a real/rational expression*)
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   173
    fun suc t = Interp("+", [Int 1, t])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   174
    fun tm (Const("Suc", T) $ x) = suc (tm x)
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   175
      | tm (Const("op +", T) $ x $ y) = 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   176
	  if is_numeric_op T then Interp("+", [tm x, tm y])
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   177
          else raise OracleExn t
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   178
      | tm (Const("op -", T) $ x $ y) = 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   179
	  if is_numeric_op T then 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   180
	      Interp("+", [tm x, Interp("*", [Int ~1, tm y])])
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   181
          else raise OracleExn t
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   182
      | tm (Const("op *", T) $ x $ y) = 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   183
	  if is_numeric_op T then Interp("*", [tm x, tm y])
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   184
          else raise OracleExn t
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   185
      | tm (Const("RealDef.rinv", T) $ x) = 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   186
	  if domain_type T = HOLogic.realT then 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   187
	      Rat(1, litExp x)
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   188
          else raise OracleExn t
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   189
      | tm (Const("uminus", T) $ x) = 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   190
	  if is_numeric_op T then Interp("*", [Int ~1, tm x])
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   191
          else raise OracleExn t
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   192
      | 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
   193
	       handle Match => var (t,[])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   194
    (*translation of a formula*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   195
    and fm pos (Const("op &", _) $ p $ q) =  
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   196
	    Buildin("AND", [fm pos p, fm pos q])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   197
      | fm pos (Const("op |", _) $ p $ q) =  
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   198
	    Buildin("OR", [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("=>", [fm (not pos) p, fm pos q])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   201
      | fm pos (Const("Not", _) $ p) =  
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   202
	    Buildin("NOT", [fm (not pos) p])
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   203
      | fm pos (Const("True", _)) = TrueExpr
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   204
      | fm pos (Const("False", _)) = FalseExpr
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   205
      | fm pos (Const("SVC_Oracle.iff_keep", _) $ p $ q) = 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   206
	     (*polarity doesn't matter*)
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   207
	    Buildin("=", [fm pos p, fm pos q]) 
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   208
      | fm pos (Const("SVC_Oracle.iff_unfold", _) $ p $ q) = 
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   209
	    Buildin("AND",   (*unfolding uses both polarities*)
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   210
			 [Buildin("=>", [fm (not pos) p, fm pos q]),
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   211
			  Buildin("=>", [fm (not pos) q, fm pos p])])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   212
      | fm pos (t as Const("op =", Type ("fun", [T,_])) $ x $ y) = 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   213
	    let val tx = tm x and ty = tm y
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   214
		in if pos orelse T = HOLogic.realT then
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   215
		       Buildin("=", [tx, ty])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   216
		   else if is_intnat T then
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   217
		       Buildin("AND", 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   218
				    [Buildin("<", [tx, suc ty]), 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   219
				     Buildin("<", [ty, suc tx])])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   220
		   else raise OracleExn t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   221
	    end
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   222
	(*inequalities: possible types are nat, int, real*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   223
      | fm pos (t as Const("op <",  Type ("fun", [T,_])) $ x $ y) = 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   224
	    if not pos orelse T = HOLogic.realT then
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   225
		Buildin("<", [tm x, tm y])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   226
	    else if is_intnat T then
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   227
		Buildin("<=", [suc (tm x), tm y])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   228
	    else raise OracleExn t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   229
      | fm pos (t as Const("op <=",  Type ("fun", [T,_])) $ x $ y) = 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   230
	    if pos orelse T = HOLogic.realT then
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   231
		Buildin("<=", [tm x, tm y])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   232
	    else if is_intnat T then
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   233
		Buildin("<", [tm x, suc (tm y)])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   234
	    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
   235
      | fm pos t = var(t,[]);
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   236
      (*entry point, and translation of a meta-formula*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   237
      fun mt pos ((c as Const("Trueprop", _)) $ p) = fm pos (iff_tag p)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   238
	| mt pos ((c as Const("==>", _)) $ p $ q) = 
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   239
	    Buildin("=>", [mt (not pos) p, mt pos q])
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   240
	| mt pos t = fm pos (iff_tag t)  (*it might be a formula*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   241
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   242
      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
   243
  in 
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   244
     foldr add_nat_var (!nat_vars, body_e) 
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   245
  end;
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   246
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   247
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   248
 (*The oracle proves the given formula t, if possible*)
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   249
 fun oracle (sign, OracleExn t) = 
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   250
   let val dummy = if !trace then writeln ("Subgoal abstracted to\n" ^
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   251
					   Sign.string_of_term sign t)
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   252
                   else ()
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   253
   in
7285
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   254
       if valid (expr_of false t) then t
52ea6848b908 removed all unnecessary code
paulson
parents: 7232
diff changeset
   255
       else raise OracleExn t
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   256
   end;
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   257
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   258
end;