smucclaw / dsl

Domain Specific Languages for Computational Law
66 stars 9 forks source link

BNFC grammar vs natural4 grammar rules #602

Open fendor opened 3 months ago

fendor commented 3 months ago

The recently added BNFC #591 parser for a fragment of natural4 has some seeming inconsistencies with the csv-based language.

This issue is for tracking these differences and discussing parts of the syntax and informal semantics.

One thing I found a bit confusing is the where clauses of a Hornlike rule. For example take:

GIVEN x
DECIDE g x IS y
WHERE y IS 5

The Hornlike rule has a wwhere :: [Rule] field, but according to #591, the WHERE part can only contain RelationalPredicates. Is this an intentional difference to natural4 or actually the status-quo of how the natural4 parser works right now?

Note, I am omitting (ignoring) a discussion from the 25.7.2024 where it sounded like WHERE should mirror Haskell's where, allowing the declaration and definition of arbitrary rules.

fendor commented 3 months ago

@inariksit I think you are likely the best person to answer this question?

mengwong commented 2 months ago

My initial ambition was to adopt Haskell where semantics. I think the narrowing to RelationalPredicates was due entirely to limitations at the time, so we went with a simplified implementation. To fully support Haskell where semantics required a fuller notion of scope. We are only now beginning to address that issue. This past week we have tentatively agreed on Module-style semantics for binding scope.

inariksit commented 2 months ago

I believe we had some discussion on whether a GIVETH would make sense in a WHERE, and the results were inconclusive. GIVEN would make sense: this is the (very ad hoc) syntax I/we established for functions last spring, fully based on the PAU case.

GIVEN x IS A Number ; 
            y IS A Number
DECIDE x `discounted by` y IS `x * (1 - y)`

Or in a table form, it looks like

GIVEN x IS A Number
y IS A Number
DECIDE x discounted by y IS x * (1 - y)
fendor commented 2 months ago

Is x * (1 - y) supposed to be a function or the bnfc equivalent MUL(x, MINUS(1, y))?

fendor commented 2 months ago

Regarding the semantics of GIVETH, if we treat as GIVETH as a declaration of output variables, then rules in WHERE clauses may have a GIVETH. For example;

GIVEN x DECIDE f x IS SUM(y, z)
WHERE
  GIVETH y, z 
  DECIDE y IS 5;
         z IS 7

Even though, it is possible to write this in two separate Rules, I think it makes sense to support this syntax. Especially, since we decided WHERE should have Haskell-like where semantics, then the rules in WHERE clauses could be allowed to be full blown Hornlike rules. However, I am unsure how this interacts with UPON and other temporal rules.

inariksit commented 2 months ago

Is x * (1 - y) supposed to be a function or the bnfc equivalent MUL(x, MINUS(1, y))?

Yes, it is! The GML transpiler uses an expression parser inside the cells, but for other targets MUL(x, MINUS(1, y)) should be used.

So in conclusion, should I amend the BNFC parser to accept full Rules in the WHERE clause? (In a way that doesn't break existing examples, of course.)

fendor commented 2 months ago

should I amend the BNFC parser to accept full Rules in the WHERE clause?

For now, I believe so, yeah :)