src/Pure/compress.ML
author wenzelm
Thu, 27 Mar 2008 14:41:14 +0100
changeset 26428 5b2beca2087d
parent 26427 f33d1b522316
child 26463 9283b4185fdf
permissions -rw-r--r--
eliminated theory ProtoPure; implicit setup of emerging theory Pure; added >>> operator;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16981
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/compress.ML
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
     3
    Author:     Lawrence C Paulson and Makarius
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
     4
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
     5
Compression of terms and types by sharing common subtrees.  Saves
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
     6
50-75% on storage requirements.  As it is a bit slow, it should be
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
     7
called only for axioms, stored theorems, etc.
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
     8
*)
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
     9
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    10
signature COMPRESS =
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    11
sig
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    12
  val typ: theory -> typ -> typ
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    13
  val term: theory -> term -> term
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    14
end;
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    15
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    16
structure Compress: COMPRESS =
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    17
struct
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    18
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    19
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    20
(* theory data *)
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    21
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    22
structure CompressData = TheoryDataFun
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 20548
diff changeset
    23
(
16981
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    24
  type T = typ Typtab.table ref * term Termtab.table ref;
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    25
  val empty : T = (ref Typtab.empty, ref Termtab.empty);
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    26
  fun copy (ref typs, ref terms) : T = (ref typs, ref terms);
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    27
  val extend = copy;
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    28
  fun merge _ ((ref typs1, ref terms1), (ref typs2, ref terms2)) : T =
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    29
   (ref (Typtab.merge (K true) (typs1, typs2)),
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    30
    ref (Termtab.merge (K true) (terms1, terms2)));
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 20548
diff changeset
    31
);
16981
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    32
26427
f33d1b522316 implicit setup of emerging theory Pure;
wenzelm
parents: 25697
diff changeset
    33
val _ = Context.>> CompressData.init;
16981
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    34
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    35
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    36
(* compress types *)
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    37
25697
a4b7eb4e20fd non-critical (accidental concurrent access does not affect functional integrity);
wenzelm
parents: 24058
diff changeset
    38
fun typ thy =
16981
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    39
  let
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    40
    val typs = #1 (CompressData.get thy);
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    41
    fun compress T =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
    42
      (case Typtab.lookup (! typs) T of
16981
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    43
        SOME T' => T'
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    44
      | NONE =>
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    45
          let val T' = (case T of Type (a, Ts) => Type (a, map compress Ts) | _ => T)
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
    46
          in change typs (Typtab.update (T', T')); T' end);
16981
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    47
  in compress end;
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    48
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    49
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    50
(* compress atomic terms *)
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    51
25697
a4b7eb4e20fd non-critical (accidental concurrent access does not affect functional integrity);
wenzelm
parents: 24058
diff changeset
    52
fun term thy =
16981
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    53
  let
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    54
    val terms = #2 (CompressData.get thy);
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    55
    fun compress (t $ u) = compress t $ compress u
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    56
      | compress (Abs (a, T, t)) = Abs (a, T, compress t)
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    57
      | compress t =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
    58
          (case Termtab.lookup (! terms) t of
16981
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    59
            SOME t' => t'
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
    60
          | NONE => (change terms (Termtab.update (t, t)); t));
25697
a4b7eb4e20fd non-critical (accidental concurrent access does not affect functional integrity);
wenzelm
parents: 24058
diff changeset
    61
  in compress o map_types (typ thy) end;
16981
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    62
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    63
end;