| 1 | (************************************************************************) |
| 2 | (* * The Rocq Prover / The Rocq Development Team *) |
| 3 | (* v * Copyright INRIA, CNRS and contributors *) |
| 4 | (* <O___,, * (see version control and CREDITS file for authors & dates) *) |
| 5 | (* \VV/ **************************************************************) |
| 6 | (* // * This file is distributed under the terms of the *) |
| 7 | (* * GNU Lesser General Public License Version 2.1 *) |
| 8 | (* * (see LICENSE file for the text of the license) *) |
| 9 | (************************************************************************) |
| 10 | |
| 11 | (** Rocq document type. *) |
| 12 | |
| 13 | (** |
| 14 | {4 Pretty printing guidelines} |
| 15 | |
| 16 | [Pp.t] is the main pretty printing document type |
| 17 | in the Rocq system. Documents are composed laying out boxes, and |
| 18 | users can add arbitrary tag metadata that backends are free |
| 19 | to interpret. |
| 20 | |
| 21 | The datatype has a public view to allow serialization or advanced |
| 22 | uses, however regular users are _strongly_ warned against its use, |
| 23 | they should instead rely on the available functions below. |
| 24 | |
| 25 | Box order and number is indeed an important factor. Try to create |
| 26 | a proper amount of boxes. The [++] operator provides "efficient" |
| 27 | concatenation, but using the list constructors is usually preferred. |
| 28 | |
| 29 | That is to say, this: |
| 30 | |
| 31 | [hov [str "Term"; hov (pr_term t); str "is defined"]] |
| 32 | |
| 33 | is preferred to: |
| 34 | |
| 35 | [hov (str "Term" ++ hov (pr_term t) ++ str "is defined")] |
| 36 | *) |
| 37 | |
| 38 | |
| 39 | (* XXX: Improve and add attributes *) |
| 40 | type pp_tag = string |
| 41 | |
| 42 | (* Following discussion on #390, we play on the safe side and make the |
| 43 | internal representation opaque here. *) |
| 44 | type t |
| 45 | |
| 46 | type block_type = |
| 47 | | Pp_hbox |
| 48 | | Pp_vbox of int |
| 49 | | Pp_hvbox of int |
| 50 | | Pp_hovbox of int |
| 51 | (** [Pp_hovbox] produces boxes according to [Format.open_box] not [Format.open_hovbox] *) |
| 52 | |
| 53 | type doc_view = |
| 54 | | Ppcmd_empty |
| 55 | | Ppcmd_string of string |
| 56 | | Ppcmd_sized_string of int * string |
| 57 | | Ppcmd_glue of t list |
| 58 | | Ppcmd_box of block_type * t |
| 59 | | Ppcmd_tag of pp_tag * t |
| 60 | (* Are those redundant? *) |
| 61 | | Ppcmd_print_break of int * int |
| 62 | | Ppcmd_force_newline |
| 63 | | Ppcmd_comment of string list |
| 64 | |
| 65 | val repr : t -> doc_view |
| 66 | val unrepr : doc_view -> t |
| 67 | |
| 68 | (** {6 Formatting commands} *) |
| 69 | |
| 70 | val str : string -> t |
| 71 | val brk : int * int -> t |
| 72 | val fnl : unit -> t |
| 73 | val ws : int -> t |
| 74 | val mt : unit -> t |
| 75 | val ismt : t -> bool |
| 76 | |
| 77 | val comment : string list -> t |
| 78 | |
| 79 | val fmt : ('a, unit, t) format -> 'a |
| 80 | (** complex formatting *) |
| 81 | |
| 82 | (** {6 Manipulation commands} *) |
| 83 | |
| 84 | val app : t -> t -> t |
| 85 | (** Concatenation. *) |
| 86 | |
| 87 | val seq : t list -> t |
| 88 | (** Multi-Concatenation. *) |
| 89 | |
| 90 | val (++) : t -> t -> t |
| 91 | (** Infix alias for [app]. *) |
| 92 | |
| 93 | (** {6 Derived commands} *) |
| 94 | |
| 95 | val spc : unit -> t |
| 96 | val cut : unit -> t |
| 97 | val align : unit -> t |
| 98 | val int : int -> t |
| 99 | val int64 : Int64.t -> t |
| 100 | val real : float -> t |
| 101 | val bool : bool -> t |
| 102 | val qstring : string -> t |
| 103 | val qs : string -> t |
| 104 | val quote : t -> t |
| 105 | val strbrk : string -> t |
| 106 | |
| 107 | (** {6 Boxing commands} *) |
| 108 | |
| 109 | val h : t -> t |
| 110 | val v : int -> t -> t |
| 111 | val hv : int -> t -> t |
| 112 | val hov : int -> t -> t |
| 113 | (** [hov] produces boxes according to [Format.open_box] not [Format.open_hovbox] *) |
| 114 | |
| 115 | (** {6 Tagging} *) |
| 116 | |
| 117 | val tag : pp_tag -> t -> t |
| 118 | |
| 119 | (** {6 Printing combinators} *) |
| 120 | |
| 121 | val pr_comma : unit -> t |
| 122 | (** Well-spaced comma. *) |
| 123 | |
| 124 | val pr_semicolon : unit -> t |
| 125 | (** Well-spaced semicolon. *) |
| 126 | |
| 127 | val pr_bar : unit -> t |
| 128 | (** Well-spaced pipe bar. *) |
| 129 | |
| 130 | val pr_spcbar : unit -> t |
| 131 | (** Pipe bar with space before and after. *) |
| 132 | |
| 133 | val pr_arg : ('a -> t) -> 'a -> t |
| 134 | (** Adds a space in front of its argument. *) |
| 135 | |
| 136 | val pr_non_empty_arg : ('a -> t) -> 'a -> t |
| 137 | (** Adds a space in front of its argument if non empty. *) |
| 138 | |
| 139 | val pr_opt : ('a -> t) -> 'a option -> t |
| 140 | (** Inner object preceded with a space if [Some], nothing otherwise. *) |
| 141 | |
| 142 | val pr_opt_no_spc : ('a -> t) -> 'a option -> t |
| 143 | (** Same as [pr_opt] but without the leading space. *) |
| 144 | |
| 145 | val pr_opt_default : (unit -> t) -> ('a -> t) -> 'a option -> t |
| 146 | (** Prints [pr v] if [ov] is [Some v], else [prdf ()]. *) |
| 147 | |
| 148 | val pr_opt_no_spc_default : (unit -> t) -> ('a -> t) -> 'a option -> t |
| 149 | (** Same as [pr_opt_default] but without the leading space. *) |
| 150 | |
| 151 | val pr_nth : int -> t |
| 152 | (** Ordinal number with the correct suffix (i.e. "st", "nd", "th", etc.). *) |
| 153 | |
| 154 | val prlist : ('a -> t) -> 'a list -> t |
| 155 | (** Concatenation of the list contents, without any separator. |
| 156 | |
| 157 | Unlike all other functions below, [prlist] works lazily. If a strict |
| 158 | behavior is needed, use [prlist_strict] instead. *) |
| 159 | |
| 160 | val prlist_strict : ('a -> t) -> 'a list -> t |
| 161 | (** Same as [prlist], but strict. *) |
| 162 | |
| 163 | val prlist_with_sep : |
| 164 | (unit -> t) -> ('a -> t) -> 'a list -> t |
| 165 | (** [prlist_with_sep sep pr [a ; ... ; c]] outputs |
| 166 | [pr a ++ sep () ++ ... ++ sep () ++ pr c]. |
| 167 | where the thunk sep is memoized, rather than being called each place |
| 168 | its result is used. |
| 169 | *) |
| 170 | |
| 171 | val prvect : ('a -> t) -> 'a array -> t |
| 172 | (** As [prlist], but on arrays. *) |
| 173 | |
| 174 | val prvecti : (int -> 'a -> t) -> 'a array -> t |
| 175 | (** Indexed version of [prvect]. *) |
| 176 | |
| 177 | val prvect_with_sep : |
| 178 | (unit -> t) -> ('a -> t) -> 'a array -> t |
| 179 | (** As [prlist_with_sep], but on arrays. *) |
| 180 | |
| 181 | val prvecti_with_sep : |
| 182 | (unit -> t) -> (int -> 'a -> t) -> 'a array -> t |
| 183 | (** Indexed version of [prvect_with_sep]. *) |
| 184 | |
| 185 | val pr_enum : ('a -> t) -> 'a list -> t |
| 186 | (** [pr_enum pr [a ; b ; ... ; c]] outputs |
| 187 | [pr a ++ str "," ++ spc () ++ pr b ++ str "," ++ spc () ++ ... ++ str "and" ++ spc () ++ pr c]. *) |
| 188 | |
| 189 | val pr_choice : ('a -> t) -> 'a list -> t |
| 190 | (** [pr_choice pr [a ; b ; ... ; c]] outputs |
| 191 | [pr a ++ str "," ++ spc () ++ pr b ++ str "," ++ spc () ++ ... ++ str "or" ++ spc () ++ pr c]. *) |
| 192 | |
| 193 | val pr_sequence : ('a -> t) -> 'a list -> t |
| 194 | (** Sequence of objects separated by space (unless an element is empty). *) |
| 195 | |
| 196 | val surround : t -> t |
| 197 | (** Surround with parenthesis. *) |
| 198 | |
| 199 | val pr_vertical_list : ('b -> t) -> 'b list -> t |
| 200 | |
| 201 | (** {6 Main renderers, to formatter and to string } *) |
| 202 | |
| 203 | (** [pp_with fmt pp] Print [pp] to [fmt] and don't flush [fmt] *) |
| 204 | val pp_with : Format.formatter -> t -> unit |
| 205 | |
| 206 | val string_of_ppcmds : t -> string |
| 207 | |
| 208 | (** Tag prefix to start a multi-token diff span *) |
| 209 | val start_pfx : string |
| 210 | |
| 211 | (** Tag prefix to end a multi-token diff span *) |
| 212 | val end_pfx : string |
| 213 | |
| 214 | (** Split a tag into prefix and base tag *) |
| 215 | val split_tag : string -> string * string |
| 216 | |
| 217 | (** Print the Pp in tree form for debugging *) |
| 218 | val db_print_pp : Format.formatter -> t -> unit |
| 219 | |
| 220 | (** Print the Pp in tree form for debugging, return as a string *) |
| 221 | val db_string_of_pp : t -> string |
| 222 | |
| 223 | (** Returns [fmt, args] such that [fmt] is a format string which |
| 224 | applied to [args] produces the same result as [pp_with]. |
| 225 | |
| 226 | [with_tags] default false. *) |
| 227 | val pp_as_format : ?with_tags:bool -> t -> string * string list |
| 228 | |
| 229 | (** Combine nested Ppcmd_glues *) |
| 230 | val flatten : t -> t |