mxgmn / MarkovJunior

Probabilistic language based on pattern matching and constraint propagation, 153 examples
MIT License
7.49k stars 316 forks source link

Question about MarkovNode nesting and union. #56

Closed zylkowski closed 2 years ago

zylkowski commented 2 years ago

I really love the work in MarkovJunior and I'd like to attempt rewriting it in Rust. My idea is to write fully unoptimized version without inference first and then learning from the process I'd attempt writing full library with optimizations and inference. I am having trouble understanding two concepts though.

Like in REFAL, Markov nodes can be nested: once we go into a child node, we ignore outer nodes until the child branch completes.

Why does nested MarkovNodes are prioritized when entered? I cannot see any advantage of this behaviour other than sometimes making the algorithm a bit more concise.

What exactly is union in terms of markov algorithms? I couldn't find any explaination online that would fit behaviour I see in some examples.

kaya3 commented 2 years ago

Nested Markov nodes aren't "prioritised", in the sense that there is no special case for the language's semantics. Basically to execute a Markov node, you repeatedly find its first applicable child and execute that, and you stop when none of the children are applicable. If one of the children is a Markov node then "executing the child" means you do that whole process and then return to the outer Markov node to continue executing it.

It's analogous to nested loops in imperative languages - when you enter an inner loop, the inner loop has to complete all of its iterations before control returns to the outer loop - so you "ignore" everything outside the inner loop while it's executing.

A union is a kind of wildcard, e.g. if your grid values are ABCD then the wildcard * means any of those characters, but you can also declare a union like E = AB and then if you write E in a rule, it will match either A or B.

kaya3 commented 2 years ago

By the way, this kind of question is better to ask in the discussion forum, rather than posting an issue.