Skip to content

Commit 19be33f

Browse files
authored
Merge pull request #1113 from stan-dev/pprinter-reorg
Clean up pretty printing of array[]
2 parents 17f651a + 6127bc2 commit 19be33f

3 files changed

Lines changed: 81 additions & 69 deletions

File tree

src/frontend/Pretty_printing.ml

Lines changed: 68 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,14 @@ let indented_box ?(offset = 0) pp_v ppf v =
177177
let pp_unsizedtype = Middle.UnsizedType.pp
178178
let pp_autodifftype = Middle.UnsizedType.pp_autodifftype
179179

180-
let rec unwind_sized_array_type = function
181-
| Middle.SizedType.SArray (st, e) -> (
182-
match unwind_sized_array_type st with st2, es -> (st2, es @ [e]) )
183-
| st -> (st, [])
180+
let rec unwind_sized_array_type st =
181+
match st with
182+
| Middle.SizedType.SInt | SReal | SComplex | SVector _ | SRowVector _
183+
|SMatrix _ ->
184+
(st, [])
185+
| SArray (st, dim) ->
186+
let st', dims = unwind_sized_array_type st in
187+
(st', dim :: dims)
184188

185189
let pp_returntype ppf = function
186190
| Middle.UnsizedType.ReturnType x -> pp_unsizedtype ppf x
@@ -303,18 +307,21 @@ let pp_printable ppf = function
303307

304308
let pp_list_of_printables ppf l = (hovbox @@ list ~sep:comma pp_printable) ppf l
305309

306-
let pp_sizedtype ppf = function
310+
let rec pp_sizedtype ppf = function
307311
| Middle.SizedType.SInt -> pf ppf "int"
308312
| SReal -> pf ppf "real"
309313
| SComplex -> pf ppf "complex"
310314
| SVector (_, e) -> pf ppf "vector[%a]" pp_expression e
311315
| SRowVector (_, e) -> pf ppf "row_vector[%a]" pp_expression e
312316
| SMatrix (_, e1, e2) ->
313317
pf ppf "matrix[%a, %a]" pp_expression e1 pp_expression e2
314-
| SArray _ ->
315-
Common.FatalError.fatal_error_msg [%message "Error printing array type"]
318+
| SArray _ as arr ->
319+
let ty, ixs = unwind_sized_array_type arr in
320+
pf ppf "array[@[%a@]]@ %a"
321+
(list ~sep:comma pp_expression)
322+
ixs pp_sizedtype ty
316323

317-
let pp_transformation ppf = function
324+
let pp_bracketed_transform ppf = function
318325
| Middle.Transformation.Lower e -> pf ppf "<@[lower=%a@]>" pp_expression e
319326
| Upper e -> pf ppf "<@[upper=%a@]>" pp_expression e
320327
| LowerUpper (e1, e2) ->
@@ -328,61 +335,57 @@ let pp_transformation ppf = function
328335
()
329336

330337
let pp_transformed_type ppf (pst, trans) =
331-
let rec discard_arrays pst =
332-
match pst with
333-
| Middle.Type.Sized st ->
334-
Middle.Type.Sized (Fn.compose fst unwind_sized_array_type st)
335-
| Unsized (UArray t) -> discard_arrays (Unsized t)
336-
| Unsized ut -> Unsized ut in
337-
let pst = discard_arrays pst in
338-
let unsizedtype_fmt =
339-
match pst with
340-
| Middle.Type.Sized (SArray _ as st) ->
341-
const pp_sizedtype (Fn.compose fst unwind_sized_array_type st)
342-
| _ -> const pp_unsizedtype (Middle.Type.to_unsized pst) in
343-
let sizes_fmt =
344-
match pst with
345-
| Sized (SVector (_, e)) | Sized (SRowVector (_, e)) ->
346-
const (fun ppf -> pf ppf "[%a]" pp_expression) e
347-
| Sized (SMatrix (_, e1, e2)) ->
348-
const (fun ppf -> pf ppf "[%a, %a]" pp_expression e1 pp_expression) e2
349-
| Sized (SArray _)
350-
|Unsized _
351-
|Sized Middle.SizedType.SInt
352-
|Sized SReal
353-
|Sized SComplex ->
354-
nop in
355-
let cov_sizes_fmt =
356-
match pst with
357-
| Sized (SMatrix (_, e1, e2)) ->
358-
if e1 = e2 then const (fun ppf -> pf ppf "[%a]" pp_expression) e1
359-
else
360-
const (fun ppf -> pf ppf "[%a, %a]" pp_expression e1 pp_expression) e2
361-
| _ -> nop in
362-
match trans with
363-
| Middle.Transformation.Identity ->
364-
pf ppf "%a%a" unsizedtype_fmt () sizes_fmt ()
365-
| Lower _ | Upper _ | LowerUpper _ | Offset _ | Multiplier _
366-
|OffsetMultiplier _ ->
367-
pf ppf "%a%a%a" unsizedtype_fmt () pp_transformation trans sizes_fmt ()
368-
| Ordered -> pf ppf "ordered%a" sizes_fmt ()
369-
| PositiveOrdered -> pf ppf "positive_ordered%a" sizes_fmt ()
370-
| Simplex -> pf ppf "simplex%a" sizes_fmt ()
371-
| UnitVector -> pf ppf "unit_vector%a" sizes_fmt ()
372-
| CholeskyCorr -> pf ppf "cholesky_factor_corr%a" cov_sizes_fmt ()
373-
| CholeskyCov -> pf ppf "cholesky_factor_cov%a" cov_sizes_fmt ()
374-
| Correlation -> pf ppf "corr_matrix%a" cov_sizes_fmt ()
375-
| Covariance -> pf ppf "cov_matrix%a" cov_sizes_fmt ()
376-
377-
let pp_array_dims ppf = function
378-
| [] -> ()
379-
| es ->
380-
let ({emeta= {loc= {end_loc; _}; _}; _} : untyped_expression) =
381-
List.hd_exn es in
382-
let es = List.rev es in
383-
let ({emeta= {loc= {begin_loc; _}; _}; _} : untyped_expression) =
384-
List.hd_exn es in
385-
pf ppf "array[@[%a]@] " pp_list_of_expression (es, {begin_loc; end_loc})
338+
let open Middle in
339+
match pst with
340+
| Type.Unsized ust ->
341+
(* unsized types are untransformed *)
342+
pf ppf "%a" pp_unsizedtype ust
343+
| Type.Sized st -> (
344+
let pp_possibly_transformed_type ppf (st, trans) =
345+
let sizes_fmt =
346+
match st with
347+
| SizedType.SVector (_, e) | SRowVector (_, e) ->
348+
const (fun ppf -> pf ppf "[%a]" pp_expression) e
349+
| SMatrix (_, e1, e2) ->
350+
const
351+
(fun ppf -> pf ppf "[%a, %a]" pp_expression e1 pp_expression)
352+
e2
353+
| SArray _ | SInt | SReal | SComplex -> nop in
354+
let cov_sizes_fmt =
355+
match st with
356+
| SMatrix (_, e1, e2) ->
357+
if e1 = e2 then const (fun ppf -> pf ppf "[%a]" pp_expression) e1
358+
else
359+
const
360+
(fun ppf -> pf ppf "[%a, %a]" pp_expression e1 pp_expression)
361+
e2
362+
| _ -> nop in
363+
match trans with
364+
| Transformation.Identity -> pf ppf "%a" pp_sizedtype st
365+
| Lower _ | Upper _ | LowerUpper _ | Offset _ | Multiplier _
366+
|OffsetMultiplier _ ->
367+
pf ppf "%a%a%a" pp_unsizedtype (SizedType.to_unsized st)
368+
pp_bracketed_transform trans sizes_fmt ()
369+
| Ordered -> pf ppf "ordered%a" sizes_fmt ()
370+
| PositiveOrdered -> pf ppf "positive_ordered%a" sizes_fmt ()
371+
| Simplex -> pf ppf "simplex%a" sizes_fmt ()
372+
| UnitVector -> pf ppf "unit_vector%a" sizes_fmt ()
373+
| CholeskyCorr -> pf ppf "cholesky_factor_corr%a" cov_sizes_fmt ()
374+
| CholeskyCov -> pf ppf "cholesky_factor_cov%a" cov_sizes_fmt ()
375+
| Correlation -> pf ppf "corr_matrix%a" cov_sizes_fmt ()
376+
| Covariance -> pf ppf "cov_matrix%a" cov_sizes_fmt () in
377+
match st with
378+
(* array goes before something like cov_matrix *)
379+
| Middle.SizedType.SArray _ ->
380+
let ty, ixs = unwind_sized_array_type st in
381+
let ({emeta= {loc= {end_loc; _}; _}; _} : untyped_expression) =
382+
List.last_exn ixs in
383+
let ({emeta= {loc= {begin_loc; _}; _}; _} : untyped_expression) =
384+
List.hd_exn ixs in
385+
pf ppf "array[@[%a@]]@ %a" pp_list_of_expression
386+
(ixs, {begin_loc; end_loc})
387+
pp_possibly_transformed_type (ty, trans)
388+
| _ -> pf ppf "%a" pp_possibly_transformed_type (st, trans) )
386389

387390
let rec pp_indent_unless_block ppf ((s : untyped_statement), loc) =
388391
match s.stmt with
@@ -459,12 +462,8 @@ and pp_statement ppf ({stmt= s_content; smeta= {loc}} as ss : untyped_statement)
459462
let pp_init ppf init =
460463
match init with None -> () | Some e -> pf ppf " = %a" pp_expression e
461464
in
462-
let es =
463-
match pst with
464-
| Sized st -> Fn.compose snd unwind_sized_array_type st
465-
| Unsized _ -> [] in
466-
pf ppf "@[<h>%a%a %a%a;@]" pp_array_dims es pp_transformed_type
467-
(pst, trans) pp_identifier id pp_init init
465+
pf ppf "@[<h>%a %a%a;@]" pp_transformed_type (pst, trans) pp_identifier id
466+
pp_init init
468467
| FunDef {returntype= rt; funname= id; arguments= args; body= b} -> (
469468
let loc_of (_, _, id) = id.id_loc in
470469
pf ppf "%a %a(%a" pp_returntype rt pp_identifier id

test/integration/good/comments.stan

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@
1717
data {
1818
//nothing here either
1919
}
20+
21+
transformed data {
22+
int N;
23+
array[N/*test1*/ , //test1.5
24+
N/*test2*/] real arr;
25+
}

test/integration/good/pretty.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,13 @@ generated quantities {
13521352
data {
13531353
//nothing here either
13541354
}
1355+
transformed data {
1356+
int N;
1357+
array[N /*test1*/, //test1.5
1358+
N] real arr;
1359+
1360+
/* ^^^:test2*/
1361+
}
13551362

13561363
$ ../../../../install/default/bin/stanc --auto-format conditional_condition_good.stan
13571364
parameters {

0 commit comments

Comments
 (0)