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