src/Tools/Compute_Oracle/compute.ML
author obua
Sat, 27 Oct 2007 18:37:33 +0200
changeset 25218 fcf0f50e478c
parent 25217 3224db6415ae
child 25520 e123c81257a5
permissions -rw-r--r--
better compute oracle
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24584
01e83ffa6c54 fixed title
haftmann
parents: 24137
diff changeset
     1
(*  Title:      Tools/Compute_Oracle/compute.ML
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
     3
    Author:     Steven Obua
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
     4
*)
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
     5
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
     6
signature COMPUTE = sig
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
     7
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
     8
    type computer
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
     9
    type theorem
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
    10
    type naming = int -> string
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
    11
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    12
    datatype machine = BARRAS | BARRAS_COMPILED | HASKELL | SML
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
    13
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
    14
    (* Functions designated with a ! in front of them actually update the computer parameter *)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
    15
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    16
    exception Make of string
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    17
    val make : machine -> theory -> thm list -> computer
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
    18
    val theory_of : computer -> theory
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    19
    val hyps_of : computer -> term list
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    20
    val shyps_of : computer -> sort list
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
    21
    (* ! *) val update : computer -> thm list -> unit
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
    22
    (* ! *) val discard : computer -> unit
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
    23
    
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
    24
    (* ! *) val set_naming : computer -> naming -> unit
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
    25
    val naming_of : computer -> naming
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
    26
    
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
    27
    exception Compute of string    
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
    28
    val simplify : computer -> theorem -> thm 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
    29
    val rewrite : computer -> cterm -> thm 
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    30
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
    31
    val make_theorem : computer -> thm -> string list -> theorem
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
    32
    (* ! *) val instantiate : computer -> (string * cterm) list -> theorem -> theorem
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
    33
    (* ! *) val evaluate_prem : computer -> int -> theorem -> theorem
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
    34
    (* ! *) val modus_ponens : computer -> int -> thm -> theorem -> theorem
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    35
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
    36
    val setup_compute : theory -> theory
24654
329f1b4d9d16 improved computing
obua
parents: 24584
diff changeset
    37
25218
fcf0f50e478c better compute oracle
obua
parents: 25217
diff changeset
    38
    val print_encoding : bool ref
fcf0f50e478c better compute oracle
obua
parents: 25217
diff changeset
    39
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
    40
end
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
    41
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    42
structure Compute :> COMPUTE = struct
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    43
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
    44
open Report;
24654
329f1b4d9d16 improved computing
obua
parents: 24584
diff changeset
    45
25218
fcf0f50e478c better compute oracle
obua
parents: 25217
diff changeset
    46
val print_encoding = ref false
fcf0f50e478c better compute oracle
obua
parents: 25217
diff changeset
    47
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    48
datatype machine = BARRAS | BARRAS_COMPILED | HASKELL | SML	 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    49
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    50
(* Terms are mapped to integer codes *)
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    51
structure Encode :> 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    52
sig
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    53
    type encoding
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    54
    val empty : encoding
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    55
    val insert : term -> encoding -> int * encoding
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    56
    val lookup_code : term -> encoding -> int option
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    57
    val lookup_term : int -> encoding -> term option					
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    58
    val remove_code : int -> encoding -> encoding
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    59
    val remove_term : term -> encoding -> encoding
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    60
    val fold : ((term * int) -> 'a -> 'a) -> encoding -> 'a -> 'a    
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    61
end 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    62
= 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    63
struct
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    64
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    65
type encoding = int * (int Termtab.table) * (term Inttab.table)
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    66
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    67
val empty = (0, Termtab.empty, Inttab.empty)
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    68
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    69
fun insert t (e as (count, term2int, int2term)) = 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    70
    (case Termtab.lookup term2int t of
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    71
	 NONE => (count, (count+1, Termtab.update_new (t, count) term2int, Inttab.update_new (count, t) int2term))
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    72
       | SOME code => (code, e))
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    73
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    74
fun lookup_code t (_, term2int, _) = Termtab.lookup term2int t
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    75
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    76
fun lookup_term c (_, _, int2term) = Inttab.lookup int2term c
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    77
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    78
fun remove_code c (e as (count, term2int, int2term)) = 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    79
    (case lookup_term c e of NONE => e | SOME t => (count, Termtab.delete t term2int, Inttab.delete c int2term))
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    80
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    81
fun remove_term t (e as (count, term2int, int2term)) = 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    82
    (case lookup_code t e of NONE => e | SOME c => (count, Termtab.delete t term2int, Inttab.delete c int2term))
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    83
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    84
fun fold f (_, term2int, _) = Termtab.fold f term2int 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    85
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    86
end
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    87
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
    88
exception Make of string;
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    89
exception Compute of string;
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
    90
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    91
local
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    92
    fun make_constant t ty encoding = 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    93
	let 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    94
	    val (code, encoding) = Encode.insert t encoding 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    95
	in 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    96
	    (encoding, AbstractMachine.Const code)
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    97
	end
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
    98
in
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
    99
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   100
fun remove_types encoding t =
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   101
    case t of 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   102
	Var (_, ty) => make_constant t ty encoding
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   103
      | Free (_, ty) => make_constant t ty encoding
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   104
      | Const (_, ty) => make_constant t ty encoding
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   105
      | Abs (_, ty, t') => 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   106
	let val (encoding, t'') = remove_types encoding t' in
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   107
	    (encoding, AbstractMachine.Abs t'')
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   108
	end
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   109
      | a $ b => 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   110
	let
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   111
	    val (encoding, a) = remove_types encoding a
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   112
	    val (encoding, b) = remove_types encoding b
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   113
	in
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   114
	    (encoding, AbstractMachine.App (a,b))
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   115
	end
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   116
      | Bound b => (encoding, AbstractMachine.Var b)
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   117
end
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   118
    
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   119
local
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   120
    fun type_of (Free (_, ty)) = ty
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   121
      | type_of (Const (_, ty)) = ty
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   122
      | type_of (Var (_, ty)) = ty
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   123
      | type_of _ = sys_error "infer_types: type_of error"
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   124
in
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   125
fun infer_types naming encoding =
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   126
    let
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   127
        fun infer_types _ bounds _ (AbstractMachine.Var v) = (Bound v, List.nth (bounds, v))
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   128
	  | infer_types _ bounds _ (AbstractMachine.Const code) = 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   129
	    let
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   130
		val c = the (Encode.lookup_term code encoding)
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   131
	    in
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   132
		(c, type_of c)
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   133
	    end
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   134
	  | infer_types level bounds _ (AbstractMachine.App (a, b)) = 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   135
	    let
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   136
		val (a, aty) = infer_types level bounds NONE a
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   137
		val (adom, arange) =
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   138
                    case aty of
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   139
                        Type ("fun", [dom, range]) => (dom, range)
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   140
                      | _ => sys_error "infer_types: function type expected"
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   141
                val (b, bty) = infer_types level bounds (SOME adom) b
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   142
	    in
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   143
		(a $ b, arange)
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   144
	    end
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   145
          | infer_types level bounds (SOME (ty as Type ("fun", [dom, range]))) (AbstractMachine.Abs m) =
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   146
            let
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   147
                val (m, _) = infer_types (level+1) (dom::bounds) (SOME range) m
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   148
            in
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   149
                (Abs (naming level, dom, m), ty)
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   150
            end
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   151
          | infer_types _ _ NONE (AbstractMachine.Abs m) = sys_error "infer_types: cannot infer type of abstraction"
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   152
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   153
        fun infer ty term =
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   154
            let
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   155
                val (term', _) = infer_types 0 [] (SOME ty) term
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   156
            in
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   157
                term'
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   158
            end
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   159
    in
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   160
        infer
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   161
    end
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   162
end
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   163
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   164
datatype prog = 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   165
	 ProgBarras of AM_Interpreter.program 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   166
       | ProgBarrasC of AM_Compiler.program
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   167
       | ProgHaskell of AM_GHC.program
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   168
       | ProgSML of AM_SML.program
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   169
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   170
fun machine_of_prog (ProgBarras _) = BARRAS
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   171
  | machine_of_prog (ProgBarrasC _) = BARRAS_COMPILED
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   172
  | machine_of_prog (ProgHaskell _) = HASKELL
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   173
  | machine_of_prog (ProgSML _) = SML
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   174
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   175
structure Sorttab = TableFun(type key = sort val ord = Term.sort_ord)
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   176
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   177
type naming = int -> string
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   178
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   179
fun default_naming i = "v_" ^ Int.toString i
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   180
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   181
datatype computer = Computer of (theory_ref * Encode.encoding * term list * unit Sorttab.table * prog * unit ref * naming) 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   182
                    option ref
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   183
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   184
fun theory_of (Computer (ref (SOME (rthy,_,_,_,_,_,_)))) = Theory.deref rthy
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   185
fun hyps_of (Computer (ref (SOME (_,_,hyps,_,_,_,_)))) = hyps
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   186
fun shyps_of (Computer (ref (SOME (_,_,_,shyptable,_,_,_)))) = Sorttab.keys (shyptable)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   187
fun shyptab_of (Computer (ref (SOME (_,_,_,shyptable,_,_,_)))) = shyptable
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   188
fun stamp_of (Computer (ref (SOME (_,_,_,_,_,stamp,_)))) = stamp
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   189
fun prog_of (Computer (ref (SOME (_,_,_,_,prog,_,_)))) = prog
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   190
fun encoding_of (Computer (ref (SOME (_,encoding,_,_,_,_,_)))) = encoding
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   191
fun set_encoding (Computer (r as ref (SOME (p1,encoding,p2,p3,p4,p5,p6)))) encoding' = 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   192
    (r := SOME (p1,encoding',p2,p3,p4,p5,p6))
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   193
fun naming_of (Computer (ref (SOME (_,_,_,_,_,_,n)))) = n
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   194
fun set_naming (Computer (r as ref (SOME (p1,p2,p3,p4,p5,p6,naming)))) naming'= 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   195
    (r := SOME (p1,p2,p3,p4,p5,p6,naming'))
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   196
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   197
fun ref_of (Computer r) = r
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   198
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   199
datatype cthm = ComputeThm of term list * sort list * term
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   200
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   201
fun thm2cthm th = 
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   202
    let
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   203
	val {hyps, prop, tpairs, shyps, ...} = Thm.rep_thm th
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   204
	val _ = if not (null tpairs) then raise Make "theorems may not contain tpairs" else ()
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   205
    in
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   206
	ComputeThm (hyps, shyps, prop)
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   207
    end
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   208
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   209
fun make_internal machine thy stamp encoding raw_ths =
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   210
    let
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   211
	fun transfer (x:thm) = Thm.transfer thy x
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   212
	val ths = map (thm2cthm o Thm.strip_shyps o transfer) raw_ths
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   213
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   214
        fun thm2rule (encoding, hyptable, shyptable) th =
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   215
            let
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   216
		val (ComputeThm (hyps, shyps, prop)) = th
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   217
		val hyptable = fold (fn h => Termtab.update (h, ())) hyps hyptable
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   218
		val shyptable = fold (fn sh => Sorttab.update (sh, ())) shyps shyptable
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   219
		val (prems, prop) = (Logic.strip_imp_prems prop, Logic.strip_imp_concl prop)
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   220
                val (a, b) = Logic.dest_equals prop
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   221
                  handle TERM _ => raise (Make "theorems must be meta-level equations (with optional guards)")
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   222
		val a = Envir.eta_contract a
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   223
		val b = Envir.eta_contract b
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   224
		val prems = map Envir.eta_contract prems
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   225
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   226
                val (encoding, left) = remove_types encoding a     
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   227
		val (encoding, right) = remove_types encoding b  
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   228
                fun remove_types_of_guard encoding g = 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   229
		    (let
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   230
			 val (t1, t2) = Logic.dest_equals g 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   231
			 val (encoding, t1) = remove_types encoding t1
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   232
			 val (encoding, t2) = remove_types encoding t2
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   233
		     in
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   234
			 (encoding, AbstractMachine.Guard (t1, t2))
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   235
		     end handle TERM _ => raise (Make "guards must be meta-level equations"))
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   236
                val (encoding, prems) = fold_rev (fn p => fn (encoding, ps) => let val (e, p) = remove_types_of_guard encoding p in (e, p::ps) end) prems (encoding, [])
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   237
                
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   238
                fun make_pattern encoding n vars (var as AbstractMachine.Abs _) =
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   239
		    raise (Make "no lambda abstractions allowed in pattern")
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   240
		  | make_pattern encoding n vars (var as AbstractMachine.Var _) =
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   241
		    raise (Make "no bound variables allowed in pattern")
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   242
		  | make_pattern encoding n vars (AbstractMachine.Const code) =
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   243
		    (case the (Encode.lookup_term code encoding) of
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   244
			 Var _ => ((n+1, Inttab.update_new (code, n) vars, AbstractMachine.PVar)
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   245
				   handle Inttab.DUP _ => raise (Make "no duplicate variable in pattern allowed"))
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   246
		       | _ => (n, vars, AbstractMachine.PConst (code, [])))
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   247
                  | make_pattern encoding n vars (AbstractMachine.App (a, b)) =
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   248
                    let
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   249
                        val (n, vars, pa) = make_pattern encoding n vars a
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   250
                        val (n, vars, pb) = make_pattern encoding n vars b
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   251
                    in
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   252
                        case pa of
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   253
                            AbstractMachine.PVar =>
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   254
                              raise (Make "patterns may not start with a variable")
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   255
                          | AbstractMachine.PConst (c, args) =>
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   256
                              (n, vars, AbstractMachine.PConst (c, args@[pb]))
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   257
                    end
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   258
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   259
                (* Principally, a check should be made here to see if the (meta-) hyps contain any of the variables of the rule.
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   260
                   As it is, all variables of the rule are schematic, and there are no schematic variables in meta-hyps, therefore
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   261
                   this check can be left out. *)
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   262
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   263
                val (vcount, vars, pattern) = make_pattern encoding 0 Inttab.empty left
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   264
                val _ = (case pattern of
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   265
                             AbstractMachine.PVar =>
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   266
                             raise (Make "patterns may not start with a variable")
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   267
                         (*  | AbstractMachine.PConst (_, []) => 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   268
			     (print th; raise (Make "no parameter rewrite found"))*)
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   269
			   | _ => ())
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   270
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   271
                (* finally, provide a function for renaming the
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   272
                   pattern bound variables on the right hand side *)
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   273
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   274
                fun rename level vars (var as AbstractMachine.Var _) = var
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   275
		  | rename level vars (c as AbstractMachine.Const code) =
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   276
		    (case Inttab.lookup vars code of 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   277
			 NONE => c 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   278
		       | SOME n => AbstractMachine.Var (vcount-n-1+level))
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   279
                  | rename level vars (AbstractMachine.App (a, b)) =
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   280
                    AbstractMachine.App (rename level vars a, rename level vars b)
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   281
                  | rename level vars (AbstractMachine.Abs m) =
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   282
                    AbstractMachine.Abs (rename (level+1) vars m)
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   283
		    
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   284
		fun rename_guard (AbstractMachine.Guard (a,b)) = 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   285
		    AbstractMachine.Guard (rename 0 vars a, rename 0 vars b)
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   286
            in
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   287
                ((encoding, hyptable, shyptable), (map rename_guard prems, pattern, rename 0 vars right))
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   288
            end
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   289
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   290
        val ((encoding, hyptable, shyptable), rules) =
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   291
          fold_rev (fn th => fn (encoding_hyptable, rules) =>
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   292
            let
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   293
              val (encoding_hyptable, rule) = thm2rule encoding_hyptable th
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   294
            in (encoding_hyptable, rule::rules) end)
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   295
          ths ((encoding, Termtab.empty, Sorttab.empty), [])
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   296
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   297
        val prog = 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   298
	    case machine of 
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   299
		BARRAS => ProgBarras (AM_Interpreter.compile rules)
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   300
	      | BARRAS_COMPILED => ProgBarrasC (AM_Compiler.compile rules)
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   301
	      | HASKELL => ProgHaskell (AM_GHC.compile rules)
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   302
	      | SML => ProgSML (AM_SML.compile rules)
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   303
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   304
        fun has_witness s = not (null (Sign.witness_sorts thy [] [s]))
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   305
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   306
	val shyptable = fold Sorttab.delete (filter has_witness (Sorttab.keys (shyptable))) shyptable
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   307
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   308
    in (Theory.check_thy thy, encoding, Termtab.keys hyptable, shyptable, prog, stamp, default_naming) end
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   309
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   310
fun make machine thy raw_thms = Computer (ref (SOME (make_internal machine thy (ref ()) Encode.empty raw_thms)))
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   311
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   312
fun update computer raw_thms =
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   313
    let 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   314
	val c = make_internal (machine_of_prog (prog_of computer)) (theory_of computer) (stamp_of computer) 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   315
			      (encoding_of computer) raw_thms
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   316
	val _ = (ref_of computer) := SOME c	
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   317
    in
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   318
	()
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   319
    end
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   320
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   321
fun discard computer =
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   322
    let
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   323
	val _ = 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   324
	    case prog_of computer of
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   325
		ProgBarras p => AM_Interpreter.discard p
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   326
	      | ProgBarrasC p => AM_Compiler.discard p
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   327
	      | ProgHaskell p => AM_GHC.discard p
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   328
	      | ProgSML p => AM_SML.discard p
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   329
	val _ = (ref_of computer) := NONE
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   330
    in
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   331
	()
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   332
    end 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   333
					      
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   334
fun runprog (ProgBarras p) = AM_Interpreter.run p
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   335
  | runprog (ProgBarrasC p) = AM_Compiler.run p
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   336
  | runprog (ProgHaskell p) = AM_GHC.run p
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   337
  | runprog (ProgSML p) = AM_SML.run p	  
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   338
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   339
(* ------------------------------------------------------------------------------------- *)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   340
(* An oracle for exporting theorems; must only be accessible from inside this structure! *)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   341
(* ------------------------------------------------------------------------------------- *)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   342
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   343
exception ExportThm of term list * sort list * term
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   344
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   345
fun merge_hyps hyps1 hyps2 = 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   346
let
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   347
    fun add hyps tab = fold (fn h => fn tab => Termtab.update (h, ()) tab) hyps tab
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   348
in
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   349
    Termtab.keys (add hyps2 (add hyps1 Termtab.empty))
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   350
end
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   351
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   352
fun add_shyps shyps tab = fold (fn h => fn tab => Sorttab.update (h, ()) tab) shyps tab
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   353
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   354
fun merge_shyps shyps1 shyps2 = Sorttab.keys (add_shyps shyps2 (add_shyps shyps1 Sorttab.empty))
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   355
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   356
fun export_oracle (thy, ExportThm (hyps, shyps, prop)) =
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   357
    let
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   358
	val shyptab = add_shyps shyps Sorttab.empty
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   359
	fun delete s shyptab = Sorttab.delete s shyptab handle Sorttab.UNDEF _ => shyptab
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   360
	fun delete_term t shyptab = fold delete (Sorts.insert_term t []) shyptab
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   361
	fun has_witness s = not (null (Sign.witness_sorts thy [] [s]))
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   362
	val shyptab = fold Sorttab.delete (filter has_witness (Sorttab.keys (shyptab))) shyptab
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   363
	val shyps = if Sorttab.is_empty shyptab then [] else Sorttab.keys (fold delete_term (prop::hyps) shyptab)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   364
	val _ = if not (null shyps) then raise Compute ("dangling sort hypotheses: "^(makestring shyps)) else ()
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   365
    in
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   366
	fold_rev (fn hyp => fn p => Logic.mk_implies (hyp, p)) hyps prop
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   367
    end
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   368
  | export_oracle _ = raise Match
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   369
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   370
val setup_compute = (fn thy => Theory.add_oracle ("compute", export_oracle) thy)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   371
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   372
fun export_thm thy hyps shyps prop =
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   373
    let
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   374
	val th = invoke_oracle_i thy "Compute_Oracle.compute" (thy, ExportThm (hyps, shyps, prop))
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   375
	val hyps = map (fn h => assume (cterm_of thy h)) hyps
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   376
    in
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   377
	fold (fn h => fn p => implies_elim p h) hyps th 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   378
    end
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   379
	
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   380
(* --------- Rewrite ----------- *)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   381
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   382
fun rewrite computer ct =
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   383
    let
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   384
	val {t=t',T=ty,thy=thy,...} = rep_cterm ct
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   385
	val _ = Theory.assert_super (theory_of computer) thy
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   386
	val naming = naming_of computer
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   387
        val (encoding, t) = remove_types (encoding_of computer) t'
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   388
	(*val _ = if (!print_encoding) then writeln (makestring ("encoding: ",Encode.fold (fn x => fn s => x::s) encoding [])) else ()*)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   389
        val t = runprog (prog_of computer) t
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   390
        val t = infer_types naming encoding ty t
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   391
	val eq = Logic.mk_equals (t', t)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   392
    in
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   393
        export_thm thy (hyps_of computer) (Sorttab.keys (shyptab_of computer)) eq
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   394
    end
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   395
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   396
(* --------- Simplify ------------ *)
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   397
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   398
datatype prem = EqPrem of AbstractMachine.term * AbstractMachine.term * Term.typ * int 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   399
	      | Prem of AbstractMachine.term
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   400
datatype theorem = Theorem of theory_ref * unit ref * (int * typ) Symtab.table * (AbstractMachine.term option) Inttab.table  
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   401
               * prem list * AbstractMachine.term * term list * sort list
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   402
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   403
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   404
exception ParamSimplify of computer * theorem
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   405
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   406
fun make_theorem computer th vars =
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   407
let
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   408
    val _ = Theory.assert_super (theory_of computer) (theory_of_thm th)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   409
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   410
    val (ComputeThm (hyps, shyps, prop)) = thm2cthm th 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   411
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   412
    val encoding = encoding_of computer
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   413
 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   414
    (* variables in the theorem are identified upfront *)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   415
    fun collect_vars (Abs (_, _, t)) tab = collect_vars t tab
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   416
      | collect_vars (a $ b) tab = collect_vars b (collect_vars a tab)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   417
      | collect_vars (Const _) tab = tab
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   418
      | collect_vars (Free _) tab = tab
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   419
      | collect_vars (Var ((s, i), ty)) tab = 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   420
	    if List.find (fn x => x=s) vars = NONE then 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   421
		tab
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   422
	    else		
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   423
		(case Symtab.lookup tab s of
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   424
		     SOME ((s',i'),ty') => 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   425
	             if s' <> s orelse i' <> i orelse ty <> ty' then 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   426
			 raise Compute ("make_theorem: variable name '"^s^"' is not unique")
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   427
		     else 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   428
			 tab
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   429
		   | NONE => Symtab.update (s, ((s, i), ty)) tab)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   430
    val vartab = collect_vars prop Symtab.empty 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   431
    fun encodevar (s, t as (_, ty)) (encoding, tab) = 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   432
	let
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   433
	    val (x, encoding) = Encode.insert (Var t) encoding
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   434
	in
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   435
	    (encoding, Symtab.update (s, (x, ty)) tab)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   436
	end
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   437
    val (encoding, vartab) = Symtab.fold encodevar vartab (encoding, Symtab.empty)  					 	       
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   438
    val varsubst = Inttab.make (map (fn (s, (x, _)) => (x, NONE)) (Symtab.dest vartab))
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   439
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   440
    (* make the premises and the conclusion *)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   441
    fun mk_prem encoding t = 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   442
	(let
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   443
	     val (a, b) = Logic.dest_equals t
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   444
	     val ty = type_of a
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   445
	     val (encoding, a) = remove_types encoding a
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   446
	     val (encoding, b) = remove_types encoding b
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   447
	     val (eq, encoding) = Encode.insert (Const ("==", ty --> ty --> @{typ "prop"})) encoding 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   448
	 in
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   449
	     (encoding, EqPrem (a, b, ty, eq))
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   450
	 end handle TERM _ => let val (encoding, t) = remove_types encoding t in (encoding, Prem t) end)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   451
    val (encoding, prems) = 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   452
	(fold_rev (fn t => fn (encoding, l) => 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   453
	    case mk_prem encoding t  of 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   454
                (encoding, t) => (encoding, t::l)) (Logic.strip_imp_prems prop) (encoding, []))
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   455
    val (encoding, concl) = remove_types encoding (Logic.strip_imp_concl prop)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   456
    val _ = set_encoding computer encoding
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   457
in
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   458
    Theorem (Theory.check_thy (theory_of_thm th), stamp_of computer, vartab, varsubst, 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   459
	     prems, concl, hyps, shyps)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   460
end
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   461
    
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   462
fun theory_of_theorem (Theorem (rthy,_,_,_,_,_,_,_)) = Theory.deref rthy
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   463
fun update_theory thy (Theorem (_,p0,p1,p2,p3,p4,p5,p6)) =
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   464
    Theorem (Theory.check_thy thy,p0,p1,p2,p3,p4,p5,p6)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   465
fun stamp_of_theorem (Theorem (_,s, _, _, _, _, _, _)) = s     
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   466
fun vartab_of_theorem (Theorem (_,_,vt,_,_,_,_,_)) = vt
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   467
fun varsubst_of_theorem (Theorem (_,_,_,vs,_,_,_,_)) = vs 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   468
fun update_varsubst vs (Theorem (p0,p1,p2,_,p3,p4,p5,p6)) = Theorem (p0,p1,p2,vs,p3,p4,p5,p6)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   469
fun prems_of_theorem (Theorem (_,_,_,_,prems,_,_,_)) = prems
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   470
fun update_prems prems (Theorem (p0,p1,p2,p3,_,p4,p5,p6)) = Theorem (p0,p1,p2,p3,prems,p4,p5,p6)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   471
fun concl_of_theorem (Theorem (_,_,_,_,_,concl,_,_)) = concl
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   472
fun hyps_of_theorem (Theorem (_,_,_,_,_,_,hyps,_)) = hyps
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   473
fun update_hyps hyps (Theorem (p0,p1,p2,p3,p4,p5,_,p6)) = Theorem (p0,p1,p2,p3,p4,p5,hyps,p6)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   474
fun shyps_of_theorem (Theorem (_,_,_,_,_,_,_,shyps)) = shyps
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   475
fun update_shyps shyps (Theorem (p0,p1,p2,p3,p4,p5,p6,_)) = Theorem (p0,p1,p2,p3,p4,p5,p6,shyps)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   476
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   477
fun check_compatible computer th s = 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   478
    if stamp_of computer <> stamp_of_theorem th then
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   479
	raise Compute (s^": computer and theorem are incompatible")
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   480
    else ()
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   481
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   482
fun instantiate computer insts th =
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   483
let
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   484
    val _ = check_compatible computer th
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   485
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   486
    val thy = theory_of computer
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   487
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   488
    val vartab = vartab_of_theorem th
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   489
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   490
    fun rewrite computer t =
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   491
    let  
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   492
	val naming = naming_of computer
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   493
        val (encoding, t) = remove_types (encoding_of computer) t
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   494
        val t = runprog (prog_of computer) t
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   495
	val _ = set_encoding computer encoding
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   496
    in
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   497
        t
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   498
    end
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   499
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   500
    fun assert_varfree vs t = 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   501
	if AbstractMachine.forall_consts (fn x => Inttab.lookup vs x = NONE) t then
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   502
	    ()
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   503
	else
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   504
	    raise Compute "instantiate: assert_varfree failed"
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   505
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   506
    fun assert_closed t =
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   507
	if AbstractMachine.closed t then
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   508
	    ()
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   509
	else 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   510
	    raise Compute "instantiate: not a closed term"
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   511
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   512
    fun compute_inst (s, ct) vs =
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   513
	let
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   514
	    val _ = Theory.assert_super (theory_of_cterm ct) thy
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   515
	    val ty = typ_of (ctyp_of_term ct) 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   516
	in	    
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   517
	    (case Symtab.lookup vartab s of 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   518
		 NONE => raise Compute ("instantiate: variable '"^s^"' not found in theorem")
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   519
	       | SOME (x, ty') => 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   520
		 (case Inttab.lookup vs x of 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   521
		      SOME (SOME _) => raise Compute ("instantiate: variable '"^s^"' has already been instantiated")
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   522
		    | SOME NONE => 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   523
		      if ty <> ty' then 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   524
			  raise Compute ("instantiate: wrong type for variable '"^s^"'")
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   525
		      else
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   526
			  let
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   527
			      val t = rewrite computer (term_of ct)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   528
			      val _ = assert_varfree vs t 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   529
			      val _ = assert_closed t
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   530
			  in
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   531
			      Inttab.update (x, SOME t) vs
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   532
			  end
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   533
		    | NONE => raise Compute "instantiate: internal error"))
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   534
	end
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   535
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   536
    val vs = fold compute_inst insts (varsubst_of_theorem th)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   537
in
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   538
    update_varsubst vs th
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   539
end
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   540
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   541
fun match_aterms subst =
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   542
    let	
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   543
	exception no_match
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   544
	open AbstractMachine
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   545
	fun match subst (b as (Const c)) a = 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   546
	    if a = b then subst
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   547
	    else 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   548
		(case Inttab.lookup subst c of 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   549
		     SOME (SOME a') => if a=a' then subst else raise no_match
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   550
		   | SOME NONE => if AbstractMachine.closed a then 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   551
				      Inttab.update (c, SOME a) subst 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   552
				  else raise no_match
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   553
		   | NONE => raise no_match)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   554
	  | match subst (b as (Var _)) a = if a=b then subst else raise no_match
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   555
	  | match subst (App (u, v)) (App (u', v')) = match (match subst u u') v v'
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   556
	  | match subst (Abs u) (Abs u') = match subst u u'
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   557
	  | match subst _ _ = raise no_match
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   558
    in
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   559
	fn b => fn a => (SOME (match subst b a) handle no_match => NONE)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   560
    end
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   561
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   562
fun apply_subst vars_allowed subst =
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   563
    let
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   564
	open AbstractMachine
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   565
	fun app (t as (Const c)) = 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   566
	    (case Inttab.lookup subst c of 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   567
		 NONE => t 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   568
	       | SOME (SOME t) => Computed t
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   569
	       | SOME NONE => if vars_allowed then t else raise Compute "apply_subst: no vars allowed")
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   570
	  | app (t as (Var _)) = t
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   571
	  | app (App (u, v)) = App (app u, app v)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   572
	  | app (Abs m) = Abs (app m)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   573
    in
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   574
	app
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   575
    end
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   576
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   577
fun splicein n l L = List.take (L, n) @ l @ List.drop (L, n+1)
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   578
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   579
fun evaluate_prem computer prem_no th =
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   580
let
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   581
    val _ = check_compatible computer th
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   582
    val prems = prems_of_theorem th
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   583
    val varsubst = varsubst_of_theorem th
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   584
    fun run vars_allowed t = 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   585
	runprog (prog_of computer) (apply_subst vars_allowed varsubst t)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   586
in
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   587
    case List.nth (prems, prem_no) of
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   588
	Prem _ => raise Compute "evaluate_prem: no equality premise"
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   589
      | EqPrem (a, b, ty, _) => 	
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   590
	let
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   591
	    val a' = run false a
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   592
	    val b' = run true b
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   593
	in
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   594
	    case match_aterms varsubst b' a' of
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   595
		NONE => 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   596
		let
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   597
		    fun mk s = makestring (infer_types (naming_of computer) (encoding_of computer) ty s)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   598
		    val left = "computed left side: "^(mk a')
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   599
		    val right = "computed right side: "^(mk b')
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   600
		in
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   601
		    raise Compute ("evaluate_prem: cannot assign computed left to right hand side\n"^left^"\n"^right^"\n")
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   602
		end
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   603
	      | SOME varsubst => 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   604
		update_prems (splicein prem_no [] prems) (update_varsubst varsubst th)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   605
	end
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   606
end
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   607
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   608
fun prem2term (Prem t) = t
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   609
  | prem2term (EqPrem (a,b,_,eq)) = 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   610
    AbstractMachine.App (AbstractMachine.App (AbstractMachine.Const eq, a), b)
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   611
25217
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   612
fun modus_ponens computer prem_no th' th = 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   613
let
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   614
    val _ = check_compatible computer th
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   615
    val thy = 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   616
	let
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   617
	    val thy1 = theory_of_theorem th
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   618
	    val thy2 = theory_of_thm th'
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   619
	in
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   620
	    if Context.subthy (thy1, thy2) then thy2 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   621
	    else if Context.subthy (thy2, thy1) then thy1 else
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   622
	    raise Compute "modus_ponens: theorems are not compatible with each other"
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   623
	end 
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   624
    val th' = make_theorem computer th' []
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   625
    val varsubst = varsubst_of_theorem th
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   626
    fun run vars_allowed t =
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   627
	runprog (prog_of computer) (apply_subst vars_allowed varsubst t)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   628
    val prems = prems_of_theorem th
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   629
    val prem = run true (prem2term (List.nth (prems, prem_no)))
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   630
    val concl = run false (concl_of_theorem th')    
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   631
in
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   632
    case match_aterms varsubst prem concl of
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   633
	NONE => raise Compute "modus_ponens: conclusion does not match premise"
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   634
      | SOME varsubst =>
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   635
	let
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   636
	    val th = update_varsubst varsubst th
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   637
	    val th = update_prems (splicein prem_no (prems_of_theorem th') prems) th
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   638
	    val th = update_hyps (merge_hyps (hyps_of_theorem th) (hyps_of_theorem th')) th
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   639
	    val th = update_shyps (merge_shyps (shyps_of_theorem th) (shyps_of_theorem th')) th
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   640
	in
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   641
	    update_theory thy th
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   642
	end
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   643
end
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   644
                     
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   645
fun simplify computer th =
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   646
let
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   647
    val _ = check_compatible computer th
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   648
    val varsubst = varsubst_of_theorem th
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   649
    val encoding = encoding_of computer
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   650
    val naming = naming_of computer
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   651
    fun infer t = infer_types naming encoding @{typ "prop"} t
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   652
    fun run t = infer (runprog (prog_of computer) (apply_subst true varsubst t))
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   653
    fun runprem p = run (prem2term p)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   654
    val prop = Logic.list_implies (map runprem (prems_of_theorem th), run (concl_of_theorem th))
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   655
    val hyps = merge_hyps (hyps_of computer) (hyps_of_theorem th)
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   656
    val shyps = merge_shyps (shyps_of_theorem th) (Sorttab.keys (shyptab_of computer))
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   657
in
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   658
    export_thm (theory_of_theorem th) hyps shyps prop
3224db6415ae better compute oracle
obua
parents: 24654
diff changeset
   659
end
23174
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   660
3913451b0418 moved Compute_Oracle from Pure/Tools to Tools;
wenzelm
parents:
diff changeset
   661
end
23663
84b5c89b8b49 new version of computing oracle
obua
parents: 23174
diff changeset
   662