src/Pure/Syntax/printer.ML
changeset 81197 794b10baf0de
parent 81194 0e27325da568
child 81199 785c0241d7b8
equal deleted inserted replaced
81196:eb397024b496 81197:794b10baf0de
    32   val get_infix: prtabs -> string -> {assoc: assoc, delim: string, pri: int} option
    32   val get_infix: prtabs -> string -> {assoc: assoc, delim: string, pri: int} option
    33   val empty_prtabs: prtabs
    33   val empty_prtabs: prtabs
    34   val update_prtabs: string -> Syntax_Ext.xprod list -> prtabs -> prtabs
    34   val update_prtabs: string -> Syntax_Ext.xprod list -> prtabs -> prtabs
    35   val remove_prtabs: string -> Syntax_Ext.xprod list -> prtabs -> prtabs
    35   val remove_prtabs: string -> Syntax_Ext.xprod list -> prtabs -> prtabs
    36   val merge_prtabs: prtabs * prtabs -> prtabs
    36   val merge_prtabs: prtabs * prtabs -> prtabs
       
    37   type pretty_ops =
       
    38    {trf: string -> Proof.context -> Ast.ast list -> Ast.ast,
       
    39     constrain_output: Ast.ast -> Pretty.T -> Pretty.T,
       
    40     markup_trans: string -> Ast.ast list -> Pretty.T option,
       
    41     markup: string -> Markup.T list,
       
    42     extern: string -> xstring}
    37   val pretty: {type_mode: bool, curried: bool} -> Proof.context -> prtab list ->
    43   val pretty: {type_mode: bool, curried: bool} -> Proof.context -> prtab list ->
    38     (string -> Proof.context -> Ast.ast list -> Ast.ast) ->
    44     pretty_ops -> Ast.ast -> Pretty.T list
    39     (Ast.ast -> Pretty.T -> Pretty.T) ->
       
    40     (string -> Ast.ast list -> Pretty.T option) ->
       
    41     ((string -> Markup.T list) * (string -> string)) ->
       
    42     Ast.ast -> Pretty.T list
       
    43   val type_mode_flags: {type_mode: bool, curried: bool}
    45   val type_mode_flags: {type_mode: bool, curried: bool}
    44 end;
    46 end;
    45 
    47 
    46 structure Printer: PRINTER =
    48 structure Printer: PRINTER =
    47 struct
    49 struct
   221 
   223 
   222 in
   224 in
   223 
   225 
   224 val type_mode_flags = {type_mode = true, curried = false};
   226 val type_mode_flags = {type_mode = true, curried = false};
   225 
   227 
   226 fun pretty {type_mode, curried} ctxt prtabs trf constrain_output markup_trans (markup, extern) =
   228 type pretty_ops =
       
   229  {trf: string -> Proof.context -> Ast.ast list -> Ast.ast,
       
   230   constrain_output: Ast.ast -> Pretty.T -> Pretty.T,
       
   231   markup_trans: string -> Ast.ast list -> Pretty.T option,
       
   232   markup: string -> Markup.T list,
       
   233   extern: string -> xstring};
       
   234 
       
   235 fun pretty {type_mode, curried} ctxt prtabs (ops: pretty_ops) =
   227   let
   236   let
   228     val show_brackets = Config.get ctxt show_brackets;
   237     val show_brackets = Config.get ctxt show_brackets;
   229 
   238 
   230     val application =
   239     val application =
   231       if type_mode then Syntax_Trans.tappl_ast_tr'
   240       if type_mode then Syntax_Trans.tappl_ast_tr'
   232       else if curried then Syntax_Trans.applC_ast_tr'
   241       else if curried then Syntax_Trans.applC_ast_tr'
   233       else Syntax_Trans.appl_ast_tr';
   242       else Syntax_Trans.appl_ast_tr';
   234 
   243 
   235     fun constrain_trans (Ast.Appl [Ast.Constant (a as "_constrain"), ast, ty]) =
   244     fun constrain_trans (Ast.Appl [Ast.Constant (a as "_constrain"), ast, ty]) =
   236           markup_trans a [ast, ty]
   245           #markup_trans ops a [ast, ty]
   237       | constrain_trans _ = NONE;
   246       | constrain_trans _ = NONE;
   238 
   247 
   239     fun main _ (Ast.Variable x) = [Ast.pretty_var x]
   248     fun main _ (Ast.Variable x) = [Ast.pretty_var x]
   240       | main p (cc as Ast.Appl [Ast.Constant "_constrain", c as Ast.Constant a, _]) =
   249       | main p (cc as Ast.Appl [Ast.Constant "_constrain", c as Ast.Constant a, _]) =
   241           combination p c a [] (SOME cc)
   250           combination p c a [] (SOME cc)
   250       | main p (Ast.Appl (f :: (args as _ :: _))) = main p (application (f, args))
   259       | main p (Ast.Appl (f :: (args as _ :: _))) = main p (application (f, args))
   251       | main _ (ast as Ast.Appl _) = raise Ast.AST ("pretty: malformed ast", [ast])
   260       | main _ (ast as Ast.Appl _) = raise Ast.AST ("pretty: malformed ast", [ast])
   252 
   261 
   253     and main_type p ast =
   262     and main_type p ast =
   254       if type_mode then main p ast
   263       if type_mode then main p ast
   255       else
   264       else pretty type_mode_flags (Config.put pretty_priority p ctxt) prtabs ops ast
   256         pretty type_mode_flags (Config.put pretty_priority p ctxt)
       
   257           prtabs trf constrain_output markup_trans (markup, extern) ast
       
   258 
   265 
   259     and combination p c a args constraint =
   266     and combination p c a args constraint =
   260       (case translation p a args of
   267       (case translation p a args of
   261         SOME prts => prts
   268         SOME prts => prts
   262       | NONE =>
   269       | NONE =>
   272             (case entry of
   279             (case entry of
   273               NONE =>
   280               NONE =>
   274                 if nargs = 0 then
   281                 if nargs = 0 then
   275                   (case Option.mapPartial constrain_trans constraint of
   282                   (case Option.mapPartial constrain_trans constraint of
   276                     SOME prt => [prt]
   283                     SOME prt => [prt]
   277                   | NONE => [Pretty.marks_str (markup a, extern a)])
   284                   | NONE => [Pretty.marks_str (#markup ops a, #extern ops a)])
   278                 else main p (application (cc, args))
   285                 else main p (application (cc, args))
   279             | SOME (symbs, n, q) =>
   286             | SOME (symbs, n, q) =>
   280                 if nargs = n then parens p q a (symbs, args) constraint
   287                 if nargs = n then parens p q a (symbs, args) constraint
   281                 else main p (application (split_args n [cc] args)))
   288                 else main p (application (split_args n [cc] args)))
   282           end)
   289           end)
   283 
   290 
   284     and translation p a args =
   291     and translation p a args =
   285       (case markup_trans a args of
   292       (case #markup_trans ops a args of
   286         SOME prt => SOME [prt]
   293         SOME prt => SOME [prt]
   287       | NONE => Option.map (main p) (SOME (trf a ctxt args) handle Match => NONE))
   294       | NONE => Option.map (main p) (SOME (#trf ops a ctxt args) handle Match => NONE))
   288 
   295 
   289     and parens p q a (symbs, args) constraint =
   296     and parens p q a (symbs, args) constraint =
   290       let
   297       let
   291         val symbs' =
   298         val symbs' =
   292           if p > q orelse (show_brackets andalso q <> 1000 andalso not (is_chain symbs))
   299           if p > q orelse (show_brackets andalso q <> 1000 andalso not (is_chain symbs))
   293           then [Block (par_block, par_bg :: symbs @ [par_en])] else symbs;
   300           then [Block (par_block, par_bg :: symbs @ [par_en])] else symbs;
   294         val output =
   301         val output =
   295           (case constraint of
   302           (case constraint of
   296             SOME (Ast.Appl [Ast.Constant "_constrain", _, ty]) => constrain_output ty
   303             SOME (Ast.Appl [Ast.Constant "_constrain", _, ty]) => #constrain_output ops ty
   297           | _ => I);
   304           | _ => I);
   298       in #1 (syntax (markup a, output) (symbs', args)) end
   305       in #1 (syntax (#markup ops a, output) (symbs', args)) end
   299 
   306 
   300     and syntax _ ([], args) = ([], args)
   307     and syntax _ ([], args) = ([], args)
   301       | syntax m (Arg p :: symbs, arg :: args) =
   308       | syntax m (Arg p :: symbs, arg :: args) =
   302           let val (prts, args') = syntax m (symbs, args);
   309           let val (prts, args') = syntax m (symbs, args);
   303           in (main p arg @ prts, args') end
   310           in (main p arg @ prts, args') end