Add ConditionalRule

This looks to capture what people generically mean when saying if-then, rather than an implication
This commit is contained in:
Olivia Appleton 2022-10-21 21:53:48 -04:00
parent a1f021b8e3
commit fe1f03d74f
GPG Key ID: AF65A9CA0FF7FD69
6 changed files with 21 additions and 4 deletions

Binary file not shown.

Binary file not shown.

View File

@ -27,6 +27,7 @@ AVAILABLE_RULES = [
"generic.XORRule",
"generic.XNORRule",
"generic.ImpliesRule",
"generic.ConditionalRule",
"generic.ResolveAtTime",
"generic.ResolveToValue",
"generic.AdditiveRule",

View File

@ -205,9 +205,8 @@ class Market(DictDeserializable):
def should_resolve(self) -> bool:
"""Return whether the market should resolve, according to our rules."""
return any(
rule.value(self) for rule in (self.do_resolve_rules or ())
) and not self.market.isResolved
futures = [parallel(rule.value, self) for rule in (self.do_resolve_rules or ())]
return any(future.result() for future in futures) and not self.market.isResolved
def resolve_to(self) -> AnyResolution:
"""Select a value to be resolved to.

View File

@ -106,6 +106,22 @@ class ImpliesRule(BinaryRule[Optional[BinaryResolution]]):
return not self.rule1._value(market) or bool(self.rule2._value(market))
@define(slots=False)
class ConditionalRule(BinaryRule[BinaryResolution]):
"""Cancels if the premise is false, and resolve to another value otherwise."""
_explainer_stub: ClassVar[str] = (
"Cancels if the next line resolves False, otherwise resolves to the value of the item after"
)
def _value(self, market: Market) -> BinaryResolution:
f_val1 = parallel(self.rule1._value, market)
f_val2 = parallel(self.rule2._value, market)
if not f_val1.result():
return "CANCEL"
return f_val2.result()
@define(slots=False)
class ResolveAtTime(DoResolveRule):
"""Return True if the specified time is in the past."""

View File

@ -32,6 +32,7 @@ validators: dict[str, Validator] = {
"generic.XORRule": (lambda x, y: bool(x) != bool(y)),
"generic.XNORRule": (lambda x, y: bool(x) == bool(y)),
"generic.ImpliesRule": (lambda x, y: bool((not x) or y)),
"generic.ConditionalRule": (lambda x, y: "CANCEL" if (not x) else bool(y)),
}
@ -62,7 +63,7 @@ def test_binary_rule(binary_rule: str) -> None:
"rule1": ["generic.ResolveToValue", {"resolve_value": val1}],
"rule2": ["generic.ResolveToValue", {"resolve_value": val2}]
})._value(mkt)
assert bool(from_dict_val) is expected
assert from_dict_val == "CANCEL" or bool(from_dict_val) is expected
def test_negate_rule_value() -> None: