src/Pure/term_ord.ML
author wenzelm
Sun Mar 01 23:36:12 2009 +0100 (2009-03-01)
changeset 30190 479806475f3c
parent 29269 5c25a2012975
child 31971 8c1b845ed105
permissions -rw-r--r--
use long names for old-style fold combinators;
wenzelm@29269
     1
(*  Title:      Pure/term_ord.ML
wenzelm@29269
     2
    Author:     Tobias Nipkow and Makarius, TU Muenchen
wenzelm@29269
     3
wenzelm@29269
     4
Term orderings.
wenzelm@29269
     5
*)
wenzelm@29269
     6
wenzelm@29269
     7
signature TERM_ORD =
wenzelm@29269
     8
sig
wenzelm@29269
     9
  val fast_indexname_ord: indexname * indexname -> order
wenzelm@29269
    10
  val sort_ord: sort * sort -> order
wenzelm@29269
    11
  val typ_ord: typ * typ -> order
wenzelm@29269
    12
  val fast_term_ord: term * term -> order
wenzelm@29269
    13
  val indexname_ord: indexname * indexname -> order
wenzelm@29269
    14
  val tvar_ord: (indexname * sort) * (indexname * sort) -> order
wenzelm@29269
    15
  val var_ord: (indexname * typ) * (indexname * typ) -> order
wenzelm@29269
    16
  val term_ord: term * term -> order
wenzelm@29269
    17
  val hd_ord: term * term -> order
wenzelm@29269
    18
  val termless: term * term -> bool
wenzelm@29269
    19
  val term_lpo: (term -> int) -> term * term -> order
wenzelm@29269
    20
end;
wenzelm@29269
    21
wenzelm@29269
    22
structure TermOrd: TERM_ORD =
wenzelm@29269
    23
struct
wenzelm@29269
    24
wenzelm@29269
    25
(* fast syntactic ordering -- tuned for inequalities *)
wenzelm@29269
    26
wenzelm@29269
    27
fun fast_indexname_ord ((x, i), (y, j)) =
wenzelm@29269
    28
  (case int_ord (i, j) of EQUAL => fast_string_ord (x, y) | ord => ord);
wenzelm@29269
    29
wenzelm@29269
    30
fun sort_ord SS =
wenzelm@29269
    31
  if pointer_eq SS then EQUAL
wenzelm@29269
    32
  else dict_ord fast_string_ord SS;
wenzelm@29269
    33
wenzelm@29269
    34
local
wenzelm@29269
    35
wenzelm@29269
    36
fun cons_nr (TVar _) = 0
wenzelm@29269
    37
  | cons_nr (TFree _) = 1
wenzelm@29269
    38
  | cons_nr (Type _) = 2;
wenzelm@29269
    39
wenzelm@29269
    40
in
wenzelm@29269
    41
wenzelm@29269
    42
fun typ_ord TU =
wenzelm@29269
    43
  if pointer_eq TU then EQUAL
wenzelm@29269
    44
  else
wenzelm@29269
    45
    (case TU of
wenzelm@29269
    46
      (Type (a, Ts), Type (b, Us)) =>
wenzelm@29269
    47
        (case fast_string_ord (a, b) of EQUAL => dict_ord typ_ord (Ts, Us) | ord => ord)
wenzelm@29269
    48
    | (TFree (a, S), TFree (b, S')) =>
wenzelm@29269
    49
        (case fast_string_ord (a, b) of EQUAL => sort_ord (S, S') | ord => ord)
wenzelm@29269
    50
    | (TVar (xi, S), TVar (yj, S')) =>
wenzelm@29269
    51
        (case fast_indexname_ord (xi, yj) of EQUAL => sort_ord (S, S') | ord => ord)
wenzelm@29269
    52
    | (T, U) => int_ord (cons_nr T, cons_nr U));
wenzelm@29269
    53
wenzelm@29269
    54
end;
wenzelm@29269
    55
wenzelm@29269
    56
local
wenzelm@29269
    57
wenzelm@29269
    58
fun cons_nr (Const _) = 0
wenzelm@29269
    59
  | cons_nr (Free _) = 1
wenzelm@29269
    60
  | cons_nr (Var _) = 2
wenzelm@29269
    61
  | cons_nr (Bound _) = 3
wenzelm@29269
    62
  | cons_nr (Abs _) = 4
wenzelm@29269
    63
  | cons_nr (_ $ _) = 5;
wenzelm@29269
    64
wenzelm@29269
    65
fun struct_ord (Abs (_, _, t), Abs (_, _, u)) = struct_ord (t, u)
wenzelm@29269
    66
  | struct_ord (t1 $ t2, u1 $ u2) =
wenzelm@29269
    67
      (case struct_ord (t1, u1) of EQUAL => struct_ord (t2, u2) | ord => ord)
wenzelm@29269
    68
  | struct_ord (t, u) = int_ord (cons_nr t, cons_nr u);
wenzelm@29269
    69
wenzelm@29269
    70
fun atoms_ord (Abs (_, _, t), Abs (_, _, u)) = atoms_ord (t, u)
wenzelm@29269
    71
  | atoms_ord (t1 $ t2, u1 $ u2) =
wenzelm@29269
    72
      (case atoms_ord (t1, u1) of EQUAL => atoms_ord (t2, u2) | ord => ord)
wenzelm@29269
    73
  | atoms_ord (Const (a, _), Const (b, _)) = fast_string_ord (a, b)
wenzelm@29269
    74
  | atoms_ord (Free (x, _), Free (y, _)) = fast_string_ord (x, y)
wenzelm@29269
    75
  | atoms_ord (Var (xi, _), Var (yj, _)) = fast_indexname_ord (xi, yj)
wenzelm@29269
    76
  | atoms_ord (Bound i, Bound j) = int_ord (i, j)
wenzelm@29269
    77
  | atoms_ord _ = sys_error "atoms_ord";
wenzelm@29269
    78
wenzelm@29269
    79
fun types_ord (Abs (_, T, t), Abs (_, U, u)) =
wenzelm@29269
    80
      (case typ_ord (T, U) of EQUAL => types_ord (t, u) | ord => ord)
wenzelm@29269
    81
  | types_ord (t1 $ t2, u1 $ u2) =
wenzelm@29269
    82
      (case types_ord (t1, u1) of EQUAL => types_ord (t2, u2) | ord => ord)
wenzelm@29269
    83
  | types_ord (Const (_, T), Const (_, U)) = typ_ord (T, U)
wenzelm@29269
    84
  | types_ord (Free (_, T), Free (_, U)) = typ_ord (T, U)
wenzelm@29269
    85
  | types_ord (Var (_, T), Var (_, U)) = typ_ord (T, U)
wenzelm@29269
    86
  | types_ord (Bound _, Bound _) = EQUAL
wenzelm@29269
    87
  | types_ord _ = sys_error "types_ord";
wenzelm@29269
    88
wenzelm@29269
    89
in
wenzelm@29269
    90
wenzelm@29269
    91
fun fast_term_ord tu =
wenzelm@29269
    92
  if pointer_eq tu then EQUAL
wenzelm@29269
    93
  else
wenzelm@29269
    94
    (case struct_ord tu of
wenzelm@29269
    95
      EQUAL => (case atoms_ord tu of EQUAL => types_ord tu | ord => ord)
wenzelm@29269
    96
    | ord => ord);
wenzelm@29269
    97
wenzelm@29269
    98
end;
wenzelm@29269
    99
wenzelm@29269
   100
wenzelm@29269
   101
(* term_ord *)
wenzelm@29269
   102
wenzelm@29269
   103
(*a linear well-founded AC-compatible ordering for terms:
wenzelm@29269
   104
  s < t <=> 1. size(s) < size(t) or
wenzelm@29269
   105
            2. size(s) = size(t) and s=f(...) and t=g(...) and f<g or
wenzelm@29269
   106
            3. size(s) = size(t) and s=f(s1..sn) and t=f(t1..tn) and
wenzelm@29269
   107
               (s1..sn) < (t1..tn) (lexicographically)*)
wenzelm@29269
   108
wenzelm@29269
   109
fun indexname_ord ((x, i), (y, j)) =
wenzelm@29269
   110
  (case int_ord (i, j) of EQUAL => string_ord (x, y) | ord => ord);
wenzelm@29269
   111
wenzelm@29269
   112
val tvar_ord = prod_ord indexname_ord sort_ord;
wenzelm@29269
   113
val var_ord = prod_ord indexname_ord typ_ord;
wenzelm@29269
   114
wenzelm@29269
   115
wenzelm@29269
   116
local
wenzelm@29269
   117
wenzelm@29269
   118
fun hd_depth (t $ _, n) = hd_depth (t, n + 1)
wenzelm@29269
   119
  | hd_depth p = p;
wenzelm@29269
   120
wenzelm@29269
   121
fun dest_hd (Const (a, T)) = (((a, 0), T), 0)
wenzelm@29269
   122
  | dest_hd (Free (a, T)) = (((a, 0), T), 1)
wenzelm@29269
   123
  | dest_hd (Var v) = (v, 2)
wenzelm@29269
   124
  | dest_hd (Bound i) = ((("", i), dummyT), 3)
wenzelm@29269
   125
  | dest_hd (Abs (_, T, _)) = ((("", 0), T), 4);
wenzelm@29269
   126
wenzelm@29269
   127
in
wenzelm@29269
   128
wenzelm@29269
   129
fun term_ord tu =
wenzelm@29269
   130
  if pointer_eq tu then EQUAL
wenzelm@29269
   131
  else
wenzelm@29269
   132
    (case tu of
wenzelm@29269
   133
      (Abs (_, T, t), Abs(_, U, u)) =>
wenzelm@29269
   134
        (case term_ord (t, u) of EQUAL => typ_ord (T, U) | ord => ord)
wenzelm@29269
   135
    | (t, u) =>
wenzelm@29269
   136
        (case int_ord (size_of_term t, size_of_term u) of
wenzelm@29269
   137
          EQUAL =>
wenzelm@29269
   138
            (case prod_ord hd_ord int_ord (hd_depth (t, 0), hd_depth (u, 0)) of
wenzelm@29269
   139
              EQUAL => args_ord (t, u) | ord => ord)
wenzelm@29269
   140
        | ord => ord))
wenzelm@29269
   141
and hd_ord (f, g) =
wenzelm@29269
   142
  prod_ord (prod_ord indexname_ord typ_ord) int_ord (dest_hd f, dest_hd g)
wenzelm@29269
   143
and args_ord (f $ t, g $ u) =
wenzelm@29269
   144
      (case args_ord (f, g) of EQUAL => term_ord (t, u) | ord => ord)
wenzelm@29269
   145
  | args_ord _ = EQUAL;
wenzelm@29269
   146
wenzelm@29269
   147
fun termless tu = (term_ord tu = LESS);
wenzelm@29269
   148
wenzelm@29269
   149
end;
wenzelm@29269
   150
wenzelm@29269
   151
wenzelm@29269
   152
(* Lexicographic path order on terms *)
wenzelm@29269
   153
wenzelm@29269
   154
(*
wenzelm@29269
   155
  See Baader & Nipkow, Term rewriting, CUP 1998.
wenzelm@29269
   156
  Without variables.  Const, Var, Bound, Free and Abs are treated all as
wenzelm@29269
   157
  constants.
wenzelm@29269
   158
wenzelm@29269
   159
  f_ord maps terms to integers and serves two purposes:
wenzelm@29269
   160
  - Predicate on constant symbols.  Those that are not recognised by f_ord
wenzelm@29269
   161
    must be mapped to ~1.
wenzelm@29269
   162
  - Order on the recognised symbols.  These must be mapped to distinct
wenzelm@29269
   163
    integers >= 0.
wenzelm@29269
   164
  The argument of f_ord is never an application.
wenzelm@29269
   165
*)
wenzelm@29269
   166
wenzelm@29269
   167
local
wenzelm@29269
   168
wenzelm@29269
   169
fun unrecognized (Const (a, T)) = ((1, ((a, 0), T)), 0)
wenzelm@29269
   170
  | unrecognized (Free (a, T)) = ((1, ((a, 0), T)), 0)
wenzelm@29269
   171
  | unrecognized (Var v) = ((1, v), 1)
wenzelm@29269
   172
  | unrecognized (Bound i) = ((1, (("", i), dummyT)), 2)
wenzelm@29269
   173
  | unrecognized (Abs (_, T, _)) = ((1, (("", 0), T)), 3);
wenzelm@29269
   174
wenzelm@29269
   175
fun dest_hd f_ord t =
wenzelm@29269
   176
  let val ord = f_ord t
wenzelm@29269
   177
  in if ord = ~1 then unrecognized t else ((0, (("", ord), fastype_of t)), 0) end;
wenzelm@29269
   178
wenzelm@29269
   179
fun term_lpo f_ord (s, t) =
wenzelm@29269
   180
  let val (f, ss) = strip_comb s and (g, ts) = strip_comb t in
wenzelm@29269
   181
    if forall (fn si => term_lpo f_ord (si, t) = LESS) ss
wenzelm@29269
   182
    then case hd_ord f_ord (f, g) of
wenzelm@29269
   183
        GREATER =>
wenzelm@29269
   184
          if forall (fn ti => term_lpo f_ord (s, ti) = GREATER) ts
wenzelm@29269
   185
          then GREATER else LESS
wenzelm@29269
   186
      | EQUAL =>
wenzelm@29269
   187
          if forall (fn ti => term_lpo f_ord (s, ti) = GREATER) ts
wenzelm@29269
   188
          then list_ord (term_lpo f_ord) (ss, ts)
wenzelm@29269
   189
          else LESS
wenzelm@29269
   190
      | LESS => LESS
wenzelm@29269
   191
    else GREATER
wenzelm@29269
   192
  end
wenzelm@29269
   193
and hd_ord f_ord (f, g) = case (f, g) of
wenzelm@29269
   194
    (Abs (_, T, t), Abs (_, U, u)) =>
wenzelm@29269
   195
      (case term_lpo f_ord (t, u) of EQUAL => typ_ord (T, U) | ord => ord)
wenzelm@29269
   196
  | (_, _) => prod_ord (prod_ord int_ord
wenzelm@29269
   197
                  (prod_ord indexname_ord typ_ord)) int_ord
wenzelm@29269
   198
                (dest_hd f_ord f, dest_hd f_ord g);
wenzelm@29269
   199
wenzelm@29269
   200
in
wenzelm@29269
   201
val term_lpo = term_lpo
wenzelm@29269
   202
end;
wenzelm@29269
   203
wenzelm@29269
   204
wenzelm@29269
   205
end;
wenzelm@29269
   206
wenzelm@29269
   207
structure Vartab = TableFun(type key = indexname val ord = TermOrd.fast_indexname_ord);
wenzelm@29269
   208
structure Typtab = TableFun(type key = typ val ord = TermOrd.typ_ord);
wenzelm@29269
   209
structure Termtab = TableFun(type key = term val ord = TermOrd.fast_term_ord);