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