zksecurity / noname

Noname: a programming language to write zkapps
https://zksecurity.github.io/noname/
161 stars 35 forks source link

better doc on `VarOrRef` #144

Open mimoo opened 2 weeks ago

mimoo commented 2 weeks ago

I think the VarOrRef explanation in the book is a bit confusing. I think this explanation is much more concise and clear and could be added as an overview:


pub enum VarOrRef<B: Backend> {
    Var(Var<B::Field, B::Var>),
    Ref {
        var_name: String,
        start: usize,
        len: usize,
    },
}

Every Expression node in the AST is resolved as a VarOrRef. An enum that represents either a variable, or a reference to a variable. The sole reason to use a reference is when the variable is mutable, in which case you must be able to go to the list of variables present in the scope and mutate the correct one (so that if some logic tries to mutate it, it can). That's why, a var_name is stored in a reference. We also pass a (start, len) tuple to handle mutable slices. As we need to remember exactly where we are in the original array. As a slice is a narrowing of an array, we must not lose track of which array we were looking at originally (as this is what needs to be mutated).