src/Tools/Compute_Oracle/linker.ML
author wenzelm
Thu Mar 20 00:20:44 2008 +0100 (2008-03-20)
changeset 26343 0dd2eab7b296
parent 26336 a0e2b706ce73
child 29269 5c25a2012975
permissions -rw-r--r--
simplified get_thm(s): back to plain name argument;
haftmann@24584
     1
(*  Title:      Tools/Compute_Oracle/linker.ML
wenzelm@24271
     2
    ID:         $Id$
obua@23663
     3
    Author:     Steven Obua
obua@23663
     4
wenzelm@24271
     5
Linker.ML solves the problem that the computing oracle does not
wenzelm@24271
     6
instantiate polymorphic rules. By going through the PCompute interface,
wenzelm@24271
     7
all possible instantiations are resolved by compiling new programs, if
wenzelm@24271
     8
necessary. The obvious disadvantage of this approach is that in the
wenzelm@24271
     9
worst case for each new term to be rewritten, a new program may be
wenzelm@24271
    10
compiled.
obua@23663
    11
*)
obua@23663
    12
wenzelm@24271
    13
(*
obua@23663
    14
   Given constants/frees c_1::t_1, c_2::t_2, ...., c_n::t_n,
obua@23663
    15
   and constants/frees d_1::d_1, d_2::s_2, ..., d_m::s_m
obua@23663
    16
obua@23663
    17
   Find all substitutions S such that
wenzelm@24271
    18
   a) the domain of S is tvars (t_1, ..., t_n)
obua@23663
    19
   b) there are indices i_1, ..., i_k, and j_1, ..., j_k with
obua@23663
    20
      1. S (c_i_1::t_i_1) = d_j_1::s_j_1, ..., S (c_i_k::t_i_k) = d_j_k::s_j_k
obua@23663
    21
      2. tvars (t_i_1, ..., t_i_k) = tvars (t_1, ..., t_n)
obua@23663
    22
*)
wenzelm@24271
    23
signature LINKER =
obua@23663
    24
sig
obua@23663
    25
    exception Link of string
wenzelm@24271
    26
obua@23663
    27
    datatype constant = Constant of bool * string * typ
obua@23663
    28
    val constant_of : term -> constant
obua@23663
    29
obua@23663
    30
    type instances
obua@23663
    31
    type subst = Type.tyenv
wenzelm@24271
    32
obua@23663
    33
    val empty : constant list -> instances
obua@23663
    34
    val typ_of_constant : constant -> typ
wenzelm@24271
    35
    val add_instances : theory -> instances -> constant list -> subst list * instances
obua@23663
    36
    val substs_of : instances -> subst list
obua@23663
    37
    val is_polymorphic : constant -> bool
obua@23663
    38
    val distinct_constants : constant list -> constant list
obua@23663
    39
    val collect_consts : term list -> constant list
obua@23663
    40
end
obua@23663
    41
obua@23663
    42
structure Linker : LINKER = struct
obua@23663
    43
obua@23663
    44
exception Link of string;
obua@23663
    45
obua@23663
    46
type subst = Type.tyenv
obua@23663
    47
obua@23663
    48
datatype constant = Constant of bool * string * typ
obua@23663
    49
fun constant_of (Const (name, ty)) = Constant (false, name, ty)
obua@23663
    50
  | constant_of (Free (name, ty)) = Constant (true, name, ty)
obua@23663
    51
  | constant_of _ = raise Link "constant_of"
obua@23663
    52
obua@23663
    53
fun bool_ord (x,y) = if x then (if y then EQUAL else GREATER) else (if y then LESS else EQUAL)
obua@23663
    54
fun constant_ord (Constant (x1,x2,x3), Constant (y1,y2,y3)) = (prod_ord (prod_ord bool_ord fast_string_ord) Term.typ_ord) (((x1,x2),x3), ((y1,y2),y3))
obua@23663
    55
fun constant_modty_ord (Constant (x1,x2,_), Constant (y1,y2,_)) = (prod_ord bool_ord fast_string_ord) ((x1,x2), (y1,y2))
obua@23663
    56
obua@23663
    57
obua@23663
    58
structure Consttab = TableFun(type key = constant val ord = constant_ord);
obua@23663
    59
structure ConsttabModTy = TableFun(type key = constant val ord = constant_modty_ord);
obua@23663
    60
obua@23663
    61
fun typ_of_constant (Constant (_, _, ty)) = ty
obua@23663
    62
obua@23663
    63
val empty_subst = (Vartab.empty : Type.tyenv)
obua@23663
    64
wenzelm@24271
    65
fun merge_subst (A:Type.tyenv) (B:Type.tyenv) =
wenzelm@24271
    66
    SOME (Vartab.fold (fn (v, t) =>
wenzelm@24271
    67
                       fn tab =>
wenzelm@24271
    68
                          (case Vartab.lookup tab v of
wenzelm@24271
    69
                               NONE => Vartab.update (v, t) tab
wenzelm@24271
    70
                             | SOME t' => if t = t' then tab else raise Type.TYPE_MATCH)) A B)
obua@23663
    71
    handle Type.TYPE_MATCH => NONE
obua@23663
    72
wenzelm@24271
    73
fun subst_ord (A:Type.tyenv, B:Type.tyenv) =
obua@23663
    74
    (list_ord (prod_ord Term.fast_indexname_ord (prod_ord Term.sort_ord Term.typ_ord))) (Vartab.dest A, Vartab.dest B)
obua@23663
    75
obua@23663
    76
structure Substtab = TableFun(type key = Type.tyenv val ord = subst_ord);
obua@23663
    77
obua@23768
    78
fun substtab_union c = Substtab.fold Substtab.update c
obua@23663
    79
fun substtab_unions [] = Substtab.empty
obua@23663
    80
  | substtab_unions [c] = c
obua@23663
    81
  | substtab_unions (c::cs) = substtab_union c (substtab_unions cs)
obua@23663
    82
obua@23663
    83
datatype instances = Instances of unit ConsttabModTy.table * Type.tyenv Consttab.table Consttab.table * constant list list * unit Substtab.table
obua@23663
    84
wenzelm@24271
    85
fun is_polymorphic (Constant (_, _, ty)) = not (null (typ_tvars ty))
obua@23663
    86
obua@23663
    87
fun distinct_constants cs =
obua@23663
    88
    Consttab.keys (fold (fn c => Consttab.update (c, ())) cs Consttab.empty)
obua@23663
    89
wenzelm@24271
    90
fun empty cs =
wenzelm@24271
    91
    let
wenzelm@24271
    92
        val cs = distinct_constants (filter is_polymorphic cs)
wenzelm@24271
    93
        val old_cs = cs
wenzelm@24271
    94
(*      fun collect_tvars ty tab = fold (fn v => fn tab => Typtab.update (TVar v, ()) tab) (typ_tvars ty) tab
wenzelm@24271
    95
        val tvars_count = length (Typtab.keys (fold (fn c => fn tab => collect_tvars (typ_of_constant c) tab) cs Typtab.empty))
wenzelm@24271
    96
        fun tvars_of ty = collect_tvars ty Typtab.empty
wenzelm@24271
    97
        val cs = map (fn c => (c, tvars_of (typ_of_constant c))) cs
obua@23663
    98
wenzelm@24271
    99
        fun tyunion A B =
wenzelm@24271
   100
            Typtab.fold
wenzelm@24271
   101
                (fn (v,()) => fn tab => Typtab.update (v, case Typtab.lookup tab v of NONE => 1 | SOME n => n+1) tab)
wenzelm@24271
   102
                A B
obua@23663
   103
obua@23663
   104
        fun is_essential A B =
wenzelm@24271
   105
            Typtab.fold
wenzelm@24271
   106
            (fn (v, ()) => fn essential => essential orelse (case Typtab.lookup B v of NONE => raise Link "is_essential" | SOME n => n=1))
wenzelm@24271
   107
            A false
obua@23663
   108
wenzelm@24271
   109
        fun add_minimal (c', tvs') (tvs, cs) =
wenzelm@24271
   110
            let
wenzelm@24271
   111
                val tvs = tyunion tvs' tvs
wenzelm@24271
   112
                val cs = (c', tvs')::cs
wenzelm@24271
   113
            in
wenzelm@24271
   114
                if forall (fn (c',tvs') => is_essential tvs' tvs) cs then
wenzelm@24271
   115
                    SOME (tvs, cs)
wenzelm@24271
   116
                else
wenzelm@24271
   117
                    NONE
wenzelm@24271
   118
            end
obua@23663
   119
wenzelm@24271
   120
        fun is_spanning (tvs, _) = (length (Typtab.keys tvs) = tvars_count)
obua@23663
   121
wenzelm@24271
   122
        fun generate_minimal_subsets subsets [] = subsets
wenzelm@24271
   123
          | generate_minimal_subsets subsets (c::cs) =
wenzelm@24271
   124
            let
wenzelm@24271
   125
                val subsets' = map_filter (add_minimal c) subsets
wenzelm@24271
   126
            in
wenzelm@24271
   127
                generate_minimal_subsets (subsets@subsets') cs
wenzelm@24271
   128
            end*)
obua@23663
   129
wenzelm@24271
   130
        val minimal_subsets = [old_cs] (*map (fn (tvs, cs) => map fst cs) (filter is_spanning (generate_minimal_subsets [(Typtab.empty, [])] cs))*)
obua@23663
   131
wenzelm@24271
   132
        val constants = Consttab.keys (fold (fold (fn c => Consttab.update (c, ()))) minimal_subsets Consttab.empty)
obua@23663
   133
obua@23663
   134
    in
wenzelm@24271
   135
        Instances (
wenzelm@24271
   136
        fold (fn c => fn tab => ConsttabModTy.update (c, ()) tab) constants ConsttabModTy.empty,
wenzelm@24271
   137
        Consttab.make (map (fn c => (c, Consttab.empty : Type.tyenv Consttab.table)) constants),
wenzelm@24271
   138
        minimal_subsets, Substtab.empty)
obua@23663
   139
    end
obua@23663
   140
obua@23663
   141
local
obua@23663
   142
fun calc ctab substtab [] = substtab
wenzelm@24271
   143
  | calc ctab substtab (c::cs) =
obua@23663
   144
    let
wenzelm@24271
   145
        val csubsts = map snd (Consttab.dest (the (Consttab.lookup ctab c)))
wenzelm@24271
   146
        fun merge_substs substtab subst =
wenzelm@24271
   147
            Substtab.fold (fn (s,_) =>
wenzelm@24271
   148
                           fn tab =>
wenzelm@24271
   149
                              (case merge_subst subst s of NONE => tab | SOME s => Substtab.update (s, ()) tab))
wenzelm@24271
   150
                          substtab Substtab.empty
wenzelm@24271
   151
        val substtab = substtab_unions (map (merge_substs substtab) csubsts)
obua@23663
   152
    in
wenzelm@24271
   153
        calc ctab substtab cs
obua@23663
   154
    end
obua@23663
   155
in
obua@23663
   156
fun calc_substs ctab (cs:constant list) = calc ctab (Substtab.update (empty_subst, ()) Substtab.empty) cs
obua@23663
   157
end
wenzelm@24271
   158
wenzelm@24271
   159
fun add_instances thy (Instances (cfilter, ctab,minsets,substs)) cs =
wenzelm@24271
   160
    let
wenzelm@24271
   161
(*      val _ = writeln (makestring ("add_instances: ", length_cs, length cs, length (Consttab.keys ctab)))*)
wenzelm@24271
   162
        fun calc_instantiations (constant as Constant (free, name, ty)) instantiations =
wenzelm@24271
   163
            Consttab.fold (fn (constant' as Constant (free', name', ty'), insttab) =>
wenzelm@24271
   164
                           fn instantiations =>
wenzelm@24271
   165
                              if free <> free' orelse name <> name' then
wenzelm@24271
   166
                                  instantiations
wenzelm@24271
   167
                              else case Consttab.lookup insttab constant of
wenzelm@24271
   168
                                       SOME _ => instantiations
wenzelm@24271
   169
                                     | NONE => ((constant', (constant, Sign.typ_match thy (ty', ty) empty_subst))::instantiations
wenzelm@24271
   170
                                                handle TYPE_MATCH => instantiations))
wenzelm@24271
   171
                          ctab instantiations
wenzelm@24271
   172
        val instantiations = fold calc_instantiations cs []
wenzelm@24271
   173
        (*val _ = writeln ("instantiations = "^(makestring (length instantiations)))*)
wenzelm@24271
   174
        fun update_ctab (constant', entry) ctab =
wenzelm@24271
   175
            (case Consttab.lookup ctab constant' of
wenzelm@24271
   176
                 NONE => raise Link "internal error: update_ctab"
wenzelm@24271
   177
               | SOME tab => Consttab.update (constant', Consttab.update entry tab) ctab)
wenzelm@24271
   178
        val ctab = fold update_ctab instantiations ctab
wenzelm@24271
   179
        val new_substs = fold (fn minset => fn substs => substtab_union (calc_substs ctab minset) substs)
wenzelm@24271
   180
                              minsets Substtab.empty
wenzelm@24271
   181
        val (added_substs, substs) =
wenzelm@24271
   182
            Substtab.fold (fn (ns, _) =>
wenzelm@24271
   183
                           fn (added, substtab) =>
wenzelm@24271
   184
                              (case Substtab.lookup substs ns of
wenzelm@24271
   185
                                   NONE => (ns::added, Substtab.update (ns, ()) substtab)
wenzelm@24271
   186
                                 | SOME () => (added, substtab)))
wenzelm@24271
   187
                          new_substs ([], substs)
obua@23663
   188
    in
wenzelm@24271
   189
        (added_substs, Instances (cfilter, ctab, minsets, substs))
obua@23663
   190
    end
wenzelm@24271
   191
obua@23663
   192
fun substs_of (Instances (_,_,_,substs)) = Substtab.keys substs
obua@23663
   193
obua@23663
   194
local
wenzelm@26343
   195
    fun get_thm thmname = PureThy.get_thm (theory "Main") thmname
obua@23663
   196
    val eq_th = get_thm "HOL.eq_reflection"
obua@23663
   197
in
wenzelm@26336
   198
  fun eq_to_meta th = (eq_th OF [th] handle THM _ => th)
obua@23663
   199
end
obua@23663
   200
wenzelm@24271
   201
obua@23663
   202
local
obua@23663
   203
obua@23663
   204
fun collect (Var x) tab = tab
obua@23663
   205
  | collect (Bound _) tab = tab
obua@23663
   206
  | collect (a $ b) tab = collect b (collect a tab)
obua@23663
   207
  | collect (Abs (_, _, body)) tab = collect body tab
obua@23663
   208
  | collect t tab = Consttab.update (constant_of t, ()) tab
obua@23663
   209
obua@23663
   210
in
obua@23663
   211
  fun collect_consts tms = Consttab.keys (fold collect tms Consttab.empty)
obua@23663
   212
end
obua@23663
   213
obua@23663
   214
end
obua@23663
   215
obua@23663
   216
signature PCOMPUTE =
obua@23663
   217
sig
obua@23663
   218
    type pcomputer
wenzelm@24271
   219
obua@23663
   220
    val make : Compute.machine -> theory -> thm list -> Linker.constant list -> pcomputer
obua@25520
   221
    val make_with_cache : Compute.machine -> theory -> term list -> thm list -> Linker.constant list -> pcomputer
obua@25218
   222
    
obua@25218
   223
    val add_instances : pcomputer -> Linker.constant list -> bool 
obua@25218
   224
    val add_instances' : pcomputer -> term list -> bool
obua@23663
   225
obua@23663
   226
    val rewrite : pcomputer -> cterm list -> thm list
obua@25218
   227
    val simplify : pcomputer -> Compute.theorem -> thm
obua@25218
   228
obua@25218
   229
    val make_theorem : pcomputer -> thm -> string list -> Compute.theorem
obua@25218
   230
    val instantiate : pcomputer -> (string * cterm) list -> Compute.theorem -> Compute.theorem
obua@25218
   231
    val evaluate_prem : pcomputer -> int -> Compute.theorem -> Compute.theorem
obua@25218
   232
    val modus_ponens : pcomputer -> int -> thm -> Compute.theorem -> Compute.theorem 
obua@23663
   233
obua@23663
   234
end
obua@23663
   235
obua@23663
   236
structure PCompute : PCOMPUTE = struct
obua@23663
   237
obua@23663
   238
exception PCompute of string
obua@23663
   239
obua@23663
   240
datatype theorem = MonoThm of thm | PolyThm of thm * Linker.instances * thm list
obua@25520
   241
datatype pattern = MonoPattern of term | PolyPattern of term * Linker.instances * term list
obua@23663
   242
obua@25520
   243
datatype pcomputer = PComputer of theory_ref * Compute.computer * theorem list ref * pattern list ref 
obua@23663
   244
obua@23663
   245
(*fun collect_consts (Var x) = []
obua@23663
   246
  | collect_consts (Bound _) = []
obua@23663
   247
  | collect_consts (a $ b) = (collect_consts a)@(collect_consts b)
obua@23663
   248
  | collect_consts (Abs (_, _, body)) = collect_consts body
obua@23663
   249
  | collect_consts t = [Linker.constant_of t]*)
obua@23663
   250
obua@25520
   251
fun computer_of (PComputer (_,computer,_,_)) = computer
obua@25218
   252
obua@25218
   253
fun collect_consts_of_thm th = 
obua@23663
   254
    let
wenzelm@24271
   255
        val th = prop_of th
wenzelm@24271
   256
        val (prems, th) = (Logic.strip_imp_prems th, Logic.strip_imp_concl th)
wenzelm@24271
   257
        val (left, right) = Logic.dest_equals th
obua@23663
   258
    in
wenzelm@24271
   259
        (Linker.collect_consts [left], Linker.collect_consts (right::prems))
wenzelm@24271
   260
    end
obua@23663
   261
obua@23663
   262
fun create_theorem th =
wenzelm@24271
   263
let
obua@23663
   264
    val (left, right) = collect_consts_of_thm th
obua@23663
   265
    val polycs = filter Linker.is_polymorphic left
obua@23663
   266
    val tytab = fold (fn p => fn tab => fold (fn n => fn tab => Typtab.update (TVar n, ()) tab) (typ_tvars (Linker.typ_of_constant p)) tab) polycs Typtab.empty
wenzelm@24271
   267
    fun check_const (c::cs) cs' =
wenzelm@24271
   268
        let
wenzelm@24271
   269
            val tvars = typ_tvars (Linker.typ_of_constant c)
wenzelm@24271
   270
            val wrong = fold (fn n => fn wrong => wrong orelse is_none (Typtab.lookup tytab (TVar n))) tvars false
wenzelm@24271
   271
        in
wenzelm@24271
   272
            if wrong then raise PCompute "right hand side of theorem contains type variables which do not occur on the left hand side"
wenzelm@24271
   273
            else
wenzelm@24271
   274
                if null (tvars) then
wenzelm@24271
   275
                    check_const cs (c::cs')
wenzelm@24271
   276
                else
wenzelm@24271
   277
                    check_const cs cs'
wenzelm@24271
   278
        end
obua@23663
   279
      | check_const [] cs' = cs'
wenzelm@24271
   280
    val monocs = check_const right []
obua@23663
   281
in
wenzelm@24271
   282
    if null (polycs) then
wenzelm@24271
   283
        (monocs, MonoThm th)
obua@23663
   284
    else
wenzelm@24271
   285
        (monocs, PolyThm (th, Linker.empty polycs, []))
obua@23663
   286
end
obua@23663
   287
obua@25520
   288
fun create_pattern pat = 
obua@25520
   289
let
obua@25520
   290
    val cs = Linker.collect_consts [pat]
obua@25520
   291
    val polycs = filter Linker.is_polymorphic cs
obua@25520
   292
in
obua@25520
   293
    if null (polycs) then
obua@25520
   294
	MonoPattern pat
obua@25520
   295
    else
obua@25520
   296
	PolyPattern (pat, Linker.empty polycs, [])
obua@25520
   297
end
obua@25520
   298
	     
obua@25520
   299
fun create_computer machine thy pats ths =
obua@23663
   300
    let
wenzelm@24271
   301
        fun add (MonoThm th) ths = th::ths
wenzelm@24271
   302
          | add (PolyThm (_, _, ths')) ths = ths'@ths
obua@25520
   303
	fun addpat (MonoPattern p) pats = p::pats
obua@25520
   304
	  | addpat (PolyPattern (_, _, ps)) pats = ps@pats
wenzelm@24271
   305
        val ths = fold_rev add ths []
obua@25520
   306
	val pats = fold_rev addpat pats []
obua@23663
   307
    in
obua@25520
   308
        Compute.make_with_cache machine thy pats ths
obua@23663
   309
    end
obua@23663
   310
obua@25520
   311
fun update_computer computer pats ths = 
obua@25218
   312
    let
obua@25218
   313
	fun add (MonoThm th) ths = th::ths
obua@25218
   314
	  | add (PolyThm (_, _, ths')) ths = ths'@ths
obua@25520
   315
	fun addpat (MonoPattern p) pats = p::pats
obua@25520
   316
	  | addpat (PolyPattern (_, _, ps)) pats = ps@pats
obua@25218
   317
	val ths = fold_rev add ths []
obua@25520
   318
	val pats = fold_rev addpat pats []
obua@25218
   319
    in
obua@25520
   320
	Compute.update_with_cache computer pats ths
obua@25218
   321
    end
obua@25218
   322
obua@23663
   323
fun conv_subst thy (subst : Type.tyenv) =
obua@23663
   324
    map (fn (iname, (sort, ty)) => (ctyp_of thy (TVar (iname, sort)), ctyp_of thy ty)) (Vartab.dest subst)
obua@23663
   325
obua@25520
   326
fun add_monos thy monocs pats ths =
obua@23663
   327
    let
wenzelm@24271
   328
        val changed = ref false
wenzelm@24271
   329
        fun add monocs (th as (MonoThm _)) = ([], th)
wenzelm@24271
   330
          | add monocs (PolyThm (th, instances, instanceths)) =
wenzelm@24271
   331
            let
wenzelm@24271
   332
                val (newsubsts, instances) = Linker.add_instances thy instances monocs
wenzelm@24271
   333
                val _ = if not (null newsubsts) then changed := true else ()
wenzelm@24271
   334
                val newths = map (fn subst => Thm.instantiate (conv_subst thy subst, []) th) newsubsts
wenzelm@24271
   335
(*              val _ = if not (null newths) then (print ("added new theorems: ", newths); ()) else ()*)
wenzelm@24271
   336
                val newmonos = fold (fn th => fn monos => (snd (collect_consts_of_thm th))@monos) newths []
wenzelm@24271
   337
            in
wenzelm@24271
   338
                (newmonos, PolyThm (th, instances, instanceths@newths))
wenzelm@24271
   339
            end
obua@25520
   340
	fun addpats monocs (pat as (MonoPattern _)) = pat
obua@25520
   341
	  | addpats monocs (PolyPattern (p, instances, instancepats)) =
obua@25520
   342
	    let
obua@25520
   343
		val (newsubsts, instances) = Linker.add_instances thy instances monocs
obua@25520
   344
		val _ = if not (null newsubsts) then changed := true else ()
obua@25520
   345
		val newpats = map (fn subst => Envir.subst_TVars subst p) newsubsts
obua@25520
   346
	    in
obua@25520
   347
		PolyPattern (p, instances, instancepats@newpats)
obua@25520
   348
	    end 
wenzelm@24271
   349
        fun step monocs ths =
wenzelm@24271
   350
            fold_rev (fn th =>
wenzelm@24271
   351
                      fn (newmonos, ths) =>
obua@25520
   352
                         let 
obua@25520
   353
			     val (newmonos', th') = add monocs th 
obua@25520
   354
			 in
wenzelm@24271
   355
                             (newmonos'@newmonos, th'::ths)
wenzelm@24271
   356
                         end)
wenzelm@24271
   357
                     ths ([], [])
obua@25520
   358
        fun loop monocs pats ths =
obua@25520
   359
            let 
obua@25520
   360
		val (monocs', ths') = step monocs ths 
obua@25520
   361
		val pats' = map (addpats monocs) pats
obua@25520
   362
	    in
wenzelm@24271
   363
                if null (monocs') then
obua@25520
   364
                    (pats', ths')
wenzelm@24271
   365
                else
obua@25520
   366
                    loop monocs' pats' ths'
wenzelm@24271
   367
            end
obua@25520
   368
        val result = loop monocs pats ths
obua@23663
   369
    in
wenzelm@24271
   370
        (!changed, result)
wenzelm@24271
   371
    end
obua@23663
   372
obua@23663
   373
datatype cthm = ComputeThm of term list * sort list * term
obua@23663
   374
wenzelm@24271
   375
fun thm2cthm th =
obua@23663
   376
    let
wenzelm@24271
   377
        val {hyps, prop, shyps, ...} = Thm.rep_thm th
obua@23663
   378
    in
wenzelm@24271
   379
        ComputeThm (hyps, shyps, prop)
obua@23663
   380
    end
obua@23663
   381
obua@23663
   382
val cthm_ord' = prod_ord (prod_ord (list_ord Term.term_ord) (list_ord Term.sort_ord)) Term.term_ord
obua@23663
   383
obua@23663
   384
fun cthm_ord (ComputeThm (h1, sh1, p1), ComputeThm (h2, sh2, p2)) = cthm_ord' (((h1,sh1), p1), ((h2, sh2), p2))
obua@23663
   385
obua@23663
   386
structure CThmtab = TableFun (type key = cthm val ord = cthm_ord)
wenzelm@24271
   387
obua@23663
   388
fun remove_duplicates ths =
obua@23663
   389
    let
wenzelm@24271
   390
        val counter = ref 0
wenzelm@24271
   391
        val tab = ref (CThmtab.empty : unit CThmtab.table)
wenzelm@24271
   392
        val thstab = ref (Inttab.empty : thm Inttab.table)
wenzelm@24271
   393
        fun update th =
wenzelm@24271
   394
            let
wenzelm@24271
   395
                val key = thm2cthm th
wenzelm@24271
   396
            in
wenzelm@24271
   397
                case CThmtab.lookup (!tab) key of
wenzelm@24271
   398
                    NONE => ((tab := CThmtab.update_new (key, ()) (!tab)); thstab := Inttab.update_new (!counter, th) (!thstab); counter := !counter + 1)
wenzelm@24271
   399
                  | _ => ()
wenzelm@24271
   400
            end
wenzelm@24271
   401
        val _ = map update ths
obua@23663
   402
    in
wenzelm@24271
   403
        map snd (Inttab.dest (!thstab))
obua@23663
   404
    end
wenzelm@24271
   405
obua@25520
   406
fun make_with_cache machine thy pats ths cs =
obua@23663
   407
    let
obua@25218
   408
	val ths = remove_duplicates ths
obua@25218
   409
	val (monocs, ths) = fold_rev (fn th => 
obua@25218
   410
				      fn (monocs, ths) => 
obua@25218
   411
					 let val (m, t) = create_theorem th in 
obua@25218
   412
					     (m@monocs, t::ths)
obua@25218
   413
					 end)
obua@25218
   414
				     ths (cs, [])
obua@25520
   415
	val pats = map create_pattern pats
obua@25520
   416
	val (_, (pats, ths)) = add_monos thy monocs pats ths
obua@25520
   417
	val computer = create_computer machine thy pats ths
obua@23663
   418
    in
obua@25520
   419
	PComputer (Theory.check_thy thy, computer, ref ths, ref pats)
obua@23663
   420
    end
obua@23663
   421
obua@25520
   422
fun make machine thy ths cs = make_with_cache machine thy [] ths cs
obua@25520
   423
obua@25520
   424
fun add_instances (PComputer (thyref, computer, rths, rpats)) cs = 
obua@23663
   425
    let
wenzelm@24271
   426
        val thy = Theory.deref thyref
obua@25520
   427
        val (changed, (pats, ths)) = add_monos thy cs (!rpats) (!rths)
obua@23663
   428
    in
obua@25218
   429
	if changed then
obua@25520
   430
	    (update_computer computer pats ths;
obua@25218
   431
	     rths := ths;
obua@25520
   432
	     rpats := pats;
obua@25218
   433
	     true)
obua@25218
   434
	else
obua@25218
   435
	    false
obua@25218
   436
obua@25218
   437
    end
obua@25218
   438
obua@25218
   439
fun add_instances' pc ts = add_instances pc (Linker.collect_consts ts)
obua@25218
   440
obua@25218
   441
fun rewrite pc cts =
obua@25218
   442
    let
obua@25218
   443
	val _ = add_instances' pc (map term_of cts)
obua@25218
   444
	val computer = (computer_of pc)
obua@25218
   445
    in
obua@25218
   446
	map (fn ct => Compute.rewrite computer ct) cts
obua@23663
   447
    end
obua@23663
   448
obua@25218
   449
fun simplify pc th = Compute.simplify (computer_of pc) th
obua@25218
   450
obua@25218
   451
fun make_theorem pc th vars = 
obua@23663
   452
    let
obua@25218
   453
	val _ = add_instances' pc [prop_of th]
obua@25218
   454
obua@23663
   455
    in
obua@25218
   456
	Compute.make_theorem (computer_of pc) th vars
obua@23663
   457
    end
wenzelm@24271
   458
obua@25218
   459
fun instantiate pc insts th = 
obua@25218
   460
    let
obua@25218
   461
	val _ = add_instances' pc (map (term_of o snd) insts)
obua@25218
   462
    in
obua@25218
   463
	Compute.instantiate (computer_of pc) insts th
obua@25218
   464
    end
obua@25218
   465
obua@25218
   466
fun evaluate_prem pc prem_no th = Compute.evaluate_prem (computer_of pc) prem_no th
obua@25218
   467
obua@25218
   468
fun modus_ponens pc prem_no th' th =
obua@25218
   469
    let
obua@25218
   470
	val _ = add_instances' pc [prop_of th']
obua@25218
   471
    in
obua@25218
   472
	Compute.modus_ponens (computer_of pc) prem_no th' th
obua@25218
   473
    end    
obua@25218
   474
		 							      			    
obua@25218
   475
wenzelm@24137
   476
end