Skip to content

Conversation

@rybern
Copy link

@rybern rybern commented Apr 10, 2020

No description provided.

@SteveBronder
Copy link
Owner

Do you know how to fix the below so it matches on the sum type correctly? I'm getting errors that look like

This pattern matches values of type [? `Dim of 'a ]
but a pattern was expected which matches values of type
  [ `Dim of Middle__Expr.Typed.t
  | `SparseIterator of
      Middle__Expr.Typed.t * Middle__Expr.Typed.t * Middle__Expr.Typed.t ]
  sexp_list
let pp_validate_data ppf (name, st) =
  if String.is_suffix ~suffix:"__" name then ()
  else
  match SizedType.get_dims st with 
  | `Dim e ->
    pf ppf "@[<hov 4>context__.validate_dims(@,%S,@,%S,@,%S,@,%a);@]@ "
      "data initialization" name
      (stantype_prim_str (SizedType.to_unsized st))
      pp_call
      ("context__.to_vec", pp_expr, e)
  | `SparseIterator (nz_row, nz_col, nz_length) ->
    pf ppf "@[<hov 4>context__.validate_dims(@,%S,@,%S,@,%S,@,%a);@]@ "
    "data initialization" name
    (stantype_prim_str (SizedType.to_unsized st))
    pp_call
    ("context__.to_vec", pp_expr, nz_length)

odd because it looks similar to the thing we did with the for loop that does not flag an error

@rybern
Copy link
Author

rybern commented Apr 13, 2020

I think it's complaining that get_dims returns a list of that sum type, but you're only matching one.
If you wanted to do this for each dimension returned, you could use List.iter, like:

let pp_validate_data ppf (name, st) =
  if String.is_suffix ~suffix:"__" name then ()
  else
  List.iter (SizedType.get_dims st) ~f:(fun dim ->
    match dim with 
    | `Dim e -> ...
    | `SparseIterator .. -> ...
  )

@SteveBronder
Copy link
Owner

Excellent! Just to spot check my answer is street legal, I also put a [] around the item before the pp_expr else it complained that I wasn't giving it a sexp_list

let pp_validate_data ppf (name, st) =
  if String.is_suffix ~suffix:"__" name then ()
  else
  List.iter (SizedType.get_dims st) ~f:(fun dim ->
  match dim with
  | `Dim e  ->
    pf ppf "@[<hov 4>context__.validate_dims(@,%S,@,%S,@,%S,@,%a);@]@ "
      "data initialization" name
      (stantype_prim_str (SizedType.to_unsized st))
      pp_call
      ("context__.to_vec", pp_expr, [e])
  | `SparseIterator (_, _, nz_length)  ->
    pf ppf "@[<hov 4>context__.validate_dims(@,%S,@,%S,@,%S,@,%a);@]@ "
    "data initialization" name
    (stantype_prim_str (SizedType.to_unsized st))
    pp_call
    ("context__.to_vec", pp_expr, [nz_length])

@rybern
Copy link
Author

rybern commented Apr 13, 2020

I'm guessing pp_call is printing out a function call, so the third element there is the list of argument expressions? So adding the [] would be right if those are the only arguments to context__.to_vec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants