src/Pure/Syntax/ast.ML
author wenzelm
Sat, 12 Oct 2024 14:29:39 +0200
changeset 81153 ee8c3375cd39
parent 81152 ece9fe9bf440
child 81156 cf750881f1fe
permissions -rw-r--r--
clarified signature;
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
    Author:     Markus Wenzel, TU Muenchen
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     3
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
     4
Abstract syntax trees, translation rules, matching and normalization of asts.
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
42224
578a51fae383 discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents: 42048
diff changeset
     7
signature AST =
16609
c787112bba1f removed obsolete (un)fold_ast2;
wenzelm
parents: 15973
diff changeset
     8
sig
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     9
  datatype ast =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    10
    Constant of string |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    11
    Variable of string |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    12
    Appl of ast list
42224
578a51fae383 discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents: 42048
diff changeset
    13
  val mk_appl: ast -> ast list -> ast
258
e540b7d4ecb1 minor internal changes;
wenzelm
parents: 235
diff changeset
    14
  exception AST of string * ast list
80881
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    15
  val ast_ord: ast ord
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    16
  structure Table: TABLE
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    17
  structure Set: SET
81153
ee8c3375cd39 clarified signature;
wenzelm
parents: 81152
diff changeset
    18
  val pretty_var: string -> Pretty.T
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    19
  val pretty_ast: ast -> Pretty.T
258
e540b7d4ecb1 minor internal changes;
wenzelm
parents: 235
diff changeset
    20
  val pretty_rule: ast * ast -> Pretty.T
42264
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents: 42224
diff changeset
    21
  val strip_positions: ast -> ast
81152
ece9fe9bf440 clarified signature;
wenzelm
parents: 81151
diff changeset
    22
  val rule_index: ast * ast -> string
42224
578a51fae383 discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents: 42048
diff changeset
    23
  val rule_error: ast * ast -> string option
10913
57eb8c1d6f88 export fold_ast etc.;
wenzelm
parents: 9372
diff changeset
    24
  val fold_ast: string -> ast list -> ast
57eb8c1d6f88 export fold_ast etc.;
wenzelm
parents: 9372
diff changeset
    25
  val fold_ast_p: string -> ast list * ast -> ast
57eb8c1d6f88 export fold_ast etc.;
wenzelm
parents: 9372
diff changeset
    26
  val unfold_ast: string -> ast -> ast list
57eb8c1d6f88 export fold_ast etc.;
wenzelm
parents: 9372
diff changeset
    27
  val unfold_ast_p: string -> ast -> ast list * ast
42277
7503beeffd8d tuned signature;
wenzelm
parents: 42264
diff changeset
    28
  val trace: bool Config.T
43347
f18cf88453d6 tuned name (cf. blast_stats);
wenzelm
parents: 42277
diff changeset
    29
  val stats: bool Config.T
41377
390c53904220 configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents: 38328
diff changeset
    30
  val normalize: Proof.context -> (string -> (ast * ast) list) -> ast -> ast
16609
c787112bba1f removed obsolete (un)fold_ast2;
wenzelm
parents: 15973
diff changeset
    31
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    32
42224
578a51fae383 discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents: 42048
diff changeset
    33
structure Ast: AST =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    34
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    35
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    36
(** abstract syntax trees **)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    37
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    38
(*asts come in two flavours:
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    39
   - ordinary asts representing terms and typs: Variables are (often) treated
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    40
     like Constants;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    41
   - patterns used as lhs and rhs in rules: Variables are placeholders for
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    42
     proper asts*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    43
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    44
datatype ast =
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    45
  Constant of string |    (*"not", "_abs", "fun"*)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    46
  Variable of string |    (*x, ?x, 'a, ?'a*)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    47
  Appl of ast list;       (*(f x y z), ("fun" 'a 'b), ("_abs" x t)*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    48
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    49
(*the list of subasts of an Appl node has to contain at least 2 elements, i.e.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    50
  there are no empty asts or nullary applications; use mk_appl for convenience*)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    51
fun mk_appl f [] = f
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    52
  | mk_appl f args = Appl (f :: args);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    53
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    54
(*exception for system errors involving asts*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    55
exception AST of string * ast list;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    56
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    57
80881
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    58
(* order *)
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    59
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    60
local
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    61
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    62
fun cons_nr (Constant _) = 0
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    63
  | cons_nr (Variable _) = 1
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    64
  | cons_nr (Appl _) = 2;
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    65
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    66
in
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    67
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    68
fun ast_ord asts =
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    69
  if pointer_eq asts then EQUAL
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    70
  else
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    71
    (case asts of
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    72
      (Constant a, Constant b) => fast_string_ord (a, b)
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    73
    | (Variable a, Variable b) => fast_string_ord (a, b)
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    74
    | (Appl a, Appl b) => list_ord ast_ord (a, b)
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    75
    | _ => int_ord (apply2 cons_nr asts));
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    76
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    77
end;
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    78
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    79
structure Table = Table(type key = ast val ord = ast_ord);
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    80
structure Set = Set(Table.Key);
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    81
94ea065a8881 more operations;
wenzelm
parents: 80809
diff changeset
    82
81152
ece9fe9bf440 clarified signature;
wenzelm
parents: 81151
diff changeset
    83
(* print asts in a LISP-like style *)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    84
81153
ee8c3375cd39 clarified signature;
wenzelm
parents: 81152
diff changeset
    85
fun pretty_var x =
ee8c3375cd39 clarified signature;
wenzelm
parents: 81152
diff changeset
    86
  (case Term_Position.decode x of
ee8c3375cd39 clarified signature;
wenzelm
parents: 81152
diff changeset
    87
    SOME pos => Term_Position.pretty pos
ee8c3375cd39 clarified signature;
wenzelm
parents: 81152
diff changeset
    88
  | NONE => Pretty.str x);
ee8c3375cd39 clarified signature;
wenzelm
parents: 81152
diff changeset
    89
14599
c3177fffd31a Replaced quote by Pretty.quote.
berghofe
parents: 12785
diff changeset
    90
fun pretty_ast (Constant a) = Pretty.quote (Pretty.str a)
81153
ee8c3375cd39 clarified signature;
wenzelm
parents: 81152
diff changeset
    91
  | pretty_ast (Variable x) = pretty_var x
38328
36afb56ec49e tuned whitespace;
wenzelm
parents: 33957
diff changeset
    92
  | pretty_ast (Appl asts) = Pretty.enclose "(" ")" (Pretty.breaks (map pretty_ast asts));
18
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
fun pretty_rule (lhs, rhs) =
67519
6905b156a030 tuned output;
wenzelm
parents: 64556
diff changeset
    95
  Pretty.block [pretty_ast lhs, Pretty.str " \<leadsto>", Pretty.brk 1, pretty_ast rhs];
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    96
80809
4a64fc4d1cde clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents: 74232
diff changeset
    97
val _ = ML_system_pp (fn _ => fn _ => Pretty.to_ML o pretty_ast);
62663
bea354f6ff21 clarified modules;
wenzelm
parents: 56438
diff changeset
    98
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    99
42264
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents: 42224
diff changeset
   100
(* strip_positions *)
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents: 42224
diff changeset
   101
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents: 42224
diff changeset
   102
fun strip_positions (Appl ((t as Constant c) :: u :: (v as Variable x) :: asts)) =
52175
626a757d3c2d uniform Term_Position.markers (cf. dbadb4d03cbc);
wenzelm
parents: 43347
diff changeset
   103
      if member (op =) Term_Position.markers c andalso is_some (Term_Position.decode x)
42264
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents: 42224
diff changeset
   104
      then mk_appl (strip_positions u) (map strip_positions asts)
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents: 42224
diff changeset
   105
      else Appl (map strip_positions (t :: u :: v :: asts))
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents: 42224
diff changeset
   106
  | strip_positions (Appl asts) = Appl (map strip_positions asts)
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents: 42224
diff changeset
   107
  | strip_positions ast = ast;
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents: 42224
diff changeset
   108
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents: 42224
diff changeset
   109
81152
ece9fe9bf440 clarified signature;
wenzelm
parents: 81151
diff changeset
   110
(* translation rules *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   111
81152
ece9fe9bf440 clarified signature;
wenzelm
parents: 81151
diff changeset
   112
fun rule_index (Constant a, _: ast) = a
ece9fe9bf440 clarified signature;
wenzelm
parents: 81151
diff changeset
   113
  | rule_index (Appl (Constant a :: _), _) = a
ece9fe9bf440 clarified signature;
wenzelm
parents: 81151
diff changeset
   114
  | rule_index _ = "";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   115
32784
1a5dde5079ac eliminated redundant bindings;
wenzelm
parents: 32738
diff changeset
   116
fun rule_error (lhs, rhs) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   117
  let
19486
wenzelm
parents: 19473
diff changeset
   118
    fun add_vars (Constant _) = I
wenzelm
parents: 19473
diff changeset
   119
      | add_vars (Variable x) = cons x
wenzelm
parents: 19473
diff changeset
   120
      | add_vars (Appl asts) = fold add_vars asts;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   121
19486
wenzelm
parents: 19473
diff changeset
   122
    val lvars = add_vars lhs [];
wenzelm
parents: 19473
diff changeset
   123
    val rvars = add_vars rhs [];
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   124
  in
19486
wenzelm
parents: 19473
diff changeset
   125
    if has_duplicates (op =) lvars then SOME "duplicate vars in lhs"
33038
8f9594c31de4 dropped redundant gen_ prefix
haftmann
parents: 33037
diff changeset
   126
    else if not (subset (op =) (rvars, lvars)) then SOME "rhs contains extra variables"
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15421
diff changeset
   127
    else NONE
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   128
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   129
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   130
81152
ece9fe9bf440 clarified signature;
wenzelm
parents: 81151
diff changeset
   131
(* ast translation utilities *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   132
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   133
fun fold_ast _ [] = raise Match
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   134
  | fold_ast _ [y] = y
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   135
  | fold_ast c (x :: xs) = Appl [Constant c, x, fold_ast c xs];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   136
19473
wenzelm
parents: 17412
diff changeset
   137
fun fold_ast_p c = uncurry (fold_rev (fn x => fn xs => Appl [Constant c, x, xs]));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   138
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   139
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   140
fun unfold_ast c (y as Appl [Constant c', x, xs]) =
16609
c787112bba1f removed obsolete (un)fold_ast2;
wenzelm
parents: 15973
diff changeset
   141
      if c = c' then x :: unfold_ast c xs else [y]
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   142
  | unfold_ast _ y = [y];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   143
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   144
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
   145
      if c = c' then apfst (cons x) (unfold_ast_p c xs)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   146
      else ([], y)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   147
  | unfold_ast_p _ y = ([], y);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   148
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   149
16609
c787112bba1f removed obsolete (un)fold_ast2;
wenzelm
parents: 15973
diff changeset
   150
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   151
(** normalization of asts **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   152
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   153
(* match *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   154
81151
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   155
local exception NO_MATCH in
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   156
81151
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   157
fun match obj pat =
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   158
  let
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   159
    fun match1 (Constant a) (Constant b) env = if a = b then env else raise NO_MATCH
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   160
      | match1 (Variable a) (Constant b) env = if a = b then env else raise NO_MATCH
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   161
      | match1 ast (Variable x) env = Symtab.update (x, ast) env
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   162
      | match1 (Appl asts) (Appl pats) env = match2 asts pats env
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   163
      | match1 _ _ _ = raise NO_MATCH
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   164
    and match2 (ast :: asts) (pat :: pats) env = match1 ast pat env |> match2 asts pats
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   165
      | match2 [] [] env = env
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   166
      | match2 _ _ _ = raise NO_MATCH;
1127
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   167
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   168
    val (head, args) =
81151
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   169
      (case (obj, pat) of
1127
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   170
        (Appl asts, Appl pats) =>
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   171
          let val a = length asts and p = length pats in
33957
e9afca2118d4 normalized uncurry take/drop
haftmann
parents: 33955
diff changeset
   172
            if a > p then (Appl (take p asts), drop p asts)
81151
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   173
            else (obj, [])
1127
42ec82147d83 changed macro expander such that patterns also match prefixes of appls;
wenzelm
parents: 922
diff changeset
   174
          end
81151
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   175
      | _ => (obj, []));
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   176
  in SOME (Symtab.build (match1 head pat), args) handle NO_MATCH => NONE end;
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   177
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   178
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   179
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   180
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   181
(* normalize *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   182
69575
f77cc54f6d47 clarified signature: more types;
wenzelm
parents: 67519
diff changeset
   183
val trace = Config.declare_bool ("syntax_ast_trace", \<^here>) (K false);
f77cc54f6d47 clarified signature: more types;
wenzelm
parents: 67519
diff changeset
   184
val stats = Config.declare_bool ("syntax_ast_stats", \<^here>) (K false);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   185
81151
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   186
local
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   187
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   188
fun subst _ (ast as Constant _) = ast
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   189
  | subst env (Variable x) = the (Symtab.lookup env x)
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   190
  | subst env (Appl asts) = Appl (map (subst env) asts);
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   191
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   192
fun head_name (Constant a) = SOME a
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   193
  | head_name (Variable a) = SOME a
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   194
  | head_name (Appl (Constant a :: _)) = SOME a
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   195
  | head_name (Appl (Variable a :: _)) = SOME a
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   196
  | head_name _ = NONE;
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   197
42048
afd11ca8e018 support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents: 41377
diff changeset
   198
fun message head body =
afd11ca8e018 support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents: 41377
diff changeset
   199
  Pretty.string_of (Pretty.block [Pretty.str head, Pretty.brk 1, body]);
afd11ca8e018 support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents: 41377
diff changeset
   200
81151
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   201
in
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   202
41377
390c53904220 configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents: 38328
diff changeset
   203
(*the normalizer works yoyo-like: top-down, bottom-up, top-down, ...*)
390c53904220 configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents: 38328
diff changeset
   204
fun normalize ctxt get_rules pre_ast =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   205
  let
42277
7503beeffd8d tuned signature;
wenzelm
parents: 42264
diff changeset
   206
    val trace = Config.get ctxt trace;
43347
f18cf88453d6 tuned name (cf. blast_stats);
wenzelm
parents: 42277
diff changeset
   207
    val stats = Config.get ctxt stats;
41377
390c53904220 configuration option "syntax_ast_trace" and "syntax_ast_stat";
wenzelm
parents: 38328
diff changeset
   208
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 30628
diff changeset
   209
    val passes = Unsynchronized.ref 0;
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 30628
diff changeset
   210
    val failed_matches = Unsynchronized.ref 0;
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 30628
diff changeset
   211
    val changes = Unsynchronized.ref 0;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   212
81151
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   213
    fun rewrite1 ((lhs, rhs) :: pats) ast =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   214
          (case match ast lhs of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15421
diff changeset
   215
            SOME (env, args) =>
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 30628
diff changeset
   216
              (Unsynchronized.inc changes; SOME (mk_appl (subst env rhs) args))
81151
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   217
          | NONE => (Unsynchronized.inc failed_matches; rewrite1 pats ast))
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   218
      | rewrite1 [] _ = NONE;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   219
81151
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   220
    fun rewrite2 (SOME a) ast =
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   221
          (case rewrite1 (get_rules a) ast of
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   222
            NONE => rewrite2 NONE ast
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   223
          | some => some)
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   224
      | rewrite2 NONE ast = rewrite1 (get_rules "") ast;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   225
81151
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   226
    fun rewrite ast = rewrite2 (head_name ast) ast;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   227
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   228
    fun rewrote old_ast new_ast =
42048
afd11ca8e018 support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents: 41377
diff changeset
   229
      if trace then tracing (message "rewrote:" (pretty_rule (old_ast, new_ast)))
21962
279b129498b6 removed conditional combinator;
wenzelm
parents: 19486
diff changeset
   230
      else ();
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   231
81151
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   232
    fun norm1 ast =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   233
      (case rewrite ast of
81151
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   234
        SOME new_ast => (rewrote ast new_ast; norm1 new_ast)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15421
diff changeset
   235
      | NONE => ast);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   236
81151
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   237
    fun norm2 ast =
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   238
      (case norm1 ast of
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   239
        Appl sub_asts =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   240
          let
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   241
            val old_changes = ! changes;
81151
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   242
            val new_ast = Appl (map norm2 sub_asts);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   243
          in
81151
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   244
            if old_changes = ! changes then new_ast else norm1 new_ast
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   245
          end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   246
      | atomic_ast => atomic_ast);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   247
81151
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   248
    fun norm ast =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   249
      let
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   250
        val old_changes = ! changes;
81151
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   251
        val new_ast = norm2 ast;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   252
      in
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 30628
diff changeset
   253
        Unsynchronized.inc passes;
81151
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   254
        if old_changes = ! changes then new_ast else norm new_ast
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   255
      end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   256
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   257
42048
afd11ca8e018 support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents: 41377
diff changeset
   258
    val _ = if trace then tracing (message "pre:" (pretty_ast pre_ast)) else ();
81151
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   259
    val post_ast = norm pre_ast;
21962
279b129498b6 removed conditional combinator;
wenzelm
parents: 19486
diff changeset
   260
    val _ =
43347
f18cf88453d6 tuned name (cf. blast_stats);
wenzelm
parents: 42277
diff changeset
   261
      if trace orelse stats then
42048
afd11ca8e018 support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents: 41377
diff changeset
   262
        tracing (message "post:" (pretty_ast post_ast) ^ "\nnormalize: " ^
21962
279b129498b6 removed conditional combinator;
wenzelm
parents: 19486
diff changeset
   263
          string_of_int (! passes) ^ " passes, " ^
279b129498b6 removed conditional combinator;
wenzelm
parents: 19486
diff changeset
   264
          string_of_int (! changes) ^ " changes, " ^
279b129498b6 removed conditional combinator;
wenzelm
parents: 19486
diff changeset
   265
          string_of_int (! failed_matches) ^ " matches failed")
279b129498b6 removed conditional combinator;
wenzelm
parents: 19486
diff changeset
   266
      else ();
279b129498b6 removed conditional combinator;
wenzelm
parents: 19486
diff changeset
   267
  in post_ast end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   268
1506
192c48376d25 Elimination of fully-functorial style.
paulson
parents: 1127
diff changeset
   269
end;
81151
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   270
0d728eadad86 misc tuning and clarification;
wenzelm
parents: 80881
diff changeset
   271
end;