src/Pure/Syntax/ast.ML
changeset 42264 b6c1b0c4c511
parent 42224 578a51fae383
child 42277 7503beeffd8d
equal deleted inserted replaced
42263:49b1b8d0782f 42264:b6c1b0c4c511
    12     Appl of ast list
    12     Appl of ast list
    13   val mk_appl: ast -> ast list -> ast
    13   val mk_appl: ast -> ast list -> ast
    14   exception AST of string * ast list
    14   exception AST of string * ast list
    15   val pretty_ast: ast -> Pretty.T
    15   val pretty_ast: ast -> Pretty.T
    16   val pretty_rule: ast * ast -> Pretty.T
    16   val pretty_rule: ast * ast -> Pretty.T
       
    17   val strip_positions: ast -> ast
    17   val head_of_rule: ast * ast -> string
    18   val head_of_rule: ast * ast -> string
    18   val rule_error: ast * ast -> string option
    19   val rule_error: ast * ast -> string option
    19   val fold_ast: string -> ast list -> ast
    20   val fold_ast: string -> ast list -> ast
    20   val fold_ast_p: string -> ast list * ast -> ast
    21   val fold_ast_p: string -> ast list * ast -> ast
    21   val unfold_ast: string -> ast -> ast list
    22   val unfold_ast: string -> ast -> ast list
    55 
    56 
    56 (** print asts in a LISP-like style **)
    57 (** print asts in a LISP-like style **)
    57 
    58 
    58 fun pretty_ast (Constant a) = Pretty.quote (Pretty.str a)
    59 fun pretty_ast (Constant a) = Pretty.quote (Pretty.str a)
    59   | pretty_ast (Variable x) =
    60   | pretty_ast (Variable x) =
    60       (case Lexicon.decode_position x of
    61       (case Term_Position.decode x of
    61         SOME pos => Lexicon.pretty_position pos
    62         SOME pos => Term_Position.pretty pos
    62       | NONE => Pretty.str x)
    63       | NONE => Pretty.str x)
    63   | pretty_ast (Appl asts) = Pretty.enclose "(" ")" (Pretty.breaks (map pretty_ast asts));
    64   | pretty_ast (Appl asts) = Pretty.enclose "(" ")" (Pretty.breaks (map pretty_ast asts));
    64 
    65 
    65 fun pretty_rule (lhs, rhs) =
    66 fun pretty_rule (lhs, rhs) =
    66   Pretty.block [pretty_ast lhs, Pretty.str "  ->", Pretty.brk 2, pretty_ast rhs];
    67   Pretty.block [pretty_ast lhs, Pretty.str "  ->", Pretty.brk 2, pretty_ast rhs];
    67 
    68 
    68 
    69 
    69 (* head_of_ast, head_of_rule *)
    70 (* strip_positions *)
       
    71 
       
    72 fun strip_positions (Appl ((t as Constant c) :: u :: (v as Variable x) :: asts)) =
       
    73       if (c = "_constrain" orelse c = "_constrainAbs") andalso is_some (Term_Position.decode x)
       
    74       then mk_appl (strip_positions u) (map strip_positions asts)
       
    75       else Appl (map strip_positions (t :: u :: v :: asts))
       
    76   | strip_positions (Appl asts) = Appl (map strip_positions asts)
       
    77   | strip_positions ast = ast;
       
    78 
       
    79 
       
    80 (* head_of_ast and head_of_rule *)
    70 
    81 
    71 fun head_of_ast (Constant a) = a
    82 fun head_of_ast (Constant a) = a
    72   | head_of_ast (Appl (Constant a :: _)) = a
    83   | head_of_ast (Appl (Constant a :: _)) = a
    73   | head_of_ast _ = "";
    84   | head_of_ast _ = "";
    74 
    85