Skip to content
27 changes: 24 additions & 3 deletions src/frontend/Semantic_check.ml
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,30 @@ let semantic_check_fn_normal ~is_cond_dist ~loc id es =
| Some _ ->
(* Check that Funaps are actually functions *)
Semantic_error.returning_fn_expected_nonfn_found loc id.name |> error
| None ->
Semantic_error.returning_fn_expected_undeclaredident_found loc id.name
|> error)
| None -> (
match List.rev (String.split id.name ~on:'_') with
| (("lupdf" | "cdf" | "lcdf" | "lccdf" | "rng") as suffix) :: tl ->
let prefix = String.concat ~sep:"_" (List.rev tl) in
let known_families =
List.map
~f:(fun (_, y, _, _) -> y)
Stan_math_signatures.distributions
in
let is_known_family s =
List.mem known_families s ~equal:String.equal
in
if is_known_family prefix then
Semantic_error.returning_fn_expected_undeclared_dist_suffix_found
loc (prefix, suffix)
|> error
else
Semantic_error.returning_fn_expected_undeclaredident_found loc
id.name
|> error
| _ ->
Semantic_error.returning_fn_expected_undeclaredident_found loc
id.name
|> error ))

(* Stan-Math function application *)
let semantic_check_fn_stan_math ~is_cond_dist ~loc id es =
Expand Down
14 changes: 14 additions & 0 deletions src/middle/Semantic_error.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module TypeError = struct
| ReturningFnExpectedNonReturningFound of string
| ReturningFnExpectedNonFnFound of string
| ReturningFnExpectedUndeclaredIdentFound of string
| ReturningFnExpectedUndeclaredDistSuffixFound of string * string
| NonReturningFnExpectedReturningFound of string
| NonReturningFnExpectedNonFnFound of string
| NonReturningFnExpectedUndeclaredIdentFound of string
Expand Down Expand Up @@ -168,6 +169,13 @@ module TypeError = struct
"A returning function was expected but an undeclared identifier \
'%s' was supplied."
fn_name
| ReturningFnExpectedUndeclaredDistSuffixFound (prefix, suffix) ->
Fmt.pf ppf
"A function was expected but an unknown identifier %s was recieved. \
This appears to be part of the %s family of distributions, for \
which the %s suffix in not implemented."
(String.concat ~sep:"_" [prefix; suffix])
prefix suffix
| NonReturningFnExpectedUndeclaredIdentFound fn_name ->
Fmt.pf ppf
"A non-returning function was expected but an undeclared identifier \
Expand Down Expand Up @@ -496,6 +504,12 @@ let returning_fn_expected_nonfn_found loc name =
let returning_fn_expected_undeclaredident_found loc name =
TypeError (loc, TypeError.ReturningFnExpectedUndeclaredIdentFound name)

let returning_fn_expected_undeclared_dist_suffix_found loc (prefix, suffix) =
TypeError
( loc
, TypeError.ReturningFnExpectedUndeclaredDistSuffixFound (prefix, suffix)
)

let nonreturning_fn_expected_returning_found loc name =
TypeError (loc, TypeError.NonReturningFnExpectedReturningFound name)

Expand Down
3 changes: 3 additions & 0 deletions src/middle/Semantic_error.mli
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ val returning_fn_expected_nonfn_found : Location_span.t -> string -> t
val returning_fn_expected_undeclaredident_found :
Location_span.t -> string -> t

val returning_fn_expected_undeclared_dist_suffix_found :
Location_span.t -> string * string -> t

val illtyped_reduce_sum :
Location_span.t
-> string
Expand Down