src/Pure/term_ord.ML
author Fabian Huch <huch@in.tum.de>
Thu, 06 Jun 2024 08:58:58 +0200
changeset 80258 60013c49cedc
parent 79096 48187f1a615e
permissions -rw-r--r--
more build manager page;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29269
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/term_ord.ML
74265
wenzelm
parents: 74260
diff changeset
     2
    Author:     Tobias Nipkow, TU Muenchen
wenzelm
parents: 74260
diff changeset
     3
    Author:     Makarius
29269
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
     4
74270
ad2899cdd528 clarified modules;
wenzelm
parents: 74269
diff changeset
     5
Term orderings.
29269
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
     6
*)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
     7
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
     8
signature TERM_ORD =
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
     9
sig
70586
57df8a85317a clarified signature;
wenzelm
parents: 67561
diff changeset
    10
  val fast_indexname_ord: indexname ord
57df8a85317a clarified signature;
wenzelm
parents: 67561
diff changeset
    11
  val sort_ord: sort ord
57df8a85317a clarified signature;
wenzelm
parents: 67561
diff changeset
    12
  val typ_ord: typ ord
57df8a85317a clarified signature;
wenzelm
parents: 67561
diff changeset
    13
  val fast_term_ord: term ord
57df8a85317a clarified signature;
wenzelm
parents: 67561
diff changeset
    14
  val syntax_term_ord: term ord
57df8a85317a clarified signature;
wenzelm
parents: 67561
diff changeset
    15
  val indexname_ord: indexname ord
57df8a85317a clarified signature;
wenzelm
parents: 67561
diff changeset
    16
  val tvar_ord: (indexname * sort) ord
57df8a85317a clarified signature;
wenzelm
parents: 67561
diff changeset
    17
  val var_ord: (indexname * typ) ord
57df8a85317a clarified signature;
wenzelm
parents: 67561
diff changeset
    18
  val term_ord: term ord
57df8a85317a clarified signature;
wenzelm
parents: 67561
diff changeset
    19
  val hd_ord: term ord
57df8a85317a clarified signature;
wenzelm
parents: 67561
diff changeset
    20
  val term_lpo: (term -> int) -> term ord
29269
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    21
end;
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    22
35408
b48ab741683b modernized structure Term_Ord;
wenzelm
parents: 35406
diff changeset
    23
structure Term_Ord: TERM_ORD =
29269
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    24
struct
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    25
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    26
(* fast syntactic ordering -- tuned for inequalities *)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    27
74260
bb37fb85d82c pointer_eq_ord: minor performance tuning;
wenzelm
parents: 73863
diff changeset
    28
val fast_indexname_ord =
bb37fb85d82c pointer_eq_ord: minor performance tuning;
wenzelm
parents: 73863
diff changeset
    29
  pointer_eq_ord (int_ord o apply2 snd ||| fast_string_ord o apply2 fst);
29269
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    30
74260
bb37fb85d82c pointer_eq_ord: minor performance tuning;
wenzelm
parents: 73863
diff changeset
    31
val sort_ord =
bb37fb85d82c pointer_eq_ord: minor performance tuning;
wenzelm
parents: 73863
diff changeset
    32
  pointer_eq_ord (dict_ord fast_string_ord);
29269
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    33
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    34
local
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    35
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    36
fun cons_nr (TVar _) = 0
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    37
  | cons_nr (TFree _) = 1
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    38
  | cons_nr (Type _) = 2;
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    39
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    40
in
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    41
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    42
fun typ_ord TU =
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    43
  if pointer_eq TU then EQUAL
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    44
  else
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    45
    (case TU of
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    46
      (Type (a, Ts), Type (b, Us)) =>
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    47
        (case fast_string_ord (a, b) of EQUAL => dict_ord typ_ord (Ts, Us) | ord => ord)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    48
    | (TFree (a, S), TFree (b, S')) =>
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    49
        (case fast_string_ord (a, b) of EQUAL => sort_ord (S, S') | ord => ord)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    50
    | (TVar (xi, S), TVar (yj, S')) =>
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    51
        (case fast_indexname_ord (xi, yj) of EQUAL => sort_ord (S, S') | ord => ord)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    52
    | (T, U) => int_ord (cons_nr T, cons_nr U));
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    53
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    54
end;
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    55
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    56
local
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    57
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    58
fun cons_nr (Const _) = 0
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    59
  | cons_nr (Free _) = 1
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    60
  | cons_nr (Var _) = 2
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    61
  | cons_nr (Bound _) = 3
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    62
  | cons_nr (Abs _) = 4
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    63
  | cons_nr (_ $ _) = 5;
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    64
77883
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    65
fun struct_ord tu =
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    66
  if pointer_eq tu then EQUAL
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    67
  else
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    68
    (case tu of
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    69
      (Abs (_, _, t), Abs (_, _, u)) => struct_ord (t, u)
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    70
    | (t1 $ t2, u1 $ u2) =>
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    71
        (case struct_ord (t1, u1) of EQUAL => struct_ord (t2, u2) | ord => ord)
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    72
    | (t, u) => int_ord (cons_nr t, cons_nr u));
29269
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    73
77883
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    74
fun atoms_ord tu =
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    75
  if pointer_eq tu then EQUAL
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    76
  else
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    77
    (case tu of
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    78
      (Abs (_, _, t), Abs (_, _, u)) => atoms_ord (t, u)
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    79
    | (t1 $ t2, u1 $ u2) =>
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    80
        (case atoms_ord (t1, u1) of EQUAL => atoms_ord (t2, u2) | ord => ord)
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    81
    | (Const (a, _), Const (b, _)) => fast_string_ord (a, b)
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    82
    | (Free (x, _), Free (y, _)) => fast_string_ord (x, y)
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    83
    | (Var (xi, _), Var (yj, _)) => fast_indexname_ord (xi, yj)
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    84
    | (Bound i, Bound j) => int_ord (i, j)
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    85
    | _ => EQUAL);
29269
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
    86
77883
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    87
fun types_ord tu =
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    88
  if pointer_eq tu then EQUAL
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    89
  else
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    90
    (case tu of
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    91
      (Abs (_, T, t), Abs (_, U, u)) =>
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    92
        (case typ_ord (T, U) of EQUAL => types_ord (t, u) | ord => ord)
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    93
    | (t1 $ t2, u1 $ u2) =>
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    94
        (case types_ord (t1, u1) of EQUAL => types_ord (t2, u2) | ord => ord)
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    95
    | (Const (_, T), Const (_, U)) => typ_ord (T, U)
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    96
    | (Free (_, T), Free (_, U)) => typ_ord (T, U)
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    97
    | (Var (_, T), Var (_, U)) => typ_ord (T, U)
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
    98
    | _ => EQUAL);
43794
49cbbe2768a8 sub-structural sharing after Syntax.check phase, with global interning of logical entities (the latter is relevant when bypassing default parsing via YXML);
wenzelm
parents: 37852
diff changeset
    99
77883
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
   100
fun comments_ord tu =
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
   101
  if pointer_eq tu then EQUAL
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
   102
  else
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
   103
    (case tu of
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
   104
      (Abs (x, _, t), Abs (y, _, u)) =>
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
   105
        (case fast_string_ord (x, y) of EQUAL => comments_ord (t, u) | ord => ord)
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
   106
    | (t1 $ t2, u1 $ u2) =>
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
   107
        (case comments_ord (t1, u1) of EQUAL => comments_ord (t2, u2) | ord => ord)
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
   108
    | _ => EQUAL);
29269
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   109
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   110
in
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   111
77883
2cd00c4054ab minor performance tuning: recursive check of pointer_eq;
wenzelm
parents: 77869
diff changeset
   112
val fast_term_ord = struct_ord ||| atoms_ord ||| types_ord;
29269
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   113
73863
wenzelm
parents: 70586
diff changeset
   114
val syntax_term_ord = fast_term_ord ||| comments_ord;
43794
49cbbe2768a8 sub-structural sharing after Syntax.check phase, with global interning of logical entities (the latter is relevant when bypassing default parsing via YXML);
wenzelm
parents: 37852
diff changeset
   115
29269
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   116
end;
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   117
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   118
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   119
(* term_ord *)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   120
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   121
(*a linear well-founded AC-compatible ordering for terms:
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   122
  s < t <=> 1. size(s) < size(t) or
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   123
            2. size(s) = size(t) and s=f(...) and t=g(...) and f<g or
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   124
            3. size(s) = size(t) and s=f(s1..sn) and t=f(t1..tn) and
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   125
               (s1..sn) < (t1..tn) (lexicographically)*)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   126
73863
wenzelm
parents: 70586
diff changeset
   127
val indexname_ord = int_ord o apply2 #2 ||| string_ord o apply2 #1;
29269
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   128
val tvar_ord = prod_ord indexname_ord sort_ord;
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   129
val var_ord = prod_ord indexname_ord typ_ord;
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   130
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   131
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   132
local
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   133
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   134
fun hd_depth (t $ _, n) = hd_depth (t, n + 1)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   135
  | hd_depth p = p;
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   136
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   137
fun dest_hd (Const (a, T)) = (((a, 0), T), 0)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   138
  | dest_hd (Free (a, T)) = (((a, 0), T), 1)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   139
  | dest_hd (Var v) = (v, 2)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   140
  | dest_hd (Bound i) = ((("", i), dummyT), 3)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   141
  | dest_hd (Abs (_, T, _)) = ((("", 0), T), 4);
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   142
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   143
in
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   144
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   145
fun term_ord tu =
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   146
  if pointer_eq tu then EQUAL
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   147
  else
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   148
    (case tu of
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   149
      (Abs (_, T, t), Abs(_, U, u)) =>
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   150
        (case term_ord (t, u) of EQUAL => typ_ord (T, U) | ord => ord)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   151
    | (t, u) =>
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   152
        (case int_ord (size_of_term t, size_of_term u) of
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   153
          EQUAL =>
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   154
            (case prod_ord hd_ord int_ord (hd_depth (t, 0), hd_depth (u, 0)) of
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   155
              EQUAL => args_ord (t, u) | ord => ord)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   156
        | ord => ord))
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   157
and hd_ord (f, g) =
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   158
  prod_ord (prod_ord indexname_ord typ_ord) int_ord (dest_hd f, dest_hd g)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   159
and args_ord (f $ t, g $ u) =
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   160
      (case args_ord (f, g) of EQUAL => term_ord (t, u) | ord => ord)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   161
  | args_ord _ = EQUAL;
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   162
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   163
end;
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   164
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   165
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   166
(* Lexicographic path order on terms *)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   167
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   168
(*
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   169
  See Baader & Nipkow, Term rewriting, CUP 1998.
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   170
  Without variables.  Const, Var, Bound, Free and Abs are treated all as
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   171
  constants.
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   172
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   173
  f_ord maps terms to integers and serves two purposes:
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   174
  - Predicate on constant symbols.  Those that are not recognised by f_ord
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   175
    must be mapped to ~1.
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   176
  - Order on the recognised symbols.  These must be mapped to distinct
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   177
    integers >= 0.
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   178
  The argument of f_ord is never an application.
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   179
*)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   180
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   181
local
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   182
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   183
fun unrecognized (Const (a, T)) = ((1, ((a, 0), T)), 0)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   184
  | unrecognized (Free (a, T)) = ((1, ((a, 0), T)), 0)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   185
  | unrecognized (Var v) = ((1, v), 1)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   186
  | unrecognized (Bound i) = ((1, (("", i), dummyT)), 2)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   187
  | unrecognized (Abs (_, T, _)) = ((1, (("", 0), T)), 3);
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   188
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   189
fun dest_hd f_ord t =
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   190
  let val ord = f_ord t
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   191
  in if ord = ~1 then unrecognized t else ((0, (("", ord), fastype_of t)), 0) end;
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   192
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   193
fun term_lpo f_ord (s, t) =
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   194
  let val (f, ss) = strip_comb s and (g, ts) = strip_comb t in
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   195
    if forall (fn si => term_lpo f_ord (si, t) = LESS) ss
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   196
    then case hd_ord f_ord (f, g) of
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   197
        GREATER =>
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   198
          if forall (fn ti => term_lpo f_ord (s, ti) = GREATER) ts
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   199
          then GREATER else LESS
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   200
      | EQUAL =>
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   201
          if forall (fn ti => term_lpo f_ord (s, ti) = GREATER) ts
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   202
          then list_ord (term_lpo f_ord) (ss, ts)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   203
          else LESS
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   204
      | LESS => LESS
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   205
    else GREATER
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   206
  end
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   207
and hd_ord f_ord (f, g) = case (f, g) of
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   208
    (Abs (_, T, t), Abs (_, U, u)) =>
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   209
      (case term_lpo f_ord (t, u) of EQUAL => typ_ord (T, U) | ord => ord)
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   210
  | (_, _) => prod_ord (prod_ord int_ord
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   211
                  (prod_ord indexname_ord typ_ord)) int_ord
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   212
                (dest_hd f_ord f, dest_hd f_ord g);
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   213
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   214
in
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   215
val term_lpo = term_lpo
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   216
end;
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   217
74270
ad2899cdd528 clarified modules;
wenzelm
parents: 74269
diff changeset
   218
end;
ad2899cdd528 clarified modules;
wenzelm
parents: 74269
diff changeset
   219
29269
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
diff changeset
   220
74265
wenzelm
parents: 74260
diff changeset
   221
(* scalable collections *)
32841
57dcddf81b01 added term_cache;
wenzelm
parents: 31976
diff changeset
   222
74270
ad2899cdd528 clarified modules;
wenzelm
parents: 74269
diff changeset
   223
structure Vartab = Table(type key = indexname val ord = Term_Ord.fast_indexname_ord);
ad2899cdd528 clarified modules;
wenzelm
parents: 74269
diff changeset
   224
structure Sorttab = Table(type key = sort val ord = Term_Ord.sort_ord);
ad2899cdd528 clarified modules;
wenzelm
parents: 74269
diff changeset
   225
structure Typtab = Table(type key = typ val ord = Term_Ord.typ_ord);
74266
612b7e0d6721 clarified signature;
wenzelm
parents: 74265
diff changeset
   226
74270
ad2899cdd528 clarified modules;
wenzelm
parents: 74269
diff changeset
   227
structure Termtab:
ad2899cdd528 clarified modules;
wenzelm
parents: 74269
diff changeset
   228
sig
77729
wenzelm
parents: 77723
diff changeset
   229
  include TABLE
74270
ad2899cdd528 clarified modules;
wenzelm
parents: 74269
diff changeset
   230
  val term_cache: (term -> 'a) -> term -> 'a
ad2899cdd528 clarified modules;
wenzelm
parents: 74269
diff changeset
   231
end =
74266
612b7e0d6721 clarified signature;
wenzelm
parents: 74265
diff changeset
   232
struct
77729
wenzelm
parents: 77723
diff changeset
   233
wenzelm
parents: 77723
diff changeset
   234
structure Table = Table(type key = term val ord = Term_Ord.fast_term_ord);
wenzelm
parents: 77723
diff changeset
   235
open Table;
wenzelm
parents: 77723
diff changeset
   236
wenzelm
parents: 77723
diff changeset
   237
fun term_cache f = Cache.create empty lookup update f;
wenzelm
parents: 77723
diff changeset
   238
74266
612b7e0d6721 clarified signature;
wenzelm
parents: 74265
diff changeset
   239
end;
612b7e0d6721 clarified signature;
wenzelm
parents: 74265
diff changeset
   240
79096
48187f1a615e reduce redundancy: avoid huge lists;
wenzelm
parents: 77883
diff changeset
   241
structure Typset = Set(Typtab.Key);
77731
48fbecc8fab1 tuned signature: more uniform structure Key;
wenzelm
parents: 77730
diff changeset
   242
structure Termset = Set(Termtab.Key);
77729
wenzelm
parents: 77723
diff changeset
   243
77731
48fbecc8fab1 tuned signature: more uniform structure Key;
wenzelm
parents: 77730
diff changeset
   244
structure Var_Graph = Graph(Vartab.Key);
48fbecc8fab1 tuned signature: more uniform structure Key;
wenzelm
parents: 77730
diff changeset
   245
structure Typ_Graph = Graph(Typtab.Key);
48fbecc8fab1 tuned signature: more uniform structure Key;
wenzelm
parents: 77730
diff changeset
   246
structure Term_Graph = Graph(Termtab.Key);