src/Pure/Syntax/ast.ML
changeset 16609 c787112bba1f
parent 15973 5fd94d84470f
child 17221 6cd180204582
equal deleted inserted replaced
16608:4f8d7b83c7e2 16609:c787112bba1f
     4 
     4 
     5 Abstract syntax trees, translation rules, matching and normalization of asts.
     5 Abstract syntax trees, translation rules, matching and normalization of asts.
     6 *)
     6 *)
     7 
     7 
     8 signature AST0 =
     8 signature AST0 =
     9   sig
     9 sig
    10   datatype ast =
    10   datatype ast =
    11     Constant of string |
    11     Constant of string |
    12     Variable of string |
    12     Variable of string |
    13     Appl of ast list
    13     Appl of ast list
    14   exception AST of string * ast list
    14   exception AST of string * ast list
    15   end;
    15 end;
    16 
    16 
    17 signature AST1 =
    17 signature AST1 =
    18   sig
    18 sig
    19   include AST0
    19   include AST0
    20   val mk_appl: ast -> ast list -> ast
    20   val mk_appl: ast -> ast list -> ast
    21   val str_of_ast: ast -> string
    21   val str_of_ast: ast -> string
    22   val pretty_ast: ast -> Pretty.T
    22   val pretty_ast: ast -> Pretty.T
    23   val pretty_rule: ast * ast -> Pretty.T
    23   val pretty_rule: ast * ast -> Pretty.T
    24   val pprint_ast: ast -> pprint_args -> unit
    24   val pprint_ast: ast -> pprint_args -> unit
    25   val fold_ast: string -> ast list -> ast
    25   val fold_ast: string -> ast list -> ast
    26   val fold_ast2: string -> string -> ast list -> ast
       
    27   val fold_ast_p: string -> ast list * ast -> ast
    26   val fold_ast_p: string -> ast list * ast -> ast
    28   val unfold_ast: string -> ast -> ast list
    27   val unfold_ast: string -> ast -> ast list
    29   val unfold_ast2: string -> string -> ast -> ast list
       
    30   val unfold_ast_p: string -> ast -> ast list * ast
    28   val unfold_ast_p: string -> ast -> ast list * ast
    31   val trace_ast: bool ref
    29   val trace_ast: bool ref
    32   val stat_ast: bool ref
    30   val stat_ast: bool ref
    33   end;
    31 end;
    34 
    32 
    35 signature AST =
    33 signature AST =
    36   sig
    34 sig
    37   include AST1
    35   include AST1
    38   val head_of_rule: ast * ast -> string
    36   val head_of_rule: ast * ast -> string
    39   val rule_error: ast * ast -> string option
    37   val rule_error: ast * ast -> string option
    40   val normalize: bool -> bool -> (string -> (ast * ast) list) -> ast -> ast
    38   val normalize: bool -> bool -> (string -> (ast * ast) list) -> ast -> ast
    41   val normalize_ast: (string -> (ast * ast) list) -> ast -> ast
    39   val normalize_ast: (string -> (ast * ast) list) -> ast -> ast
    42   end;
    40 end;
    43 
    41 
    44 structure Ast : AST =
    42 structure Ast : AST =
    45 struct
    43 struct
    46 
    44 
    47 (** abstract syntax trees **)
    45 (** abstract syntax trees **)
    71 
    69 
    72 
    70 
    73 
    71 
    74 (** print asts in a LISP-like style **)
    72 (** print asts in a LISP-like style **)
    75 
    73 
    76 (* str_of_ast *)
       
    77 
       
    78 fun str_of_ast (Constant a) = quote a
    74 fun str_of_ast (Constant a) = quote a
    79   | str_of_ast (Variable x) = x
    75   | str_of_ast (Variable x) = x
    80   | str_of_ast (Appl asts) = "(" ^ (space_implode " " (map str_of_ast asts)) ^ ")";
    76   | str_of_ast (Appl asts) = "(" ^ (space_implode " " (map str_of_ast asts)) ^ ")";
    81 
       
    82 
       
    83 (* pretty_ast *)
       
    84 
    77 
    85 fun pretty_ast (Constant a) = Pretty.quote (Pretty.str a)
    78 fun pretty_ast (Constant a) = Pretty.quote (Pretty.str a)
    86   | pretty_ast (Variable x) = Pretty.str x
    79   | pretty_ast (Variable x) = Pretty.str x
    87   | pretty_ast (Appl asts) =
    80   | pretty_ast (Appl asts) =
    88       Pretty.enclose "(" ")" (Pretty.breaks (map pretty_ast asts));
    81       Pretty.enclose "(" ")" (Pretty.breaks (map pretty_ast asts));
    89 
    82 
    90 
       
    91 (* pprint_ast *)
       
    92 
       
    93 val pprint_ast = Pretty.pprint o pretty_ast;
    83 val pprint_ast = Pretty.pprint o pretty_ast;
    94 
       
    95 
       
    96 (* pretty_rule *)
       
    97 
    84 
    98 fun pretty_rule (lhs, rhs) =
    85 fun pretty_rule (lhs, rhs) =
    99   Pretty.block [pretty_ast lhs, Pretty.str "  ->", Pretty.brk 2, pretty_ast rhs];
    86   Pretty.block [pretty_ast lhs, Pretty.str "  ->", Pretty.brk 2, pretty_ast rhs];
   100 
    87 
   101 
    88 
   142   | fold_ast _ [y] = y
   129   | fold_ast _ [y] = y
   143   | fold_ast c (x :: xs) = Appl [Constant c, x, fold_ast c xs];
   130   | fold_ast c (x :: xs) = Appl [Constant c, x, fold_ast c xs];
   144 
   131 
   145 fun fold_ast_p c = Library.foldr (fn (x, xs) => Appl [Constant c, x, xs]);
   132 fun fold_ast_p c = Library.foldr (fn (x, xs) => Appl [Constant c, x, xs]);
   146 
   133 
   147 fun fold_ast2 _ _ [] = raise Match
       
   148   | fold_ast2 _ c1 [y] = Appl [Constant c1, y]
       
   149   | fold_ast2 c c1 (x :: xs) = Appl [Constant c, x, fold_ast2 c c1 xs];
       
   150 
   134 
   151 (* unfold asts *)
   135 (* unfold asts *)
   152 
   136 
   153 fun unfold_ast c (y as Appl [Constant c', x, xs]) =
   137 fun unfold_ast c (y as Appl [Constant c', x, xs]) =
   154       if c = c' then x :: (unfold_ast c xs) else [y]
   138       if c = c' then x :: unfold_ast c xs else [y]
   155   | unfold_ast _ y = [y];
   139   | unfold_ast _ y = [y];
   156 
       
   157 fun unfold_ast2 c c1 (y as Appl [Constant c', x, xs]) =
       
   158       if c = c' then x :: (unfold_ast2 c c1 xs) else [y]
       
   159   | unfold_ast2 _ c1 (y as Appl [Constant c', x]) =
       
   160       if c1 = c' then [x] else [y]
       
   161   | unfold_ast2 _ _ y = [y];
       
   162 
   140 
   163 fun unfold_ast_p c (y as Appl [Constant c', x, xs]) =
   141 fun unfold_ast_p c (y as Appl [Constant c', x, xs]) =
   164       if c = c' then apfst (cons x) (unfold_ast_p c xs)
   142       if c = c' then apfst (cons x) (unfold_ast_p c xs)
   165       else ([], y)
   143       else ([], y)
   166   | unfold_ast_p _ y = ([], y);
   144   | unfold_ast_p _ y = ([], y);
       
   145 
   167 
   146 
   168 
   147 
   169 (** normalization of asts **)
   148 (** normalization of asts **)
   170 
   149 
   171 (* match *)
   150 (* match *)
   282 
   261 
   283 fun normalize_ast get_rules ast =
   262 fun normalize_ast get_rules ast =
   284   normalize (! trace_ast) (! stat_ast) get_rules ast;
   263   normalize (! trace_ast) (! stat_ast) get_rules ast;
   285 
   264 
   286 end;
   265 end;
   287