src/HOL/SVC_Oracle.ML
author wenzelm
Tue, 24 Aug 1999 11:50:58 +0200
changeset 7333 6cb15c6f1d9f
parent 7304 94c6f8f07631
child 7539 680eca63b98e
permissions -rw-r--r--
isar: no_pos flag;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7237
2919daadba91 turned SVC_Oracle into a new-style theory in order to get automatic
wenzelm
parents: 7187
diff changeset
     1
(*  Title:      HOL/SVC_Oracle.ML
7144
0feee8201d67 the SVC oracle theory
paulson
parents:
diff changeset
     2
    ID:         $Id$
0feee8201d67 the SVC oracle theory
paulson
parents:
diff changeset
     3
    Author:     Lawrence C Paulson
0feee8201d67 the SVC oracle theory
paulson
parents:
diff changeset
     4
    Copyright   1999  University of Cambridge
0feee8201d67 the SVC oracle theory
paulson
parents:
diff changeset
     5
0feee8201d67 the SVC oracle theory
paulson
parents:
diff changeset
     6
Installing the oracle for SVC (Stanford Validity Checker)
0feee8201d67 the SVC oracle theory
paulson
parents:
diff changeset
     7
0feee8201d67 the SVC oracle theory
paulson
parents:
diff changeset
     8
Based upon the work of Søren T. Heilmann
0feee8201d67 the SVC oracle theory
paulson
parents:
diff changeset
     9
*)
0feee8201d67 the SVC oracle theory
paulson
parents:
diff changeset
    10
7284
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    11
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    12
(*Generalize an Isabelle formula, replacing by Vars
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    13
  all subterms not intelligible to SVC.*)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    14
fun svc_abstract t =
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    15
  let
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    16
    val params = Term.strip_all_vars t
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    17
    and body   = Term.strip_all_body t
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    18
    val Us = map #2 params
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    19
    val nPar = length params
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    20
    val vname = ref "V_a"
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    21
    val pairs = ref ([] : (term*term) list)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    22
    fun insert t = 
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    23
	let val T = fastype_of t
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    24
	    val v = Unify.combound (Var ((!vname,0), Us--->T),
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    25
				    0, nPar)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    26
	in  vname := bump_string (!vname); 
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    27
	    pairs := (t, v) :: !pairs;
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    28
	    v
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    29
	end;
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    30
    fun replace t = 
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    31
	case t of
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    32
	    Free _  => t  (*but not existing Vars, lest the names clash*)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    33
	  | Bound _ => t
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    34
	  | _ => (case gen_assoc (op aconv) (!pairs, t) of
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    35
		      Some v => v
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    36
		    | None   => insert t)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    37
    (*abstraction of a real/rational expression*)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    38
    fun rat ((c as Const("op +", _)) $ x $ y) = c $ (rat x) $ (rat y)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    39
      | rat ((c as Const("op -", _)) $ x $ y) = c $ (rat x) $ (rat y)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    40
      | rat ((c as Const("op /", _)) $ x $ y) = c $ (rat x) $ (rat y)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    41
      | rat ((c as Const("op *", _)) $ x $ y) = c $ (rat x) $ (rat y)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    42
      | rat ((c as Const("uminus", _)) $ x) = c $ (rat x)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    43
      | rat ((c as Const("RealDef.0r", _))) = c
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    44
      | rat ((c as Const("RealDef.1r", _))) = c 
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    45
      | rat (t as Const("Numeral.number_of", _) $ w) = t
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    46
      | rat t = replace t
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    47
    (*abstraction of an integer expression: no div, mod*)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    48
    fun int ((c as Const("op +", _)) $ x $ y) = c $ (int x) $ (int y)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    49
      | int ((c as Const("op -", _)) $ x $ y) = c $ (int x) $ (int y)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    50
      | int ((c as Const("op *", _)) $ x $ y) = c $ (int x) $ (int y)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    51
      | int ((c as Const("uminus", _)) $ x) = c $ (int x)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    52
      | int (t as Const("Numeral.number_of", _) $ w) = t
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    53
      | int t = replace t
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    54
    (*abstraction of a natural number expression: no minus*)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    55
    fun nat ((c as Const("op +", _)) $ x $ y) = c $ (nat x) $ (nat y)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    56
      | nat ((c as Const("op *", _)) $ x $ y) = c $ (nat x) $ (nat y)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    57
      | nat ((c as Const("Suc", _)) $ x) = c $ (nat x)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    58
      | nat (t as Const("0", _)) = t
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    59
      | nat (t as Const("Numeral.number_of", _) $ w) = t
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    60
      | nat t = replace t
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    61
    (*abstraction of a relation: =, <, <=*)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    62
    fun rel (T, c $ x $ y) =
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    63
	    if T = HOLogic.realT then c $ (rat x) $ (rat y)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    64
	    else if T = HOLogic.intT then c $ (int x) $ (int y)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    65
	    else if T = HOLogic.natT then c $ (nat x) $ (nat y)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    66
	    else if T = HOLogic.boolT then c $ (fm x) $ (fm y)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    67
	    else replace (c $ x $ y)   (*non-numeric comparison*)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    68
    (*abstraction of a formula*)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    69
    and fm ((c as Const("op &", _)) $ p $ q) = c $ (fm p) $ (fm q)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    70
      | fm ((c as Const("op |", _)) $ p $ q) = c $ (fm p) $ (fm q)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    71
      | fm ((c as Const("op -->", _)) $ p $ q) = c $ (fm p) $ (fm q)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    72
      | fm ((c as Const("Not", _)) $ p) = c $ (fm p)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    73
      | fm ((c as Const("True", _))) = c
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    74
      | fm ((c as Const("False", _))) = c
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    75
      | fm (t as Const("op =", Type ("fun", [T,_])) $ x $ y) = rel (T, t)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    76
      | fm (t as Const("op <", Type ("fun", [T,_])) $ x $ y) = rel (T, t)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    77
      | fm (t as Const("op <=", Type ("fun", [T,_])) $ x $ y) = rel (T, t)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    78
      | fm t = replace t
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    79
    (*entry point, and abstraction of a meta-formula*)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    80
    fun mt ((c as Const("Trueprop", _)) $ p) = c $ (fm p)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    81
      | mt ((c as Const("==>", _)) $ p $ q)  = c $ (mt p) $ (mt q)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    82
      | mt t = fm t  (*it might be a formula*)
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    83
  in (list_all (params, mt body), !pairs) end;
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    84
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    85
7144
0feee8201d67 the SVC oracle theory
paulson
parents:
diff changeset
    86
(*Present the entire subgoal to the oracle, assumptions and all, but possibly
0feee8201d67 the SVC oracle theory
paulson
parents:
diff changeset
    87
  abstracted.  Use via compose_tac, which performs no lifting but will
0feee8201d67 the SVC oracle theory
paulson
parents:
diff changeset
    88
  instantiate variables.*)
7237
2919daadba91 turned SVC_Oracle into a new-style theory in order to get automatic
wenzelm
parents: 7187
diff changeset
    89
local val svc_thy = the_context () in
2919daadba91 turned SVC_Oracle into a new-style theory in order to get automatic
wenzelm
parents: 7187
diff changeset
    90
7162
8737390d1d0a biconditionals and the natural numbers
paulson
parents: 7144
diff changeset
    91
fun svc_tac i st = 
7237
2919daadba91 turned SVC_Oracle into a new-style theory in order to get automatic
wenzelm
parents: 7187
diff changeset
    92
  let val prem = BasisLibrary.List.nth (prems_of st, i-1)
7284
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    93
      val (absPrem, _) = svc_abstract prem
7237
2919daadba91 turned SVC_Oracle into a new-style theory in order to get automatic
wenzelm
parents: 7187
diff changeset
    94
      val th = invoke_oracle svc_thy "svc_oracle"
7284
29105299799c now with abstraction code previously in HOL/Tools/svc_funcs.ML
paulson
parents: 7237
diff changeset
    95
	             (#sign (rep_thm st), Svc.OracleExn absPrem)
7162
8737390d1d0a biconditionals and the natural numbers
paulson
parents: 7144
diff changeset
    96
   in 
8737390d1d0a biconditionals and the natural numbers
paulson
parents: 7144
diff changeset
    97
      compose_tac (false, th, 0) i st
8737390d1d0a biconditionals and the natural numbers
paulson
parents: 7144
diff changeset
    98
   end 
8737390d1d0a biconditionals and the natural numbers
paulson
parents: 7144
diff changeset
    99
   handle Svc.OracleExn _ => Seq.empty
8737390d1d0a biconditionals and the natural numbers
paulson
parents: 7144
diff changeset
   100
	| Subscript       => Seq.empty;
7144
0feee8201d67 the SVC oracle theory
paulson
parents:
diff changeset
   101
7237
2919daadba91 turned SVC_Oracle into a new-style theory in order to get automatic
wenzelm
parents: 7187
diff changeset
   102
end;
2919daadba91 turned SVC_Oracle into a new-style theory in order to get automatic
wenzelm
parents: 7187
diff changeset
   103
7144
0feee8201d67 the SVC oracle theory
paulson
parents:
diff changeset
   104
7304
94c6f8f07631 if_svc_enabled;
wenzelm
parents: 7284
diff changeset
   105
(*check if user has SVC installed*)
94c6f8f07631 if_svc_enabled;
wenzelm
parents: 7284
diff changeset
   106
fun svc_enabled () = getenv "SVC_HOME" <> "";
94c6f8f07631 if_svc_enabled;
wenzelm
parents: 7284
diff changeset
   107
fun if_svc_enabled f x = if svc_enabled () then f x else ();