src/Tools/Compute_Oracle/compute.ML
author wenzelm
Tue Sep 29 16:24:36 2009 +0200 (2009-09-29)
changeset 32740 9dd0a2f83429
parent 31976 17414e2736f4
child 36945 9bec62c10714
permissions -rw-r--r--
explicit indication of Unsynchronized.ref;
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
obua@25217
   170
type naming = int -> string
obua@25217
   171
obua@25217
   172
fun default_naming i = "v_" ^ Int.toString i
obua@25217
   173
wenzelm@32740
   174
datatype computer = Computer of
wenzelm@32740
   175
  (theory_ref * Encode.encoding * term list * unit Sorttab.table * prog * unit Unsynchronized.ref * naming)
wenzelm@32740
   176
    option Unsynchronized.ref
obua@25217
   177
wenzelm@32740
   178
fun theory_of (Computer (Unsynchronized.ref (SOME (rthy,_,_,_,_,_,_)))) = Theory.deref rthy
wenzelm@32740
   179
fun hyps_of (Computer (Unsynchronized.ref (SOME (_,_,hyps,_,_,_,_)))) = hyps
wenzelm@32740
   180
fun shyps_of (Computer (Unsynchronized.ref (SOME (_,_,_,shyptable,_,_,_)))) = Sorttab.keys (shyptable)
wenzelm@32740
   181
fun shyptab_of (Computer (Unsynchronized.ref (SOME (_,_,_,shyptable,_,_,_)))) = shyptable
wenzelm@32740
   182
fun stamp_of (Computer (Unsynchronized.ref (SOME (_,_,_,_,_,stamp,_)))) = stamp
wenzelm@32740
   183
fun prog_of (Computer (Unsynchronized.ref (SOME (_,_,_,_,prog,_,_)))) = prog
wenzelm@32740
   184
fun encoding_of (Computer (Unsynchronized.ref (SOME (_,encoding,_,_,_,_,_)))) = encoding
wenzelm@32740
   185
fun set_encoding (Computer (r as Unsynchronized.ref (SOME (p1,encoding,p2,p3,p4,p5,p6)))) encoding' = 
obua@25217
   186
    (r := SOME (p1,encoding',p2,p3,p4,p5,p6))
wenzelm@32740
   187
fun naming_of (Computer (Unsynchronized.ref (SOME (_,_,_,_,_,_,n)))) = n
wenzelm@32740
   188
fun set_naming (Computer (r as Unsynchronized.ref (SOME (p1,p2,p3,p4,p5,p6,naming)))) naming'= 
obua@25217
   189
    (r := SOME (p1,p2,p3,p4,p5,p6,naming'))
obua@25217
   190
obua@25217
   191
fun ref_of (Computer r) = r
obua@23663
   192
obua@23663
   193
datatype cthm = ComputeThm of term list * sort list * term
obua@23663
   194
obua@23663
   195
fun thm2cthm th = 
wenzelm@23174
   196
    let
wenzelm@26626
   197
        val {hyps, prop, tpairs, shyps, ...} = Thm.rep_thm th
wenzelm@26626
   198
        val _ = if not (null tpairs) then raise Make "theorems may not contain tpairs" else ()
obua@23663
   199
    in
wenzelm@26626
   200
        ComputeThm (hyps, shyps, prop)
obua@23663
   201
    end
wenzelm@23174
   202
obua@25520
   203
fun make_internal machine thy stamp encoding cache_pattern_terms raw_ths =
obua@23663
   204
    let
wenzelm@26626
   205
        fun transfer (x:thm) = Thm.transfer thy x
wenzelm@26626
   206
        val ths = map (thm2cthm o Thm.strip_shyps o transfer) raw_ths
wenzelm@23174
   207
obua@25520
   208
        fun make_pattern encoding n vars (var as AbstractMachine.Abs _) =
wenzelm@26626
   209
            raise (Make "no lambda abstractions allowed in pattern")
wenzelm@26626
   210
          | make_pattern encoding n vars (var as AbstractMachine.Var _) =
wenzelm@26626
   211
            raise (Make "no bound variables allowed in pattern")
wenzelm@26626
   212
          | make_pattern encoding n vars (AbstractMachine.Const code) =
wenzelm@26626
   213
            (case the (Encode.lookup_term code encoding) of
wenzelm@26626
   214
                 Var _ => ((n+1, Inttab.update_new (code, n) vars, AbstractMachine.PVar)
wenzelm@26626
   215
                           handle Inttab.DUP _ => raise (Make "no duplicate variable in pattern allowed"))
wenzelm@26626
   216
               | _ => (n, vars, AbstractMachine.PConst (code, [])))
obua@25520
   217
          | make_pattern encoding n vars (AbstractMachine.App (a, b)) =
obua@25520
   218
            let
obua@25520
   219
                val (n, vars, pa) = make_pattern encoding n vars a
obua@25520
   220
                val (n, vars, pb) = make_pattern encoding n vars b
obua@25520
   221
            in
obua@25520
   222
                case pa of
obua@25520
   223
                    AbstractMachine.PVar =>
obua@25520
   224
                    raise (Make "patterns may not start with a variable")
obua@25520
   225
                  | AbstractMachine.PConst (c, args) =>
obua@25520
   226
                    (n, vars, AbstractMachine.PConst (c, args@[pb]))
obua@25520
   227
            end
obua@25520
   228
obua@23663
   229
        fun thm2rule (encoding, hyptable, shyptable) th =
obua@23663
   230
            let
wenzelm@26626
   231
                val (ComputeThm (hyps, shyps, prop)) = th
wenzelm@26626
   232
                val hyptable = fold (fn h => Termtab.update (h, ())) hyps hyptable
wenzelm@26626
   233
                val shyptable = fold (fn sh => Sorttab.update (sh, ())) shyps shyptable
wenzelm@26626
   234
                val (prems, prop) = (Logic.strip_imp_prems prop, Logic.strip_imp_concl prop)
obua@23663
   235
                val (a, b) = Logic.dest_equals prop
obua@23663
   236
                  handle TERM _ => raise (Make "theorems must be meta-level equations (with optional guards)")
wenzelm@26626
   237
                val a = Envir.eta_contract a
wenzelm@26626
   238
                val b = Envir.eta_contract b
wenzelm@26626
   239
                val prems = map Envir.eta_contract prems
wenzelm@23174
   240
obua@23663
   241
                val (encoding, left) = remove_types encoding a     
wenzelm@26626
   242
                val (encoding, right) = remove_types encoding b  
obua@23663
   243
                fun remove_types_of_guard encoding g = 
wenzelm@26626
   244
                    (let
wenzelm@26626
   245
                         val (t1, t2) = Logic.dest_equals g 
wenzelm@26626
   246
                         val (encoding, t1) = remove_types encoding t1
wenzelm@26626
   247
                         val (encoding, t2) = remove_types encoding t2
wenzelm@26626
   248
                     in
wenzelm@26626
   249
                         (encoding, AbstractMachine.Guard (t1, t2))
wenzelm@26626
   250
                     end handle TERM _ => raise (Make "guards must be meta-level equations"))
obua@23663
   251
                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
   252
obua@23663
   253
                (* Principally, a check should be made here to see if the (meta-) hyps contain any of the variables of the rule.
obua@23663
   254
                   As it is, all variables of the rule are schematic, and there are no schematic variables in meta-hyps, therefore
obua@23663
   255
                   this check can be left out. *)
obua@23663
   256
obua@23663
   257
                val (vcount, vars, pattern) = make_pattern encoding 0 Inttab.empty left
wenzelm@23174
   258
                val _ = (case pattern of
obua@23663
   259
                             AbstractMachine.PVar =>
wenzelm@23174
   260
                             raise (Make "patterns may not start with a variable")
obua@23663
   261
                         (*  | AbstractMachine.PConst (_, []) => 
wenzelm@26626
   262
                             (print th; raise (Make "no parameter rewrite found"))*)
wenzelm@26626
   263
                           | _ => ())
wenzelm@23174
   264
wenzelm@23174
   265
                (* finally, provide a function for renaming the
obua@23663
   266
                   pattern bound variables on the right hand side *)
wenzelm@23174
   267
obua@23663
   268
                fun rename level vars (var as AbstractMachine.Var _) = var
wenzelm@26626
   269
                  | rename level vars (c as AbstractMachine.Const code) =
wenzelm@26626
   270
                    (case Inttab.lookup vars code of 
wenzelm@26626
   271
                         NONE => c 
wenzelm@26626
   272
                       | SOME n => AbstractMachine.Var (vcount-n-1+level))
obua@23663
   273
                  | rename level vars (AbstractMachine.App (a, b)) =
obua@23663
   274
                    AbstractMachine.App (rename level vars a, rename level vars b)
obua@23663
   275
                  | rename level vars (AbstractMachine.Abs m) =
obua@23663
   276
                    AbstractMachine.Abs (rename (level+1) vars m)
wenzelm@26626
   277
                    
wenzelm@26626
   278
                fun rename_guard (AbstractMachine.Guard (a,b)) = 
wenzelm@26626
   279
                    AbstractMachine.Guard (rename 0 vars a, rename 0 vars b)
wenzelm@23174
   280
            in
obua@23663
   281
                ((encoding, hyptable, shyptable), (map rename_guard prems, pattern, rename 0 vars right))
wenzelm@23174
   282
            end
wenzelm@23174
   283
obua@23663
   284
        val ((encoding, hyptable, shyptable), rules) =
obua@23663
   285
          fold_rev (fn th => fn (encoding_hyptable, rules) =>
wenzelm@23174
   286
            let
obua@23663
   287
              val (encoding_hyptable, rule) = thm2rule encoding_hyptable th
obua@23663
   288
            in (encoding_hyptable, rule::rules) end)
obua@25217
   289
          ths ((encoding, Termtab.empty, Sorttab.empty), [])
wenzelm@23174
   290
obua@25520
   291
        fun make_cache_pattern t (encoding, cache_patterns) =
wenzelm@26626
   292
            let
wenzelm@26626
   293
                val (encoding, a) = remove_types encoding t
wenzelm@26626
   294
                val (_,_,p) = make_pattern encoding 0 Inttab.empty a
wenzelm@26626
   295
            in
wenzelm@26626
   296
                (encoding, p::cache_patterns)
wenzelm@26626
   297
            end
wenzelm@26626
   298
        
wenzelm@26626
   299
        val (encoding, cache_patterns) = fold_rev make_cache_pattern cache_pattern_terms (encoding, [])
obua@25520
   300
wenzelm@26626
   301
        fun arity (Type ("fun", [a,b])) = 1 + arity b
wenzelm@26626
   302
          | arity _ = 0
obua@25520
   303
wenzelm@26626
   304
        fun make_arity (Const (s, _), i) tab = 
wenzelm@26626
   305
            (Inttab.update (i, arity (Sign.the_const_type thy s)) tab handle TYPE _ => tab)
wenzelm@26626
   306
          | make_arity _ tab = tab
obua@25520
   307
wenzelm@26626
   308
        val const_arity_tab = Encode.fold make_arity encoding Inttab.empty
wenzelm@26626
   309
        fun const_arity x = Inttab.lookup const_arity_tab x 
obua@25520
   310
obua@23663
   311
        val prog = 
wenzelm@26626
   312
            case machine of 
wenzelm@26626
   313
                BARRAS => ProgBarras (AM_Interpreter.compile cache_patterns const_arity rules)
wenzelm@26626
   314
              | BARRAS_COMPILED => ProgBarrasC (AM_Compiler.compile cache_patterns const_arity rules)
wenzelm@26626
   315
              | HASKELL => ProgHaskell (AM_GHC.compile cache_patterns const_arity rules)
wenzelm@26626
   316
              | SML => ProgSML (AM_SML.compile cache_patterns const_arity rules)
wenzelm@23174
   317
obua@23663
   318
        fun has_witness s = not (null (Sign.witness_sorts thy [] [s]))
wenzelm@23174
   319
wenzelm@26626
   320
        val shyptable = fold Sorttab.delete (filter has_witness (Sorttab.keys (shyptable))) shyptable
obua@23663
   321
obua@25217
   322
    in (Theory.check_thy thy, encoding, Termtab.keys hyptable, shyptable, prog, stamp, default_naming) end
obua@25217
   323
wenzelm@32740
   324
fun make_with_cache machine thy cache_patterns raw_thms =
wenzelm@32740
   325
  Computer (Unsynchronized.ref (SOME (make_internal machine thy (Unsynchronized.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@31322
   382
        val _ =
wenzelm@31322
   383
          if not (null shyps) then
wenzelm@31322
   384
            raise Compute ("dangling sort hypotheses: " ^
wenzelm@31322
   385
              commas (map (Syntax.string_of_sort_global thy) shyps))
wenzelm@31322
   386
          else ()
obua@25217
   387
    in
wenzelm@28290
   388
        Thm.cterm_of thy (fold_rev (fn hyp => fn p => Logic.mk_implies (hyp, p)) hyps prop)
wenzelm@28290
   389
    end)));
obua@25217
   390
obua@25217
   391
fun export_thm thy hyps shyps prop =
obua@25217
   392
    let
wenzelm@28290
   393
        val th = export_oracle (thy, hyps, shyps, prop)
wenzelm@26626
   394
        val hyps = map (fn h => assume (cterm_of thy h)) hyps
obua@25217
   395
    in
wenzelm@26626
   396
        fold (fn h => fn p => implies_elim p h) hyps th 
obua@25217
   397
    end
wenzelm@26626
   398
        
obua@25217
   399
(* --------- Rewrite ----------- *)
obua@25217
   400
obua@25217
   401
fun rewrite computer ct =
obua@25217
   402
    let
wenzelm@26626
   403
        val thy = Thm.theory_of_cterm ct
wenzelm@26626
   404
        val {t=t',T=ty,...} = rep_cterm ct
wenzelm@26626
   405
        val _ = Theory.assert_super (theory_of computer) thy
wenzelm@26626
   406
        val naming = naming_of computer
obua@25217
   407
        val (encoding, t) = remove_types (encoding_of computer) t'
wenzelm@26626
   408
        (*val _ = if (!print_encoding) then writeln (makestring ("encoding: ",Encode.fold (fn x => fn s => x::s) encoding [])) else ()*)
obua@25217
   409
        val t = runprog (prog_of computer) t
obua@25217
   410
        val t = infer_types naming encoding ty t
wenzelm@26626
   411
        val eq = Logic.mk_equals (t', t)
obua@25217
   412
    in
obua@25217
   413
        export_thm thy (hyps_of computer) (Sorttab.keys (shyptab_of computer)) eq
obua@25217
   414
    end
obua@25217
   415
obua@25217
   416
(* --------- Simplify ------------ *)
obua@23663
   417
obua@25217
   418
datatype prem = EqPrem of AbstractMachine.term * AbstractMachine.term * Term.typ * int 
wenzelm@26626
   419
              | Prem of AbstractMachine.term
wenzelm@32740
   420
datatype theorem = Theorem of theory_ref * unit Unsynchronized.ref * (int * typ) Symtab.table * (AbstractMachine.term option) Inttab.table  
obua@25217
   421
               * prem list * AbstractMachine.term * term list * sort list
obua@25217
   422
obua@25217
   423
obua@25217
   424
exception ParamSimplify of computer * theorem
obua@25217
   425
obua@25217
   426
fun make_theorem computer th vars =
obua@25217
   427
let
obua@25217
   428
    val _ = Theory.assert_super (theory_of computer) (theory_of_thm th)
obua@25217
   429
obua@25217
   430
    val (ComputeThm (hyps, shyps, prop)) = thm2cthm th 
obua@25217
   431
obua@25217
   432
    val encoding = encoding_of computer
obua@25217
   433
 
obua@25217
   434
    (* variables in the theorem are identified upfront *)
obua@25217
   435
    fun collect_vars (Abs (_, _, t)) tab = collect_vars t tab
obua@25217
   436
      | collect_vars (a $ b) tab = collect_vars b (collect_vars a tab)
obua@25217
   437
      | collect_vars (Const _) tab = tab
obua@25217
   438
      | collect_vars (Free _) tab = tab
obua@25217
   439
      | collect_vars (Var ((s, i), ty)) tab = 
wenzelm@26626
   440
            if List.find (fn x => x=s) vars = NONE then 
wenzelm@26626
   441
                tab
wenzelm@26626
   442
            else                
wenzelm@26626
   443
                (case Symtab.lookup tab s of
wenzelm@26626
   444
                     SOME ((s',i'),ty') => 
wenzelm@26626
   445
                     if s' <> s orelse i' <> i orelse ty <> ty' then 
wenzelm@26626
   446
                         raise Compute ("make_theorem: variable name '"^s^"' is not unique")
wenzelm@26626
   447
                     else 
wenzelm@26626
   448
                         tab
wenzelm@26626
   449
                   | NONE => Symtab.update (s, ((s, i), ty)) tab)
obua@25217
   450
    val vartab = collect_vars prop Symtab.empty 
obua@25217
   451
    fun encodevar (s, t as (_, ty)) (encoding, tab) = 
wenzelm@26626
   452
        let
wenzelm@26626
   453
            val (x, encoding) = Encode.insert (Var t) encoding
wenzelm@26626
   454
        in
wenzelm@26626
   455
            (encoding, Symtab.update (s, (x, ty)) tab)
wenzelm@26626
   456
        end
wenzelm@26626
   457
    val (encoding, vartab) = Symtab.fold encodevar vartab (encoding, Symtab.empty)                                                     
obua@25217
   458
    val varsubst = Inttab.make (map (fn (s, (x, _)) => (x, NONE)) (Symtab.dest vartab))
wenzelm@23174
   459
obua@25217
   460
    (* make the premises and the conclusion *)
obua@25217
   461
    fun mk_prem encoding t = 
wenzelm@26626
   462
        (let
wenzelm@26626
   463
             val (a, b) = Logic.dest_equals t
wenzelm@26626
   464
             val ty = type_of a
wenzelm@26626
   465
             val (encoding, a) = remove_types encoding a
wenzelm@26626
   466
             val (encoding, b) = remove_types encoding b
wenzelm@26626
   467
             val (eq, encoding) = Encode.insert (Const ("==", ty --> ty --> @{typ "prop"})) encoding 
wenzelm@26626
   468
         in
wenzelm@26626
   469
             (encoding, EqPrem (a, b, ty, eq))
wenzelm@26626
   470
         end handle TERM _ => let val (encoding, t) = remove_types encoding t in (encoding, Prem t) end)
obua@25217
   471
    val (encoding, prems) = 
wenzelm@26626
   472
        (fold_rev (fn t => fn (encoding, l) => 
wenzelm@26626
   473
            case mk_prem encoding t  of 
obua@25217
   474
                (encoding, t) => (encoding, t::l)) (Logic.strip_imp_prems prop) (encoding, []))
obua@25217
   475
    val (encoding, concl) = remove_types encoding (Logic.strip_imp_concl prop)
obua@25217
   476
    val _ = set_encoding computer encoding
obua@25217
   477
in
obua@25217
   478
    Theorem (Theory.check_thy (theory_of_thm th), stamp_of computer, vartab, varsubst, 
wenzelm@26626
   479
             prems, concl, hyps, shyps)
obua@25217
   480
end
obua@25217
   481
    
obua@25217
   482
fun theory_of_theorem (Theorem (rthy,_,_,_,_,_,_,_)) = Theory.deref rthy
obua@25217
   483
fun update_theory thy (Theorem (_,p0,p1,p2,p3,p4,p5,p6)) =
obua@25217
   484
    Theorem (Theory.check_thy thy,p0,p1,p2,p3,p4,p5,p6)
obua@25217
   485
fun stamp_of_theorem (Theorem (_,s, _, _, _, _, _, _)) = s     
obua@25217
   486
fun vartab_of_theorem (Theorem (_,_,vt,_,_,_,_,_)) = vt
obua@25217
   487
fun varsubst_of_theorem (Theorem (_,_,_,vs,_,_,_,_)) = vs 
obua@25217
   488
fun update_varsubst vs (Theorem (p0,p1,p2,_,p3,p4,p5,p6)) = Theorem (p0,p1,p2,vs,p3,p4,p5,p6)
obua@25217
   489
fun prems_of_theorem (Theorem (_,_,_,_,prems,_,_,_)) = prems
obua@25217
   490
fun update_prems prems (Theorem (p0,p1,p2,p3,_,p4,p5,p6)) = Theorem (p0,p1,p2,p3,prems,p4,p5,p6)
obua@25217
   491
fun concl_of_theorem (Theorem (_,_,_,_,_,concl,_,_)) = concl
obua@25217
   492
fun hyps_of_theorem (Theorem (_,_,_,_,_,_,hyps,_)) = hyps
obua@25217
   493
fun update_hyps hyps (Theorem (p0,p1,p2,p3,p4,p5,_,p6)) = Theorem (p0,p1,p2,p3,p4,p5,hyps,p6)
obua@25217
   494
fun shyps_of_theorem (Theorem (_,_,_,_,_,_,_,shyps)) = shyps
obua@25217
   495
fun update_shyps shyps (Theorem (p0,p1,p2,p3,p4,p5,p6,_)) = Theorem (p0,p1,p2,p3,p4,p5,p6,shyps)
obua@25217
   496
obua@25217
   497
fun check_compatible computer th s = 
obua@25217
   498
    if stamp_of computer <> stamp_of_theorem th then
wenzelm@26626
   499
        raise Compute (s^": computer and theorem are incompatible")
obua@25217
   500
    else ()
obua@25217
   501
obua@25217
   502
fun instantiate computer insts th =
obua@25217
   503
let
obua@25217
   504
    val _ = check_compatible computer th
obua@25217
   505
obua@25217
   506
    val thy = theory_of computer
obua@25217
   507
obua@25217
   508
    val vartab = vartab_of_theorem th
obua@25217
   509
obua@25217
   510
    fun rewrite computer t =
obua@25217
   511
    let  
wenzelm@26626
   512
        val naming = naming_of computer
obua@25217
   513
        val (encoding, t) = remove_types (encoding_of computer) t
obua@25217
   514
        val t = runprog (prog_of computer) t
wenzelm@26626
   515
        val _ = set_encoding computer encoding
wenzelm@23174
   516
    in
wenzelm@23174
   517
        t
wenzelm@23174
   518
    end
wenzelm@23174
   519
obua@25217
   520
    fun assert_varfree vs t = 
wenzelm@26626
   521
        if AbstractMachine.forall_consts (fn x => Inttab.lookup vs x = NONE) t then
wenzelm@26626
   522
            ()
wenzelm@26626
   523
        else
wenzelm@26626
   524
            raise Compute "instantiate: assert_varfree failed"
obua@25217
   525
obua@25217
   526
    fun assert_closed t =
wenzelm@26626
   527
        if AbstractMachine.closed t then
wenzelm@26626
   528
            ()
wenzelm@26626
   529
        else 
wenzelm@26626
   530
            raise Compute "instantiate: not a closed term"
obua@23663
   531
obua@25217
   532
    fun compute_inst (s, ct) vs =
wenzelm@26626
   533
        let
wenzelm@26626
   534
            val _ = Theory.assert_super (theory_of_cterm ct) thy
wenzelm@26626
   535
            val ty = typ_of (ctyp_of_term ct) 
wenzelm@26626
   536
        in          
wenzelm@26626
   537
            (case Symtab.lookup vartab s of 
wenzelm@26626
   538
                 NONE => raise Compute ("instantiate: variable '"^s^"' not found in theorem")
wenzelm@26626
   539
               | SOME (x, ty') => 
wenzelm@26626
   540
                 (case Inttab.lookup vs x of 
wenzelm@26626
   541
                      SOME (SOME _) => raise Compute ("instantiate: variable '"^s^"' has already been instantiated")
wenzelm@26626
   542
                    | SOME NONE => 
wenzelm@26626
   543
                      if ty <> ty' then 
wenzelm@26626
   544
                          raise Compute ("instantiate: wrong type for variable '"^s^"'")
wenzelm@26626
   545
                      else
wenzelm@26626
   546
                          let
wenzelm@26626
   547
                              val t = rewrite computer (term_of ct)
wenzelm@26626
   548
                              val _ = assert_varfree vs t 
wenzelm@26626
   549
                              val _ = assert_closed t
wenzelm@26626
   550
                          in
wenzelm@26626
   551
                              Inttab.update (x, SOME t) vs
wenzelm@26626
   552
                          end
wenzelm@26626
   553
                    | NONE => raise Compute "instantiate: internal error"))
wenzelm@26626
   554
        end
wenzelm@23174
   555
obua@25217
   556
    val vs = fold compute_inst insts (varsubst_of_theorem th)
obua@25217
   557
in
obua@25217
   558
    update_varsubst vs th
obua@25217
   559
end
wenzelm@23174
   560
obua@25217
   561
fun match_aterms subst =
wenzelm@26626
   562
    let 
wenzelm@26626
   563
        exception no_match
wenzelm@26626
   564
        open AbstractMachine
wenzelm@26626
   565
        fun match subst (b as (Const c)) a = 
wenzelm@26626
   566
            if a = b then subst
wenzelm@26626
   567
            else 
wenzelm@26626
   568
                (case Inttab.lookup subst c of 
wenzelm@26626
   569
                     SOME (SOME a') => if a=a' then subst else raise no_match
wenzelm@26626
   570
                   | SOME NONE => if AbstractMachine.closed a then 
wenzelm@26626
   571
                                      Inttab.update (c, SOME a) subst 
wenzelm@26626
   572
                                  else raise no_match
wenzelm@26626
   573
                   | NONE => raise no_match)
wenzelm@26626
   574
          | match subst (b as (Var _)) a = if a=b then subst else raise no_match
wenzelm@26626
   575
          | match subst (App (u, v)) (App (u', v')) = match (match subst u u') v v'
wenzelm@26626
   576
          | match subst (Abs u) (Abs u') = match subst u u'
wenzelm@26626
   577
          | match subst _ _ = raise no_match
obua@23663
   578
    in
wenzelm@26626
   579
        fn b => fn a => (SOME (match subst b a) handle no_match => NONE)
obua@25217
   580
    end
obua@25217
   581
obua@25217
   582
fun apply_subst vars_allowed subst =
obua@25217
   583
    let
wenzelm@26626
   584
        open AbstractMachine
wenzelm@26626
   585
        fun app (t as (Const c)) = 
wenzelm@26626
   586
            (case Inttab.lookup subst c of 
wenzelm@26626
   587
                 NONE => t 
wenzelm@26626
   588
               | SOME (SOME t) => Computed t
wenzelm@26626
   589
               | SOME NONE => if vars_allowed then t else raise Compute "apply_subst: no vars allowed")
wenzelm@26626
   590
          | app (t as (Var _)) = t
wenzelm@26626
   591
          | app (App (u, v)) = App (app u, app v)
wenzelm@26626
   592
          | app (Abs m) = Abs (app m)
obua@25217
   593
    in
wenzelm@26626
   594
        app
obua@23663
   595
    end
obua@23663
   596
obua@25217
   597
fun splicein n l L = List.take (L, n) @ l @ List.drop (L, n+1)
obua@23663
   598
obua@25217
   599
fun evaluate_prem computer prem_no th =
obua@25217
   600
let
obua@25217
   601
    val _ = check_compatible computer th
obua@25217
   602
    val prems = prems_of_theorem th
obua@25217
   603
    val varsubst = varsubst_of_theorem th
obua@25217
   604
    fun run vars_allowed t = 
wenzelm@26626
   605
        runprog (prog_of computer) (apply_subst vars_allowed varsubst t)
obua@25217
   606
in
obua@25217
   607
    case List.nth (prems, prem_no) of
wenzelm@26626
   608
        Prem _ => raise Compute "evaluate_prem: no equality premise"
wenzelm@26626
   609
      | EqPrem (a, b, ty, _) =>         
wenzelm@26626
   610
        let
wenzelm@26626
   611
            val a' = run false a
wenzelm@26626
   612
            val b' = run true b
wenzelm@26626
   613
        in
wenzelm@26626
   614
            case match_aterms varsubst b' a' of
wenzelm@26626
   615
                NONE => 
wenzelm@26626
   616
                let
wenzelm@31322
   617
                    fun mk s = Syntax.string_of_term_global Pure.thy
wenzelm@31322
   618
                      (infer_types (naming_of computer) (encoding_of computer) ty s)
wenzelm@26626
   619
                    val left = "computed left side: "^(mk a')
wenzelm@26626
   620
                    val right = "computed right side: "^(mk b')
wenzelm@26626
   621
                in
wenzelm@26626
   622
                    raise Compute ("evaluate_prem: cannot assign computed left to right hand side\n"^left^"\n"^right^"\n")
wenzelm@26626
   623
                end
wenzelm@26626
   624
              | SOME varsubst => 
wenzelm@26626
   625
                update_prems (splicein prem_no [] prems) (update_varsubst varsubst th)
wenzelm@26626
   626
        end
obua@25217
   627
end
obua@23663
   628
obua@25217
   629
fun prem2term (Prem t) = t
obua@25217
   630
  | prem2term (EqPrem (a,b,_,eq)) = 
obua@25217
   631
    AbstractMachine.App (AbstractMachine.App (AbstractMachine.Const eq, a), b)
obua@23663
   632
obua@25217
   633
fun modus_ponens computer prem_no th' th = 
obua@25217
   634
let
obua@25217
   635
    val _ = check_compatible computer th
obua@25217
   636
    val thy = 
wenzelm@26626
   637
        let
wenzelm@26626
   638
            val thy1 = theory_of_theorem th
wenzelm@26626
   639
            val thy2 = theory_of_thm th'
wenzelm@26626
   640
        in
wenzelm@26674
   641
            if Theory.subthy (thy1, thy2) then thy2 
wenzelm@26674
   642
            else if Theory.subthy (thy2, thy1) then thy1 else
wenzelm@26626
   643
            raise Compute "modus_ponens: theorems are not compatible with each other"
wenzelm@26626
   644
        end 
obua@25217
   645
    val th' = make_theorem computer th' []
obua@25217
   646
    val varsubst = varsubst_of_theorem th
obua@25217
   647
    fun run vars_allowed t =
wenzelm@26626
   648
        runprog (prog_of computer) (apply_subst vars_allowed varsubst t)
obua@25217
   649
    val prems = prems_of_theorem th
obua@25217
   650
    val prem = run true (prem2term (List.nth (prems, prem_no)))
obua@25217
   651
    val concl = run false (concl_of_theorem th')    
obua@25217
   652
in
obua@25217
   653
    case match_aterms varsubst prem concl of
wenzelm@26626
   654
        NONE => raise Compute "modus_ponens: conclusion does not match premise"
obua@25217
   655
      | SOME varsubst =>
wenzelm@26626
   656
        let
wenzelm@26626
   657
            val th = update_varsubst varsubst th
wenzelm@26626
   658
            val th = update_prems (splicein prem_no (prems_of_theorem th') prems) th
wenzelm@26626
   659
            val th = update_hyps (merge_hyps (hyps_of_theorem th) (hyps_of_theorem th')) th
wenzelm@26626
   660
            val th = update_shyps (merge_shyps (shyps_of_theorem th) (shyps_of_theorem th')) th
wenzelm@26626
   661
        in
wenzelm@26626
   662
            update_theory thy th
wenzelm@26626
   663
        end
obua@25217
   664
end
obua@25217
   665
                     
obua@25217
   666
fun simplify computer th =
obua@25217
   667
let
obua@25217
   668
    val _ = check_compatible computer th
obua@25217
   669
    val varsubst = varsubst_of_theorem th
obua@25217
   670
    val encoding = encoding_of computer
obua@25217
   671
    val naming = naming_of computer
obua@25217
   672
    fun infer t = infer_types naming encoding @{typ "prop"} t
obua@25217
   673
    fun run t = infer (runprog (prog_of computer) (apply_subst true varsubst t))
obua@25217
   674
    fun runprem p = run (prem2term p)
obua@25217
   675
    val prop = Logic.list_implies (map runprem (prems_of_theorem th), run (concl_of_theorem th))
obua@25217
   676
    val hyps = merge_hyps (hyps_of computer) (hyps_of_theorem th)
obua@25217
   677
    val shyps = merge_shyps (shyps_of_theorem th) (Sorttab.keys (shyptab_of computer))
obua@25217
   678
in
obua@25217
   679
    export_thm (theory_of_theorem th) hyps shyps prop
obua@25217
   680
end
wenzelm@23174
   681
wenzelm@23174
   682
end
obua@23663
   683