-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support statements in ITE in hint functions #248
Comments
agree that in the hint functions we should allow anything (early return, early break, large if-then-else statements, etc.) in the rest IMO we should not for now, as it would be hard to support other backends than r1cs |
@katat I see the pr that you raised was closed for some reason. Is this something not desired anymore? or is it just too difficult to implement atm? imho it will be a great feature and would love to work on this |
It can be tricky to support this on different arithmetic backends. Also there will be issues when calling builtin functions like However, it should be feasible to enhance the ITE in hint functions, which doesn't involve constraints. |
Yeah, cool! Let's ignore #211 for now, just focus on enhancing the ITE in hint functions. |
@katat a question pub fn parse(ctx: &mut ParserCtx, tokens: &mut Tokens) -> Result<Self> so do we change this to pub fn parse(ctx: &mut ParserCtx, tokens: &mut Tokens, from_hint: bool) -> Result<Self> or is there any way for me to get the context of body is from which function that I am parsing? I cannot reverse iterate over tokens as the iterator is already moved forward. |
Also there is a thing with returning early from the ite statements. I think we have to also drill down the function's So what I was doing was something like this: StmtKind::Ite {
condition,
then_branch,
else_branch,
// add a return_type Option<Ty> here
} => {
//enter a new scope
typed_fn_env.nest();
let cond_type = self.compute_type(condition, typed_fn_env)?.unwrap();
if !is_bool(&cond_type.typ) {
return Err(self.error(
ErrorKind::IfElseInvalidConditionType(cond_type.typ),
condition.span,
));
}
typed_fn_env.start_ite();
// check block and pass the expected return which should the return type of the hint function
self.check_block(typed_fn_env, then_branch, expected_return, new_scope) // pass it down here
} or is there any other way for me to get the function scope that I am in and extract that function's return type |
In parser phase, it would need to assume the ITE branches support statements. When it comes to type check phase, it will know whether it is a hint function or not when computing the expression types. ITE can be checked and statements should be only allowed in hint functions.
I think it makes sense to create a new statement kind for the ITE like that, as it would to type check the branch blocks in a way similar to function block. After the parser phase, the |
So we would parse ITE anyway and throw error at the type checker level
Can we add this to the struct ParserCtx {
..
// bool indicates whether it is a hint function or not
pub fn_sig: Option<(FnSig,bool)> // stores the Fn signature for which the parser was called from this allows us to type check the ite
// statement and see if the return type in the ite statements match the return type of fn_sig from the parser
} or is there anything else we can do? So here is what the flow is:
const fn_sig = FnSig::parse(ctx,tokens)?; // code in function def
ctx.fn_sig = Some((fn_sig,false) // from FunctionDef::parse
ctx.fn_sig = Some((fn_sig,true)) // from FunctionDef::parse_hint
pub enum Stmt {
Ite {
return_ty: Option<Ty> // if the put this in the stmt so we can type check at the checker
}
|
I am not sure I follow the idea of adding We already have the Or are there benefits of having |
Got it will move this logic to struct TypedFnEnv {
..
return_and_is_hint: (Option<Ty> , bool)
} Should I make two fields in the struct or one tuple? Does this makes more sense now I agree that this better than mutating the parser ctx. Just set this value in the |
In order to support the following calculation:
The compiler should support statements and early returns in the ITE block, otherwise it is too tricky to implement the logic like this. Perhaps at least we should enhance this feature in the hint functions.
The text was updated successfully, but these errors were encountered: