src/Pure/Syntax/ast.ML
author wenzelm
Wed, 16 Jan 2002 23:19:34 +0100
changeset 12785 27debaf2112d
parent 12262 11ff5f47df6e
child 14599 c3177fffd31a
permissions -rw-r--r--
GPLed;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
     1
(*  Title:      Pure/Syntax/ast.ML
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    ID:         $Id$
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
12785
wenzelm
parents: 12262
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
     6
Abstract syntax trees, translation rules, matching and normalization of asts.
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     8
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
     9
signature AST0 =
1506
192c48376d25 Elimination of fully-functorial style.
paulson
parents: 1127
diff changeset
    10
  sig
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    11
  datatype ast =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    12
    Constant of string |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    13
    Variable of string |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    14
    Appl of ast list
258
e540b7d4ecb1 minor internal changes;
wenzelm
parents: 235
diff changeset
    15
  exception AST of string * ast list
1506
192c48376d25 Elimination of fully-functorial style.
paulson
parents: 1127
diff changeset
    16
  end;
258
e540b7d4ecb1 minor internal changes;
wenzelm
parents: 235
diff changeset
    17
e540b7d4ecb1 minor internal changes;
wenzelm
parents: 235
diff changeset
    18
signature AST1 =
1506
192c48376d25 Elimination of fully-functorial style.
paulson
parents: 1127
diff changeset
    19
  sig
258
e540b7d4ecb1 minor internal changes;
wenzelm
parents: 235
diff changeset
    20
  include AST0
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    21
  val mk_appl: ast -> ast list -> ast
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    22
  val str_of_ast: ast -> string
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    23
  val pretty_ast: ast -> Pretty.T
258
e540b7d4ecb1 minor internal changes;
wenzelm
parents: 235
diff changeset
    24
  val pretty_rule: ast * ast -> Pretty.T
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    25
  val pprint_ast: ast -> pprint_args -> unit
10913
57eb8c1d6f88 export fold_ast etc.;
wenzelm
parents: 9372
diff changeset
    26
  val fold_ast: string -> ast list -> ast
57eb8c1d6f88 export fold_ast etc.;
wenzelm
parents: 9372
diff changeset
    27
  val fold_ast_p: string -> ast list * ast -> ast
57eb8c1d6f88 export fold_ast etc.;
wenzelm
parents: 9372
diff changeset
    28
  val unfold_ast: string -> ast -> ast list
57eb8c1d6f88 export fold_ast etc.;
wenzelm
parents: 9372
diff changeset
    29
  val unfold_ast_p: string -> ast -> ast list * ast
8997
da290d99d8b2 renamed trace/stat_norm_ast to trace/stat_ast;
wenzelm
parents: 5689
diff changeset
    30
  val trace_ast: bool ref
da290d99d8b2 renamed trace/stat_norm_ast to trace/stat_ast;
wenzelm
parents: 5689
diff changeset
    31
  val stat_ast: bool ref
1506
192c48376d25 Elimination of fully-functorial style.
paulson
parents: 1127
diff changeset
    32
  end;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    33
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    34
signature AST =
1506
192c48376d25 Elimination of fully-functorial style.
paulson
parents: 1127
diff changeset
    35
  sig
258
e540b7d4ecb1 minor internal changes;
wenzelm
parents: 235
diff changeset
    36
  include AST1
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    37
  val head_of_rule: ast * ast -> string
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    38
  val rule_error: ast * ast -> string option
9372
7834e56e2277 AST translation rules no longer require constant head on LHS;
wenzelm
parents: 8997
diff changeset
    39
  val normalize: bool -> bool -> (string -> (ast * ast) list) -> ast -> ast
7834e56e2277 AST translation rules no longer require constant head on LHS;
wenzelm
parents: 8997
diff changeset
    40
  val normalize_ast: (string -> (ast * ast) list) -> ast -> ast
1506
192c48376d25 Elimination of fully-functorial style.
paulson
parents: 1127
diff changeset
    41
  end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    42
1506
192c48376d25 Elimination of fully-functorial style.
paulson
parents: 1127
diff changeset
    43
structure Ast : AST =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    44
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    45
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    46
(** abstract syntax trees **)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    47
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    48
(*asts come in two flavours:
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    49
   - ordinary asts representing terms and typs: Variables are (often) treated
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    50
     like Constants;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    51
   - patterns used as lhs and rhs in rules: Variables are placeholders for
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    52
     proper asts*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    53
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    54
datatype ast =
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    55
  Constant of string |    (*"not", "_abs", "fun"*)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    56
  Variable of string |    (*x, ?x, 'a, ?'a*)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    57
  Appl of ast list;       (*(f x y z), ("fun" 'a 'b), ("_abs" x t)*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    58
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    59
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    60
(*the list of subasts of an Appl node has to contain at least 2 elements, i.e.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    61
  there are no empty asts or nullary applications; use mk_appl for convenience*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    62
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    63
fun mk_appl f [] = f
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    64
  | mk_appl f args = Appl (f :: args);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    65
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    66
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    67
(*exception for system errors involving asts*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    68
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    69
exception AST of string * ast list;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    70
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    71
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    72
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    73
(** print asts in a LISP-like style **)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    74
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    75
(* str_of_ast *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    76
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    77
fun str_of_ast (Constant a) = quote a
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    78
  | str_of_ast (Variable x) = x
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    79
  | str_of_ast (Appl asts) = "(" ^ (space_implode " " (map str_of_ast asts)) ^ ")";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    80
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    81
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    82
(* pretty_ast *)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    83
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    84
fun pretty_ast (Constant a) = Pretty.str (quote a)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    85
  | pretty_ast (Variable x) = Pretty.str x
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    86
  | pretty_ast (Appl asts) =
513
97a879e8d01b updated reference to parents
lcp
parents: 258
diff changeset
    87
      Pretty.enclose "(" ")" (Pretty.breaks (map pretty_ast asts));
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    88
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    89
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    90
(* pprint_ast *)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    91
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    92
val pprint_ast = Pretty.pprint o pretty_ast;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    93
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    94
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    95
(* pretty_rule *)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    96
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    97
fun pretty_rule (lhs, rhs) =
235
775dd81a58e5 cosmetic changes;
wenzelm
parents: 18
diff changeset
    98
  Pretty.block [pretty_ast lhs, Pretty.str "  ->", Pretty.brk 2, pretty_ast rhs];
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    99
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   100
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   101
(* head_of_ast, head_of_rule *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   102
9372
7834e56e2277 AST translation rules no longer require constant head on LHS;
wenzelm
parents: 8997
diff changeset
   103
fun head_of_ast (Constant a) = a
7834e56e2277 AST translation rules no longer require constant head on LHS;
wenzelm
parents: 8997
diff changeset
   104
  | head_of_ast (Appl (Constant a :: _)) = a
7834e56e2277 AST translation rules no longer require constant head on LHS;
wenzelm
parents: 8997
diff changeset
   105
  | head_of_ast _ = "";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   106
9372
7834e56e2277 AST translation rules no longer require constant head on LHS;
wenzelm
parents: 8997
diff changeset
   107
fun head_of_rule (lhs, _) = head_of_ast lhs;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   108
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   109
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   110
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   111
(** check translation rules **)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   112
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   113
(*a wellformed rule (lhs, rhs): (ast * ast) obeys the following conditions:
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   114
   - the lhs has unique vars,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   115
   - vars of rhs is subset of vars of lhs*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   116
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   117
fun rule_error (rule as (lhs, rhs)) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   118
  let
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   119
    fun vars_of (Constant _) = []
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   120
      | vars_of (Variable x) = [x]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   121
      | vars_of (Appl asts) = flat (map vars_of asts);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   122
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   123
    fun unique (x :: xs) = not (x mem xs) andalso unique xs
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   124
      | unique [] = true;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   125
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   126
    val lvars = vars_of lhs;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   127
    val rvars = vars_of rhs;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   128
  in
9372
7834e56e2277 AST translation rules no longer require constant head on LHS;
wenzelm
parents: 8997
diff changeset
   129
    if not (unique lvars) then Some "duplicate vars in lhs"
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   130
    else if not (rvars subset lvars) then Some "rhs contains extra variables"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   131
    else None
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   132
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   133
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   134
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   135
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   136
(** ast translation utilities **)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   137
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   138
(* fold asts *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   139
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   140
fun fold_ast _ [] = raise Match
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   141
  | fold_ast _ [y] = y
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   142
  | fold_ast c (x :: xs) = Appl [Constant c, x, fold_ast c xs];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   143
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   144
fun fold_ast_p c = foldr (fn (x, xs) => Appl [Constant c, x, xs]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   145
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   146
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   147
(* unfold asts *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   148
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   149
fun unfold_ast c (y as Appl [Constant c', x, xs]) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   150
      if c = c' then x :: (unfold_ast c xs) else [y]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   151
  | unfold_ast _ y = [y];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   152
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   153
fun unfold_ast_p c (y as Appl [Constant c', x, xs]) =
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   154
      if c = c' then apfst (cons x) (unfold_ast_p c xs)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   155
      else ([], y)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   156
  | unfold_ast_p _ y = ([], y);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   157
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   158
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   159
(** normalization of asts **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   160
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   161
(* match *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   162
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   163
fun match ast pat =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   164
  let
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   165
    exception NO_MATCH;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   166
1127
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   167
    fun mtch (Constant a) (Constant b) env =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   168
          if a = b then env else raise NO_MATCH
1127
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   169
      | mtch (Variable a) (Constant b) env =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   170
          if a = b then env else raise NO_MATCH
5689
ffecea547501 simple Env replaced by Symtab;
wenzelm
parents: 3775
diff changeset
   171
      | mtch ast (Variable x) env = Symtab.update ((x, ast), env)
1127
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   172
      | mtch (Appl asts) (Appl pats) env = mtch_lst asts pats env
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   173
      | mtch _ _ _ = raise NO_MATCH
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   174
    and mtch_lst (ast :: asts) (pat :: pats) env =
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   175
          mtch_lst asts pats (mtch ast pat env)
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   176
      | mtch_lst [] [] env = env
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   177
      | mtch_lst _ _ _ = raise NO_MATCH;
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   178
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   179
    val (head, args) =
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   180
      (case (ast, pat) of
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   181
        (Appl asts, Appl pats) =>
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   182
          let val a = length asts and p = length pats in
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   183
            if a > p then (Appl (take (p, asts)), drop (p, asts))
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   184
            else (ast, [])
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   185
          end
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   186
      | _ => (ast, []));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   187
  in
5689
ffecea547501 simple Env replaced by Symtab;
wenzelm
parents: 3775
diff changeset
   188
    Some (mtch head pat Symtab.empty, args) handle NO_MATCH => None
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   189
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   190
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   191
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   192
(* normalize *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   193
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   194
(*the normalizer works yoyo-like: top-down, bottom-up, top-down, ...*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   195
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   196
fun normalize trace stat get_rules pre_ast =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   197
  let
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   198
    val passes = ref 0;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   199
    val failed_matches = ref 0;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   200
    val changes = ref 0;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   201
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   202
    fun subst _ (ast as Constant _) = ast
5689
ffecea547501 simple Env replaced by Symtab;
wenzelm
parents: 3775
diff changeset
   203
      | subst env (Variable x) = the (Symtab.lookup (env, x))
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   204
      | subst env (Appl asts) = Appl (map (subst env) asts);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   205
11733
9dd88f3aa8e0 removed lookups count;
wenzelm
parents: 10913
diff changeset
   206
    fun try_rules ((lhs, rhs) :: pats) ast =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   207
          (case match ast lhs of
1127
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   208
            Some (env, args) =>
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   209
              (inc changes; Some (mk_appl (subst env rhs) args))
11733
9dd88f3aa8e0 removed lookups count;
wenzelm
parents: 10913
diff changeset
   210
          | None => (inc failed_matches; try_rules pats ast))
9dd88f3aa8e0 removed lookups count;
wenzelm
parents: 10913
diff changeset
   211
      | try_rules [] _ = None;
9dd88f3aa8e0 removed lookups count;
wenzelm
parents: 10913
diff changeset
   212
    val try_headless_rules = try_rules (get_rules "");
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   213
11733
9dd88f3aa8e0 removed lookups count;
wenzelm
parents: 10913
diff changeset
   214
    fun try ast a =
9dd88f3aa8e0 removed lookups count;
wenzelm
parents: 10913
diff changeset
   215
      (case try_rules (get_rules a) ast of
9dd88f3aa8e0 removed lookups count;
wenzelm
parents: 10913
diff changeset
   216
        None => try_headless_rules ast
9dd88f3aa8e0 removed lookups count;
wenzelm
parents: 10913
diff changeset
   217
      | some => some);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   218
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   219
    fun rewrite (ast as Constant a) = try ast a
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   220
      | rewrite (ast as Variable a) = try ast a
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   221
      | rewrite (ast as Appl (Constant a :: _)) = try ast a
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   222
      | rewrite (ast as Appl (Variable a :: _)) = try ast a
11733
9dd88f3aa8e0 removed lookups count;
wenzelm
parents: 10913
diff changeset
   223
      | rewrite ast = try_headless_rules ast;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   224
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   225
    fun rewrote old_ast new_ast =
12262
11ff5f47df6e use tracing function for trace output;
wenzelm
parents: 11733
diff changeset
   226
      conditional trace (fn () =>
11ff5f47df6e use tracing function for trace output;
wenzelm
parents: 11733
diff changeset
   227
        tracing ("rewrote: " ^ str_of_ast old_ast ^ "  ->  " ^ str_of_ast new_ast));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   228
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   229
    fun norm_root ast =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   230
      (case rewrite ast of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   231
        Some new_ast => (rewrote ast new_ast; norm_root new_ast)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   232
      | None => ast);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   233
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   234
    fun norm ast =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   235
      (case norm_root ast of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   236
        Appl sub_asts =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   237
          let
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   238
            val old_changes = ! changes;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   239
            val new_ast = Appl (map norm sub_asts);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   240
          in
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   241
            if old_changes = ! changes then new_ast else norm_root new_ast
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   242
          end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   243
      | atomic_ast => atomic_ast);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   244
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   245
    fun normal ast =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   246
      let
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   247
        val old_changes = ! changes;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   248
        val new_ast = norm ast;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   249
      in
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   250
        inc passes;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   251
        if old_changes = ! changes then new_ast else normal new_ast
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   252
      end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   253
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   254
12262
11ff5f47df6e use tracing function for trace output;
wenzelm
parents: 11733
diff changeset
   255
    val _ = conditional trace (fn () => tracing ("pre: " ^ str_of_ast pre_ast));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   256
9372
7834e56e2277 AST translation rules no longer require constant head on LHS;
wenzelm
parents: 8997
diff changeset
   257
    val post_ast = normal pre_ast;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   258
  in
12262
11ff5f47df6e use tracing function for trace output;
wenzelm
parents: 11733
diff changeset
   259
    conditional (trace orelse stat) (fn () =>
11ff5f47df6e use tracing function for trace output;
wenzelm
parents: 11733
diff changeset
   260
      tracing ("post: " ^ str_of_ast post_ast ^ "\nnormalize: " ^
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   261
        string_of_int (! passes) ^ " passes, " ^
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   262
        string_of_int (! changes) ^ " changes, " ^
12262
11ff5f47df6e use tracing function for trace output;
wenzelm
parents: 11733
diff changeset
   263
        string_of_int (! failed_matches) ^ " matches failed"));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   264
    post_ast
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   265
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   266
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   267
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   268
(* normalize_ast *)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   269
12262
11ff5f47df6e use tracing function for trace output;
wenzelm
parents: 11733
diff changeset
   270
val trace_ast = ref false;
11ff5f47df6e use tracing function for trace output;
wenzelm
parents: 11733
diff changeset
   271
val stat_ast = ref false;
11ff5f47df6e use tracing function for trace output;
wenzelm
parents: 11733
diff changeset
   272
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   273
fun normalize_ast get_rules ast =
8997
da290d99d8b2 renamed trace/stat_norm_ast to trace/stat_ast;
wenzelm
parents: 5689
diff changeset
   274
  normalize (! trace_ast) (! stat_ast) get_rules ast;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   275
1506
192c48376d25 Elimination of fully-functorial style.
paulson
parents: 1127
diff changeset
   276
end;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   277