src/HOL/ex/SVC_Oracle.ML
author wenzelm
Sun, 01 Oct 2006 18:29:32 +0200
changeset 20813 379ce56e5dc2
parent 19336 fb5e19d26d5e
permissions -rw-r--r--
proper use of svc_oracle.ML;
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
19336
fb5e19d26d5e removed some illegal characters: they were crashing SML/NJ
paulson
parents: 19277
diff changeset
    11
Based upon the work of Soren T. Heilmann
12869
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
18961
9000bb9e6718 Logic.combound;
wenzelm
parents: 17415
diff changeset
    31
            val v = Logic.combound (Var ((!vname,0), Us--->T), 0, nPar)
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    32
        in  vname := Symbol.bump_string (!vname);
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    33
            pairs := (t, v) :: !pairs;
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    34
            v
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    35
        end;
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    36
    fun replace t =
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    37
        case t of
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    38
            Free _  => t  (*but not existing Vars, lest the names clash*)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    39
          | Bound _ => t
17314
04e21a27c0ad introduces some modern-style AList operations
haftmann
parents: 16836
diff changeset
    40
          | _ => (case AList.lookup Pattern.aeconv (!pairs) t of
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    41
                      SOME v => v
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    42
                    | NONE   => insert t)
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    43
    (*abstraction of a numeric literal*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    44
    fun lit (t as Const("0", _)) = t
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    45
      | lit (t as Const("1", _)) = t
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    46
      | lit (t as Const("Numeral.number_of", _) $ w) = t
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    47
      | lit t = replace t
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    48
    (*abstraction of a real/rational expression*)
19233
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 18961
diff changeset
    49
    fun rat ((c as Const("HOL.plus", _)) $ x $ y) = c $ (rat x) $ (rat y)
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 18961
diff changeset
    50
      | rat ((c as Const("HOL.minus", _)) $ x $ y) = c $ (rat x) $ (rat y)
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 18961
diff changeset
    51
      | rat ((c as Const("HOL.divide", _)) $ x $ y) = c $ (rat x) $ (rat y)
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 18961
diff changeset
    52
      | rat ((c as Const("HOL.times", _)) $ x $ y) = c $ (rat x) $ (rat y)
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 18961
diff changeset
    53
      | rat ((c as Const("HOL.uminus", _)) $ x) = c $ (rat x)
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    54
      | rat t = lit t
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    55
    (*abstraction of an integer expression: no div, mod*)
19233
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 18961
diff changeset
    56
    fun int ((c as Const("HOL.plus", _)) $ x $ y) = c $ (int x) $ (int y)
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 18961
diff changeset
    57
      | int ((c as Const("HOL.minus", _)) $ x $ y) = c $ (int x) $ (int y)
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 18961
diff changeset
    58
      | int ((c as Const("HOL.times", _)) $ x $ y) = c $ (int x) $ (int y)
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 18961
diff changeset
    59
      | int ((c as Const("HOL.uminus", _)) $ x) = c $ (int x)
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    60
      | int t = lit t
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    61
    (*abstraction of a natural number expression: no minus*)
19233
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 18961
diff changeset
    62
    fun nat ((c as Const("HOL.plus", _)) $ x $ y) = c $ (nat x) $ (nat y)
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 18961
diff changeset
    63
      | nat ((c as Const("HOL.times", _)) $ x $ y) = c $ (nat x) $ (nat y)
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    64
      | nat ((c as Const("Suc", _)) $ x) = c $ (nat x)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    65
      | nat t = lit t
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    66
    (*abstraction of a relation: =, <, <=*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    67
    fun rel (T, c $ x $ y) =
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    68
            if T = HOLogic.realT then c $ (rat x) $ (rat y)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    69
            else if T = HOLogic.intT then c $ (int x) $ (int y)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    70
            else if T = HOLogic.natT then c $ (nat x) $ (nat y)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    71
            else if T = HOLogic.boolT then c $ (fm x) $ (fm y)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    72
            else replace (c $ x $ y)   (*non-numeric comparison*)
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    73
    (*abstraction of a formula*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    74
    and fm ((c as Const("op &", _)) $ p $ q) = c $ (fm p) $ (fm q)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    75
      | 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("Not", _)) $ p) = c $ (fm p)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    78
      | fm ((c as Const("True", _))) = c
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    79
      | fm ((c as Const("False", _))) = c
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    80
      | fm (t as Const("op =",  Type ("fun", [T,_])) $ _ $ _) = rel (T, t)
19277
f7602e74d948 renamed op < <= to Orderings.less(_eq)
haftmann
parents: 19233
diff changeset
    81
      | fm (t as Const("Orderings.less",  Type ("fun", [T,_])) $ _ $ _) = rel (T, t)
f7602e74d948 renamed op < <= to Orderings.less(_eq)
haftmann
parents: 19233
diff changeset
    82
      | fm (t as Const("Orderings.less_eq", Type ("fun", [T,_])) $ _ $ _) = rel (T, t)
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    83
      | fm t = replace t
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    84
    (*entry point, and abstraction of a meta-formula*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    85
    fun mt ((c as Const("Trueprop", _)) $ p) = c $ (fm p)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    86
      | mt ((c as Const("==>", _)) $ p $ q)  = c $ (mt p) $ (mt q)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    87
      | mt t = fm t  (*it might be a formula*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    88
  in (list_all (params, mt body), !pairs) end;
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    89
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    90
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    91
(*Present the entire subgoal to the oracle, assumptions and all, but possibly
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    92
  abstracted.  Use via compose_tac, which performs no lifting but will
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    93
  instantiate variables.*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
    94
16836
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    95
fun svc_tac i st =
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    96
  let
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    97
    val (abs_goal, _) = svc_abstract (Logic.get_goal (Thm.prop_of st) i)
45a3dc4688bc improved oracle setup;
wenzelm
parents: 15531
diff changeset
    98
    val th = svc_oracle (Thm.theory_of_thm st) abs_goal
17415
ec859c451f59 fixed ML;
wenzelm
parents: 17314
diff changeset
    99
   in compose_tac (false, th, 0) i st end
ec859c451f59 fixed ML;
wenzelm
parents: 17314
diff changeset
   100
   handle TERM _ => no_tac st;
12869
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   101
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   102
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   103
(*check if user has SVC installed*)
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   104
fun svc_enabled () = getenv "SVC_HOME" <> "";
f362c0323d92 moved SVC stuff to ex;
wenzelm
parents:
diff changeset
   105
fun if_svc_enabled f x = if svc_enabled () then f x else ();