dry-rb / dry-logic

Predicate logic with rule composition

Home Page:https://dry-rb.org/gems/dry-logic/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Include argmument names in predicate AST

fran-worley opened this issue · comments

Currently the predicate AST simply includes an array of the argument values (excluding the input) as follows:

[:predicate, [:eql?, [1]]
[:predicate, [:filled?, []]

I see a couple of problems with this.

  1. It is hard to read, from the ast alone I should be able to understand what is being passed into the predicate. In addition, the predicate ast only includes the curried arguments, not the called ones.
  2. dry-validation uses the ast to build error messages. Because the AST doesn't provide argument names we end up having to implement methods for each predicate to provide variables to the error messages. This is brittle and means that custom predicates are only given the input value & not the value of other arguments.

I propose the following change to the AST to resolve the above.

Rather than including an array of the curried argument values lets include a nested array of all the arguments with argument name as the first element and the value as the second. For example:

[:predicate, [:eql?, [[:left, 1], [:right, 2]]]

[:predicate, [:filled?, [[:input, "hello"]]]

The determination of argument names should be dynamic and support custom predicates.

Well, that's SO DONE :)