Skip to content

Commit

Permalink
grammar: program: default from expand (+) to no-expand/shared (*) for…
Browse files Browse the repository at this point in the history
… PRs
  • Loading branch information
RenatoGeh committed May 29, 2024
1 parent 0016235 commit 579a5e2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions pasp/grammar.lark
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ GITHUB: /[A-Za-z\d](?:[A-Za-z\d]|-(?=[A-Za-z\d])){0,38}\/[a-zA-Z0-9\-_.]{1,100}/
// Path.
path.10: "\"" (LOCAL_DATA | URL) "\""

EXPAND: "+"
SHARED: "*"
LEARN: "?"
CONST: "!"

Expand Down Expand Up @@ -104,7 +104,7 @@ body: (lit | bop) ("," (lit | bop))*
// Rule.
rule: head ":-" body "."
// Probabilistic rule.
prule: prob? (EXPAND? LEARN? | LEARN? EXPAND?) "::" ohead ":-" body "."
prule: prob? (SHARED? LEARN? | LEARN? SHARED?) "::" ohead ":-" body "."
_rule: prule | rule

// Annotated disjunction head.
Expand Down
12 changes: 6 additions & 6 deletions pasp/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def OP(self, o): return self.pack("OP", str(o))
def REAL(self, r): return self.pack("REAL", val = float(r))
def frac(self, f): return self.pack("frac", val = f[0][2]/f[1][2])
def prob(self, p): return self.pack("prob", val = p[0][2])
def EXPAND(self, e): return self.pack("EXPAND", str(e))
def SHARED(self, s): return self.pack("SHARED", str(s))
def LEARN(self, l): return self.pack("LEARN", str(l))
def CONST(self, c): return self.pack("CONST", str(c))
def BOOL(self, b): return self.pack("BOOL", v := b.lower(), v != "false")
Expand Down Expand Up @@ -245,7 +245,7 @@ def body(self, B): return self.pack("body", ", ".join(getnths(B, 1)), B, self.jo
def rule(self, R): return self.pack("rule", " :- ".join(getnths(R, 1)) + ".")
def prule(self, R):
l = "LEARN" in getnths(R, 0)
e = "EXPAND" in getnths(R, 0)
s = "SHARED" in getnths(R, 0)
h, b = R[-2], R[-1]
o = f"{h[1]} :- {b[1]}"
p = R[0][2] if R[0][0] == "prob" else 0.5
Expand All @@ -262,12 +262,12 @@ def prule(self, R):
h_s = ", ".join(hscope) + ", " if len(hscope) > 0 else ""
b_s = ", ".join(map(lambda x: f"1, {x[1]}" if x[2][0] else f"0, {x[1][4:]}", body_preds))
# If parameters are shared, then we require a special ID.
upr = -1 if (e or not l) else unique_pgrule_id()
upr = -1 if not (s and l) else unique_pgrule_id()
# The number of body arguments is twice as we need to store the sugoal's sign and symbol.
rid = self.n_prules; self.n_prules += 1
u = f"{name}(@unify({rid}, {name}, {int(l)}, {upr}, {len(hscope)}, {2*len(body_preds)}, {h_s}{b_s})) :- {b[1]}."
return self.pack("prule", "", ProbRule(p, o, is_prop = False, unify = u, learnable = l,
sharing = not e))
sharing = s))

# Annotated disjunction head.
def ad_head(self, H):
Expand Down Expand Up @@ -568,7 +568,7 @@ def rule(self, R):

def prule(self, R):
l = "LEARN" in getnths(R, 0)
e = "EXPAND" in getnths(R, 0)
s = "SHARED" in getnths(R, 0)
p = R[0][2] if R[0][0] == "prob" else 0.5
h, b = R[-2], R[-1]
tr_negs = lambda x: x[1] if x[2][0] else f"not _{x[1][4:]}"
Expand All @@ -590,7 +590,7 @@ def prule(self, R):
h_s = ", ".join(hscope) + ", " if len(hscope) > 0 else ""
b1_s = ", ".join(map(lambda x: f"1, {x[1]}" if x[2][0] else f"0, _{x[1][4:]}", body_preds))
# If parameters are shared, then we require a special ID.
upr = -1 if (e or not l) else unique_pgrule_id()
upr = -1 if not(s and l) else unique_pgrule_id()
# Let the grounder deal with the _f rule.
rid = self.n_prules; self.n_prules += 1
u1 = f"{name}(@unify({rid}, {name}, {int(l)}, {upr}, {len(hscope)}, {2*len(body_preds)}, {h_s}{b1_s})) :- {b1}."
Expand Down
2 changes: 1 addition & 1 deletion pasp/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(self, p: str, f: str, is_prop: bool = True, unify: str = None, ufac

def __str__(self) -> str:
return f"{self.prop_pf.p if self.is_prop else self.p}" \
f"{('' if (self.sharing or self.is_prop) else '+') + ('?' if self.learnable else '')}" \
f"{('*' if self.sharing else '') + ('?' if self.learnable else '')}" \
f"::{self.f}"
def __repr__(self) -> str: return self.__str__()

Expand Down

0 comments on commit 579a5e2

Please sign in to comment.