Skip to content

Inconsistency of allowing target+= #979

@WardBrian

Description

@WardBrian

Consider two stan programs:

prog1:

transformed parameters {
   target += 1;
}

prog2:

functions {
   void foo_lp(real x){
     target += x;
   }
}

transformed parameters {
   foo_lp(1);
}

prog1. stan produces a type error:

Semantic error in 'prog1.stan', line 2, column 3 to column 15:
   -------------------------------------------------
     1:  transformed parameters {
     2:     target += 1;
            ^
     3:  }
   -------------------------------------------------
Target can only be accessed in the model block or in definitions of functions with the suffix _lp.

prog2.stan compiles just fine.

This is because we have this check for target+= statements:

let semantic_check_target_pe_usage ~loc ~cf =
if cf.in_lp_fun_def || cf.current_block = Model then Validate.ok ()

And this check for the calling of _lp functions:

let semantic_check_fn_target_plus_equals cf ~loc id =
Validate.(
if
String.is_suffix id.name ~suffix:"_lp"
&& not
( cf.in_lp_fun_def || cf.current_block = Model
|| cf.current_block = TParam )
then Semantic_error.target_plusequals_outisde_model_or_logprob loc |> error
else ok ())

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions