src/Pure/interpretation.ML
author wenzelm
Wed, 21 Mar 2012 23:26:35 +0100
changeset 47069 451fc10a81f3
parent 33522 737589bb9bb8
child 58177 166131276380
permissions -rw-r--r--
more explicit Toplevel.open_target/close_target; replaced 'context_includes' by 'context' 'includes'; tuned command descriptions;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24667
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/interpretation.ML
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
     2
    Author:     Florian Haftmann and Makarius
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
     3
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
     4
Generic interpretation of theory data.
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
     5
*)
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
     6
24711
e8bba7723858 simplified interpretation setup;
wenzelm
parents: 24667
diff changeset
     7
signature INTERPRETATION =
24667
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
     8
sig
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
     9
  type T
24711
e8bba7723858 simplified interpretation setup;
wenzelm
parents: 24667
diff changeset
    10
  val result: theory -> T list
e8bba7723858 simplified interpretation setup;
wenzelm
parents: 24667
diff changeset
    11
  val interpretation: (T -> theory -> theory) -> theory -> theory
e8bba7723858 simplified interpretation setup;
wenzelm
parents: 24667
diff changeset
    12
  val data: T -> theory -> theory
24667
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
    13
  val init: theory -> theory
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
    14
end;
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
    15
33314
53d49370f7af modernized functor/structures Interpretation;
wenzelm
parents: 29606
diff changeset
    16
functor Interpretation(type T val eq: T * T -> bool): INTERPRETATION =
24667
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
    17
struct
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
    18
24711
e8bba7723858 simplified interpretation setup;
wenzelm
parents: 24667
diff changeset
    19
type T = T;
24667
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
    20
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33314
diff changeset
    21
structure Interp = Theory_Data
24667
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
    22
(
24711
e8bba7723858 simplified interpretation setup;
wenzelm
parents: 24667
diff changeset
    23
  type T = T list * (((T -> theory -> theory) * stamp) * T list) list;
e8bba7723858 simplified interpretation setup;
wenzelm
parents: 24667
diff changeset
    24
  val empty = ([], []);
24667
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
    25
  val extend = I;
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33314
diff changeset
    26
  fun merge ((data1, interps1), (data2, interps2)) : T =
24711
e8bba7723858 simplified interpretation setup;
wenzelm
parents: 24667
diff changeset
    27
    (Library.merge eq (data1, data2),
e8bba7723858 simplified interpretation setup;
wenzelm
parents: 24667
diff changeset
    28
     AList.join (eq_snd (op =)) (K (Library.merge eq)) (interps1, interps2));
24667
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
    29
);
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
    30
24711
e8bba7723858 simplified interpretation setup;
wenzelm
parents: 24667
diff changeset
    31
val result = #1 o Interp.get;
e8bba7723858 simplified interpretation setup;
wenzelm
parents: 24667
diff changeset
    32
24667
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
    33
fun consolidate thy =
24711
e8bba7723858 simplified interpretation setup;
wenzelm
parents: 24667
diff changeset
    34
  let
e8bba7723858 simplified interpretation setup;
wenzelm
parents: 24667
diff changeset
    35
    val (data, interps) = Interp.get thy;
24857
2dde4189a055 subtract: minor performance tuning;
wenzelm
parents: 24711
diff changeset
    36
    val unfinished = interps |> map (fn ((f, _), xs) =>
2dde4189a055 subtract: minor performance tuning;
wenzelm
parents: 24711
diff changeset
    37
      (f, if eq_list eq (xs, data) then [] else subtract eq xs data));
2dde4189a055 subtract: minor performance tuning;
wenzelm
parents: 24711
diff changeset
    38
    val finished = interps |> map (fn (interp, _) => (interp, data));
24711
e8bba7723858 simplified interpretation setup;
wenzelm
parents: 24667
diff changeset
    39
  in
e8bba7723858 simplified interpretation setup;
wenzelm
parents: 24667
diff changeset
    40
    if forall (null o #2) unfinished then NONE
e8bba7723858 simplified interpretation setup;
wenzelm
parents: 24667
diff changeset
    41
    else SOME (thy |> fold_rev (uncurry fold_rev) unfinished |> Interp.put (data, finished))
e8bba7723858 simplified interpretation setup;
wenzelm
parents: 24667
diff changeset
    42
  end;
e8bba7723858 simplified interpretation setup;
wenzelm
parents: 24667
diff changeset
    43
e8bba7723858 simplified interpretation setup;
wenzelm
parents: 24667
diff changeset
    44
fun interpretation f = Interp.map (apsnd (cons ((f, stamp ()), []))) #> perhaps consolidate;
e8bba7723858 simplified interpretation setup;
wenzelm
parents: 24667
diff changeset
    45
fun data x = Interp.map (apfst (cons x)) #> perhaps consolidate;
24667
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
    46
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
    47
val init = Theory.at_begin consolidate;
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
    48
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
    49
end;
9f29588d57d5 Generic interpretation of theory data.
wenzelm
parents:
diff changeset
    50