src/HOL/Tools/svc_funcs.ML
author paulson
Tue, 03 Aug 1999 13:15:54 +0200
changeset 7165 8c937127fd8c
parent 7164 295882e50b7a
child 7232 6e7b6b8490e0
permissions -rw-r--r--
new examples file for SVC
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
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
     6
Translation and abstraction functions for the interface to SVC
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 =
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    25
     bracketed_expr of expr
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    26
   | ref_def_expr of string * expr
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    27
   | ref_expr of string
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    28
   | typed_expr of Type * expr
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    29
   | buildin_expr of string * expr list
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    30
   | interp_expr of string * expr list
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    31
   | uninterp_expr of string * expr list
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    32
   | false_expr 
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    33
   | true_expr
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    34
   | distinct_expr of string
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    35
   | int_expr of int
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    36
   | rat_expr of int * int
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    37
 and Type = 
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    38
     simple_type of string
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    39
   | array_type of Type * Type
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    40
   | record_type of (expr * Type) list
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    41
   | bitvec_type of int;
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    42
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    43
 open BasisLibrary
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    44
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    45
 fun toString t =
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    46
     let fun signedInt i = 
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    47
	 if i < 0 then "-" ^ Int.toString (~i)
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    48
	 else Int.toString i
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    49
	 fun ut(simple_type s) = s ^ " "
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    50
	   | ut(array_type(t1, t2)) = "ARRAY " ^ (ut t1) ^ (ut t2)
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    51
	   | ut(record_type fl) = 
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    52
	     "RECORD" ^ 
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    53
	     (foldl (fn (a, (d, t)) => a ^ (ue d) ^ (ut t)) (" ", fl))
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    54
	   | ut(bitvec_type n) = "BITVEC " ^ (Int.toString n) ^ " "
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    55
	 and ue(bracketed_expr e) = "(" ^ (ue e) ^ ") "
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    56
	   | ue(ref_def_expr(r, e)) = "$" ^ r ^ ":" ^ (ue e)
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    57
	   | ue(ref_expr r) = "$" ^ r ^ " "
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    58
	   | ue(typed_expr(t, e)) = (ut t) ^ (ue e)
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    59
	   | ue(buildin_expr(s, l)) = 
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    60
	     "(" ^ s ^ (foldl (fn (a, b) => a ^ " " ^ (ue b)) ("", l)) ^ ") "
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    61
	   | ue(interp_expr(s, l)) = 
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    62
	     "{" ^ s ^ (foldl (fn (a, b) => a ^ " " ^ (ue b)) ("", l)) ^ "} "
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    63
	   | ue(uninterp_expr(s, l)) = 
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    64
	     "(" ^ s ^ (foldl (fn (a, b) => a ^ " " ^ (ue b)) ("", l)) ^ ") "
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    65
	   | ue(false_expr) = "FALSE "
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    66
	   | ue(true_expr) = "TRUE "
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    67
	   | ue(distinct_expr s) = "@" ^ s ^ " "
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    68
	   | ue(int_expr i) = (signedInt i) ^ " "
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    69
	   | ue(rat_expr(i, j)) = (signedInt i) ^ "|" ^ (signedInt j) ^ " "
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    70
     in
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    71
	 ue t
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    72
     end;
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    73
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    74
 fun valid e = 
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    75
  let val svc_home = getenv "SVC_HOME" 
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    76
      val svc_machine = getenv "SVC_MACHINE"
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    77
      val check_valid = if svc_home = ""
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    78
	                then error "Environment variable SVC_HOME not set"
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    79
			else if svc_machine = ""
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    80
	                then error "Environment variable SVC_MACHINE not set"
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    81
			else svc_home ^ "/" ^ svc_machine ^ "/bin/check_valid"
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    82
      val svc_input = toString e
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    83
      val _ = if !trace then writeln ("Calling SVC:\n" ^ svc_input) else ()
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    84
      val svc_input_file  = File.tmp_path (Path.basic "SVM_in");
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    85
      val svc_output_file = File.tmp_path (Path.basic "SVM_out");
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    86
      val _ = (File.write svc_input_file svc_input;
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    87
	       execute (check_valid ^ " -dump-result " ^ 
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    88
			File.sysify_path svc_output_file ^
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
    89
			" " ^ File.sysify_path svc_input_file ^ 
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    90
			"> /dev/null 2>&1"))
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    91
      val svc_output = File.read svc_output_file
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    92
	               handle _ => error "SVC returned no output"
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    93
      val _ = if !trace then writeln ("SVC Returns:\n" ^ svc_output) else ()
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    94
  in
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    95
      String.isPrefix "VALID" svc_output
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    96
  end
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    97
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    98
 (*New exception constructor for passing arguments to the oracle*)
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
    99
 exception OracleExn of term;
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   100
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   101
 fun apply c args =
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   102
     let val (ts, bs) = ListPair.unzip args
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   103
     in  (list_comb(c,ts), exists I bs)  end;
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   104
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   105
 fun is_intnat T = T = HOLogic.intT orelse T = HOLogic.natT;
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   106
 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   107
 (*Determining whether the biconditionals must be unfoled: if there are
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   108
   int or nat comparisons below*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   109
 val iff_tag =
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   110
   let fun tag t =
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   111
	 let val (c,ts) = strip_comb t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   112
	 in  case c of
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   113
	     Const("op &", _)   => apply c (map tag ts)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   114
	   | Const("op |", _)   => apply c (map tag ts)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   115
	   | Const("op -->", _) => apply c (map tag ts)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   116
	   | Const("Not", _)    => apply c (map tag ts)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   117
	   | Const("True", _)   => (c, false)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   118
	   | Const("False", _)  => (c, false)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   119
	   | Const("op =", Type ("fun", [T,_])) => 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   120
		 if T = HOLogic.boolT then
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   121
		     (*biconditional: with int/nat comparisons below?*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   122
		     let val [t1,t2] = ts
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   123
			 val (u1,b1) = tag t1
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   124
			 and (u2,b2) = tag t2
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   125
			 val cname = if b1 orelse b2 then "unfold" else "keep"
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   126
		     in 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   127
			(Const ("SVC_Oracle.iff_" ^ cname, dummyT) $ u1 $ u2,
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   128
			 b1 orelse b2)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   129
		     end
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   130
		 else (*numeric equality*)          (t, is_intnat T)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   131
	   | Const("op <", Type ("fun", [T,_]))  => (t, is_intnat T)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   132
	   | Const("op <=", Type ("fun", [T,_])) => (t, is_intnat T)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   133
	   | _ => (t, false)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   134
	 end
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   135
   in #1 o tag end;
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   136
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   137
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   138
 (*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
   139
 fun add_nat_var (a, e) = 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   140
     buildin_expr("=>", [buildin_expr("<=", [int_expr 0, 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   141
					     uninterp_expr (a, [])]),
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   142
			 e]);
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   143
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   144
 (*Translate an Isabelle formula into an SVC expression
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   145
   pos ["positive"]: true if an assumption, false if a goal*)
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   146
 fun expr_of pos t =
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   147
  let
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   148
    val params = rev (rename_wrt_term t (Term.strip_all_vars t))
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   149
    and body   = Term.strip_all_body t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   150
    val nat_vars = ref ([] : string list)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   151
    (*translation of a variable: record all natural numbers*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   152
    fun trans_var (a,T) =
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   153
	(if T = HOLogic.natT then nat_vars := (a ins_string (!nat_vars))
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   154
	                     else ();
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   155
         uninterp_expr (a, []))
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   156
    fun var (Free(a,T))      = trans_var ("F_" ^ a, T)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   157
      | var (Var((a, 0), T)) = trans_var (a, T)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   158
      | var (Bound i)        = 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   159
          let val (a,T) = List.nth (params, i)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   160
	  in  trans_var ("B_" ^ a, T)  end
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   161
      | var (t $ Bound _)    = var t    (*removing a parameter from a Var*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   162
      | var t = raise OracleExn t;
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   163
    (*translation of a literal*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   164
    fun lit (Const("Numeral.number_of", _) $ w) = NumeralSyntax.dest_bin w
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   165
      | lit (Const("0", _)) = 0
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   166
      | lit (Const("RealDef.0r", _)) = 0
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   167
      | lit (Const("RealDef.1r", _)) = 1
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   168
    (*translation of a literal expression [no variables]*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   169
    fun litExp (Const("op +", T) $ x $ y) = (litExp x) + (litExp y)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   170
      | litExp (Const("op -", T) $ x $ y) = (litExp x) - (litExp y)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   171
      | litExp (Const("op *", T) $ x $ y) = (litExp x) * (litExp y)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   172
      | litExp (Const("uminus", _) $ x)   = ~(litExp x)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   173
      | litExp t = lit t 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   174
		handle Match => raise OracleExn t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   175
    (*translation of a real/rational expression*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   176
    fun suc t = interp_expr("+", [int_expr 1, t])
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   177
    fun tm (Const("Suc", T) $ x) = suc (tm x)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   178
      | tm (Const("op +", T) $ x $ y) = interp_expr("+", [tm x, tm y])
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   179
      | tm (Const("op -", _) $ x $ y) = 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   180
	  interp_expr("+", [tm x, interp_expr("*", [int_expr ~1, tm y])])
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   181
      | tm (Const("op *", _) $ x $ y) = interp_expr("*", [tm x, tm y])
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   182
      | tm (Const("op /", _) $ x $ y) = 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   183
	  interp_expr("*", [tm x, rat_expr(1, litExp y)])
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   184
      | tm (Const("uminus", _) $ x) = interp_expr("*", [int_expr ~1, tm x])
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   185
      | tm t = int_expr (lit t) 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   186
	       handle Match => var t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   187
    (*translation of a formula*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   188
    and fm pos (Const("op &", _) $ p $ q) =  
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   189
	    buildin_expr("AND", [fm pos p, fm pos q])
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   190
      | fm pos (Const("op |", _) $ p $ q) =  
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   191
	    buildin_expr("OR", [fm pos p, fm pos q])
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   192
      | fm pos (Const("op -->", _) $ p $ q) =  
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   193
	    buildin_expr("=>", [fm (not pos) p, fm pos q])
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   194
      | fm pos (Const("Not", _) $ p) =  
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   195
	    buildin_expr("NOT", [fm (not pos) p])
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   196
      | fm pos (Const("True", _)) = true_expr
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   197
      | fm pos (Const("False", _)) = false_expr
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   198
      | fm pos (Const("SVC_Oracle.iff_keep", _) $ p $ q) = 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   199
	     (*polarity doesn't matter*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   200
	    buildin_expr("=", [fm pos p, fm pos q]) 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   201
      | fm pos (Const("SVC_Oracle.iff_unfold", _) $ p $ q) = 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   202
	    buildin_expr("AND",   (*unfolding uses both polarities*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   203
			 [buildin_expr("=>", [fm (not pos) p, fm pos q]),
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   204
			  buildin_expr("=>", [fm (not pos) q, fm pos p])])
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   205
      | fm pos (t as Const("op =", Type ("fun", [T,_])) $ x $ y) = 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   206
	    let val tx = tm x and ty = tm y
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   207
		in if pos orelse T = HOLogic.realT then
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   208
		       buildin_expr("=", [tx, ty])
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   209
		   else if is_intnat T then
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   210
		       buildin_expr("AND", 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   211
				    [buildin_expr("<", [tx, suc ty]), 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   212
				     buildin_expr("<", [ty, suc tx])])
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   213
		   else raise OracleExn t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   214
	    end
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   215
	(*inequalities: possible types are nat, int, real*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   216
      | fm pos (t as Const("op <",  Type ("fun", [T,_])) $ x $ y) = 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   217
	    if not pos orelse T = HOLogic.realT then
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   218
		buildin_expr("<", [tm x, tm y])
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   219
	    else if is_intnat T then
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   220
		buildin_expr("<=", [suc (tm x), tm y])
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   221
	    else raise OracleExn t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   222
      | fm pos (t as Const("op <=",  Type ("fun", [T,_])) $ x $ y) = 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   223
	    if pos orelse T = HOLogic.realT then
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   224
		buildin_expr("<=", [tm x, tm y])
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   225
	    else if is_intnat T then
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   226
		buildin_expr("<", [tm x, suc (tm y)])
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   227
	    else raise OracleExn t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   228
      | fm pos t = var t;
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   229
      (*entry point, and translation of a meta-formula*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   230
      fun mt pos ((c as Const("Trueprop", _)) $ p) = fm pos (iff_tag p)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   231
	| mt pos ((c as Const("==>", _)) $ p $ q) = 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   232
	    buildin_expr("=>", [mt (not pos) p, mt pos q])
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   233
	| mt pos t = fm pos (iff_tag t)  (*it might be a formula*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   234
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   235
      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
   236
  in 
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   237
     foldr add_nat_var (!nat_vars, body_e) 
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   238
  end;
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   239
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   240
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   241
 (*Generalize an Isabelle formula, replacing by Vars
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   242
   all subterms not intelligible to SVC.  
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   243
   Do not present "raw" terms to expr_of; the translation could be unsound!*)
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   244
 fun abstract t =
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   245
  let
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   246
    val params = Term.strip_all_vars t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   247
    and body   = Term.strip_all_body t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   248
    val Us = map #2 params
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   249
    val nPar = length params
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   250
    val vname = ref "V_a"
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   251
    val pairs = ref ([] : (term*term) list)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   252
    fun insert t = 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   253
	let val T = fastype_of t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   254
	    val v = Unify.combound (Var ((!vname,0), Us--->T),
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   255
				    0, nPar)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   256
	in  vname := bump_string (!vname); 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   257
	    pairs := (t, v) :: !pairs;
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   258
	    v
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   259
	end;
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   260
    fun replace t = 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   261
	case t of
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   262
	    Free _  => t  (*but not existing Vars, lest the names clash*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   263
	  | Bound _ => t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   264
	  | _ => (case gen_assoc (op aconv) (!pairs, t) of
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   265
		      Some v => v
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   266
		    | None   => insert t)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   267
    (*abstraction of a real/rational expression*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   268
    fun rat ((c as Const("op +", _)) $ x $ y) = c $ (rat x) $ (rat y)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   269
      | rat ((c as Const("op -", _)) $ x $ y) = c $ (rat x) $ (rat y)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   270
      | rat ((c as Const("op /", _)) $ x $ y) = c $ (rat x) $ (rat y)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   271
      | rat ((c as Const("op *", _)) $ x $ y) = c $ (rat x) $ (rat y)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   272
      | rat ((c as Const("uminus", _)) $ x) = c $ (rat x)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   273
      | rat ((c as Const("RealDef.0r", _))) = c
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   274
      | rat ((c as Const("RealDef.1r", _))) = c 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   275
      | rat (t as Const("Numeral.number_of", _) $ w) = t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   276
      | rat t = replace t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   277
    (*abstraction of an integer expression: no div, mod*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   278
    fun int ((c as Const("op +", _)) $ x $ y) = c $ (int x) $ (int y)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   279
      | int ((c as Const("op -", _)) $ x $ y) = c $ (int x) $ (int y)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   280
      | int ((c as Const("op *", _)) $ x $ y) = c $ (int x) $ (int y)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   281
      | int ((c as Const("uminus", _)) $ x) = c $ (int x)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   282
      | int (t as Const("Numeral.number_of", _) $ w) = t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   283
      | int t = replace t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   284
    (*abstraction of a natural number expression: no minus*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   285
    fun nat ((c as Const("op +", _)) $ x $ y) = c $ (nat x) $ (nat y)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   286
      | nat ((c as Const("op *", _)) $ x $ y) = c $ (nat x) $ (nat y)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   287
      | nat ((c as Const("Suc", _)) $ x) = c $ (nat x)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   288
      | nat (t as Const("0", _)) = t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   289
      | nat (t as Const("Numeral.number_of", _) $ w) = t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   290
      | nat t = replace t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   291
    (*abstraction of a relation: =, <, <=*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   292
    fun rel (T, c $ x $ y) =
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   293
	    if T = HOLogic.realT then c $ (rat x) $ (rat y)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   294
	    else if T = HOLogic.intT then c $ (int x) $ (int y)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   295
	    else if T = HOLogic.natT then c $ (nat x) $ (nat y)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   296
	    else if T = HOLogic.boolT then c $ (fm x) $ (fm y)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   297
	    else replace (c $ x $ y)   (*non-numeric comparison*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   298
    (*abstraction of a formula*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   299
    and fm ((c as Const("op &", _)) $ p $ q) = c $ (fm p) $ (fm q)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   300
      | fm ((c as Const("op |", _)) $ p $ q) = c $ (fm p) $ (fm q)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   301
      | fm ((c as Const("op -->", _)) $ p $ q) = c $ (fm p) $ (fm q)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   302
      | fm ((c as Const("Not", _)) $ p) = c $ (fm p)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   303
      | fm ((c as Const("True", _))) = c
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   304
      | fm ((c as Const("False", _))) = c
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   305
      | fm (t as Const("op =", Type ("fun", [T,_])) $ x $ y) = rel (T, t)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   306
      | fm (t as Const("op <", Type ("fun", [T,_])) $ x $ y) = rel (T, t)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   307
      | fm (t as Const("op <=", Type ("fun", [T,_])) $ x $ y) = rel (T, t)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   308
      | fm t = replace t
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   309
    (*entry point, and abstraction of a meta-formula*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   310
    fun mt ((c as Const("Trueprop", _)) $ p) = c $ (fm p)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   311
      | mt ((c as Const("==>", _)) $ p $ q)  = c $ (mt p) $ (mt q)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   312
      | mt t = fm t  (*it might be a formula*)
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   313
  in (list_all (params, mt body), !pairs) end;
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   314
7164
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   315
 (*The oracle proves not the original formula but the abstracted version*)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   316
 fun oracle (sign, OracleExn P) = 
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   317
   let val (absP, _) = abstract P
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   318
       val dummy = if !trace then writeln ("Subgoal abstracted to\n" ^
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   319
					   Sign.string_of_term sign absP)
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   320
                   else ()
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   321
   in
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   322
       if valid (expr_of false absP) then absP
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   323
       else raise OracleExn P
295882e50b7a biconditionals and the natural numbers
paulson
parents: 7145
diff changeset
   324
   end;
7145
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   325
c05373eebee3 new files for the SVC link-up
paulson
parents:
diff changeset
   326
end;