Synthesis with partial functions
Calvin-L opened this issue · 0 comments
Calvin-L commented
Right now all AST nodes in Cozy are total, meaning that they can never fail. A good example is map lookup (map[key]
) which returns a default value if the key is missing rather than throw an exception.
Unfortunately, this means that at codegen time we need to insert additional checks (e.g. "is the key in the map? if not, return a default value"). It would be much better if the synthesizer knew that some functions were only legal under some conditions so that we would not have to emit these checks. After all, in many cases the checks will always be true.
Examples of partial functions:
the xs
is only legal whenxs
is a singleton collection- for handles,
x.val
is only legal whenx
is not null m[key]
is only legal whenkey
is inm
min xs
is only legal whenxs
is not emptyx / y
is only legal wheny
is nonzero
Plan:
- As far as
satisfy
andeval
are concerned, everything remains total - Need a new function
fails : Exp -> Exp
that produces a boolean expression describing under what circumstances the input expression fails - Optional: all input queries should never fail (their preconditions must be strong enough)
- Failure on each example should be part of the fingerprint of an expression (and furthermore, failure on an input renders the output value irrelevant when comparing fingerprints)
-
core.py
should ensure that not only does the new expression produce the same values, but it may only fail when the original expression fails (although the output is allowed to fail less often) - Codegen may then skip unnecessary checks on partial AST nodes