src/HOL/ex/SVC_Oracle.ML
author wenzelm
Mon, 29 Aug 2005 16:18:04 +0200
changeset 17184 3d80209e9a53
parent 16836 45a3dc4688bc
child 17314 04e21a27c0ad
permissions -rw-r--r--
use AList operations;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/SVC_Oracle.ML
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
     3
    Author:     Lawrence C Paulson
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
     4
    Copyright   1999  University of Cambridge
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
     5
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
     6
Installing the oracle for SVC (Stanford Validity Checker)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
     7
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
     8
The following code merely CALLS the oracle;
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
     9
  the soundness-critical functions are at HOL/Tools/svc_funcs.ML
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    10
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    11
Based upon the work of Søren T. Heilmann
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    12
*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    13
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    14
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    15
(*Generalize an Isabelle formula, replacing by Vars
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    16
  all subterms not intelligible to SVC.*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    17
fun svc_abstract t =
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    18
  let
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    19
    (*The oracle's result is given to the subgoal using compose_tac because
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    20
      its premises are matched against the assumptions rather than used
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    21
      to make subgoals.  Therefore , abstraction must copy the parameters
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    22
      precisely and make them available to all generated Vars.*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    23
    val params = Term.strip_all_vars t
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    24
    and body   = Term.strip_all_body t
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    25
    val Us = map #2 params
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    26
    val nPar = length params
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    27
    val vname = ref "V_a"
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    28
    val pairs = ref ([] : (term*term) list)
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    29
    fun insert t =
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    30
        let val T = fastype_of t
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    31
            val v = Unify.combound (Var ((!vname,0), Us--->T),
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    32
                                    0, nPar)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    33
        in  vname := Symbol.bump_string (!vname);
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    34
            pairs := (t, v) :: !pairs;
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    35
            v
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    36
        end;
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    37
    fun replace t =
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    38
        case t of
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    39
            Free _  => t  (*but not existing Vars, lest the names clash*)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    40
          | Bound _ => t
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    41
          | _ => (case gen_assoc Pattern.aeconv (!pairs, t) of
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    42
                      SOME v => v
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    43
                    | NONE   => insert t)
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    44
    (*abstraction of a numeric literal*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    45
    fun lit (t as Const("0", _)) = t
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    46
      | lit (t as Const("1", _)) = t
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    47
      | lit (t as Const("Numeral.number_of", _) $ w) = t
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    48
      | lit t = replace t
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    49
    (*abstraction of a real/rational expression*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    50
    fun rat ((c as Const("op +", _)) $ x $ y) = c $ (rat x) $ (rat y)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    51
      | rat ((c as Const("op -", _)) $ x $ y) = c $ (rat x) $ (rat y)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    52
      | rat ((c as Const("op /", _)) $ x $ y) = c $ (rat x) $ (rat y)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    53
      | rat ((c as Const("op *", _)) $ x $ y) = c $ (rat x) $ (rat y)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    54
      | rat ((c as Const("uminus", _)) $ x) = c $ (rat x)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    55
      | rat t = lit t
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    56
    (*abstraction of an integer expression: no div, mod*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    57
    fun int ((c as Const("op +", _)) $ x $ y) = c $ (int x) $ (int y)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    58
      | int ((c as Const("op -", _)) $ x $ y) = c $ (int x) $ (int y)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    59
      | int ((c as Const("op *", _)) $ x $ y) = c $ (int x) $ (int y)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    60
      | int ((c as Const("uminus", _)) $ x) = c $ (int x)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    61
      | int t = lit t
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    62
    (*abstraction of a natural number expression: no minus*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    63
    fun nat ((c as Const("op +", _)) $ x $ y) = c $ (nat x) $ (nat y)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    64
      | nat ((c as Const("op *", _)) $ x $ y) = c $ (nat x) $ (nat y)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    65
      | nat ((c as Const("Suc", _)) $ x) = c $ (nat x)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    66
      | nat t = lit t
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    67
    (*abstraction of a relation: =, <, <=*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    68
    fun rel (T, c $ x $ y) =
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    69
            if T = HOLogic.realT then c $ (rat x) $ (rat y)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    70
            else if T = HOLogic.intT then c $ (int x) $ (int y)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    71
            else if T = HOLogic.natT then c $ (nat x) $ (nat y)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    72
            else if T = HOLogic.boolT then c $ (fm x) $ (fm y)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    73
            else replace (c $ x $ y)   (*non-numeric comparison*)
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    74
    (*abstraction of a formula*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    75
    and fm ((c as Const("op &", _)) $ p $ q) = c $ (fm p) $ (fm q)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    76
      | fm ((c as Const("op |", _)) $ p $ q) = c $ (fm p) $ (fm q)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    77
      | fm ((c as Const("op -->", _)) $ p $ q) = c $ (fm p) $ (fm q)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    78
      | fm ((c as Const("Not", _)) $ p) = c $ (fm p)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    79
      | fm ((c as Const("True", _))) = c
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    80
      | fm ((c as Const("False", _))) = c
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    81
      | fm (t as Const("op =",  Type ("fun", [T,_])) $ _ $ _) = rel (T, t)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    82
      | fm (t as Const("op <",  Type ("fun", [T,_])) $ _ $ _) = rel (T, t)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    83
      | fm (t as Const("op <=", Type ("fun", [T,_])) $ _ $ _) = rel (T, t)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    84
      | fm t = replace t
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    85
    (*entry point, and abstraction of a meta-formula*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    86
    fun mt ((c as Const("Trueprop", _)) $ p) = c $ (fm p)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    87
      | mt ((c as Const("==>", _)) $ p $ q)  = c $ (mt p) $ (mt q)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    88
      | mt t = fm t  (*it might be a formula*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    89
  in (list_all (params, mt body), !pairs) end;
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    90
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    91
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    92
(*Present the entire subgoal to the oracle, assumptions and all, but possibly
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    93
  abstracted.  Use via compose_tac, which performs no lifting but will
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    94
  instantiate variables.*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    95
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    96
fun svc_tac i st =
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    97
  let
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    98
    val (abs_goal, _) = svc_abstract (Logic.get_goal (Thm.prop_of st) i)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    99
    val th = svc_oracle (Thm.theory_of_thm st) abs_goal
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
   100
   in compose_tac (false, th, 0) i end
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
   101
   handle TERM _ => no_tac;
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   102
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   103
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   104
(*check if user has SVC installed*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   105
fun svc_enabled () = getenv "SVC_HOME" <> "";
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   106
fun if_svc_enabled f x = if svc_enabled () then f x else ();