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