src/Pure/compress.ML
author wenzelm
Tue, 18 Dec 2007 19:54:31 +0100
changeset 25697 a4b7eb4e20fd
parent 24058 81aafd465662
child 26427 f33d1b522316
permissions -rw-r--r--
non-critical (accidental concurrent access does not affect functional integrity);
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 init_data: theory -> theory
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    13
  val typ: theory -> typ -> typ
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    14
  val term: theory -> term -> term
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    15
end;
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    16
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    17
structure Compress: COMPRESS =
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    18
struct
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
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    21
(* theory data *)
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    22
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    23
structure CompressData = TheoryDataFun
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 20548
diff changeset
    24
(
16981
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    25
  type T = typ Typtab.table ref * term Termtab.table ref;
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    26
  val empty : T = (ref Typtab.empty, ref Termtab.empty);
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    27
  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
    28
  val extend = copy;
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    29
  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
    30
   (ref (Typtab.merge (K true) (typs1, typs2)),
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    31
    ref (Termtab.merge (K true) (terms1, terms2)));
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 20548
diff changeset
    32
);
16981
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    33
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    34
val init_data = CompressData.init;
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
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    37
(* compress types *)
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    38
25697
a4b7eb4e20fd non-critical (accidental concurrent access does not affect functional integrity);
wenzelm
parents: 24058
diff changeset
    39
fun typ thy =
16981
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    40
  let
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    41
    val typs = #1 (CompressData.get thy);
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    42
    fun compress T =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
    43
      (case Typtab.lookup (! typs) T of
16981
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    44
        SOME T' => T'
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    45
      | NONE =>
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    46
          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
    47
          in change typs (Typtab.update (T', T')); T' end);
16981
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    48
  in compress end;
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
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    51
(* compress atomic terms *)
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    52
25697
a4b7eb4e20fd non-critical (accidental concurrent access does not affect functional integrity);
wenzelm
parents: 24058
diff changeset
    53
fun term thy =
16981
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    54
  let
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    55
    val terms = #2 (CompressData.get thy);
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    56
    fun compress (t $ u) = compress t $ compress u
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    57
      | compress (Abs (a, T, t)) = Abs (a, T, compress t)
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    58
      | compress t =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
    59
          (case Termtab.lookup (! terms) t of
16981
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    60
            SOME t' => t'
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
    61
          | 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
    62
  in compress o map_types (typ thy) end;
16981
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    63
2ae3f621d076 compression of terms and types by sharing common subtrees;
wenzelm
parents:
diff changeset
    64
end;