Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Does anyone know if there's a datalog-like embeddable language that is suitable for simple integer constraints (the sum of a, b, c, and d must be e; e=10.,b>2.)?

For prolog, there's apparently https://github.com/triska/clpz/

But I really just want some simple constraints-oriented form validation (you have 100 points, please feel free to by x's for 1 point each, y's for a cost of 5 points. You don't have to spend all points - with feedback of how many points are left as more x and y are bought. But for more variables, where there are clear rules that can be modeled naturally as constraints rather than with state and callbacks on change).

Ed: specifically for forms, there's https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML... - but it feels like it gets messy with interdependent fields.



You might like a Satisfiability Modulo Theories (SMT) solver such as Z3.

https://github.com/Z3Prover/z3

It has bindings for other languages and is able to make some rather sophisticated arithmetical and logical deductions in an often pretty efficient way. I've used it successfully for parts of puzzle competitions and CTFs (and I'm really only beginning to scratch the surface of what it can do).

You can read more about the options in that area at

https://en.wikipedia.org/wiki/Satisfiability_modulo_theories

which also links to the related tools in

https://en.wikipedia.org/wiki/Answer_set_programming

https://en.wikipedia.org/wiki/Constraint_programming

These systems can feel pretty magical and are getting more powerful all the time. (Of course there are cases where there is a better algorithm that you could find, perhaps a much more efficient one, and using these solvers will be asymptotically or worst-case slower than it had to be given more human insight.)

Some of these could be overkill for your application, and Z3 doesn't have a Javascript binding, so I guess it's not as helpful for web forms.


Do you have any suggestions on how to get started? I remember reading "The unreasonable effectiveness of SAT solvers" or whatever it was called which showed that SAT solvers are not always the most efficient performers, but they can take you within a factor 10 in much less time. I remember thinking, "Oh, but what about SMT solvers, then?" But not getting anywhere with that.


SAT, SMT, constraint programming lie on a continuum. SMT is SAT plus machinery from constraint programming. I'm not an expert on this, just info from this video. According to him constraint programming will win in the long run because of constraint programming has larger structures which can be exploited with global constraints (SAT is only a flat sequence).

https://www.youtube.com/watch?v=YVbbNeM74lc


If your main use case is browser form validation, have a look at Tau Prolog. It doesn't have constraint library but standard Prolog should be enough for something like this.

http://tau-prolog.org/

The newly released Yarn 2 uses Tay Prolog to implement constraints on package versions and usage:

https://github.com/yarnpkg/berry/tree/master/packages/plugin...


Afair, lpsolve had both C api (you may find it horrible) and dsl. http://lpsolve.sourceforge.net/5.5/

There are few web/node-based, but I assume you googled them already.


lp_solve and coinOR project's CBC are free/open source and can bind to C or C++.

They also are released as binary executables that can be called from the command line or Excel.


Not anything embeddable that I know of, but the functionality you describe is present in SWI-Prolog, which is free software.

If it's a one-off job, I'd just use Mathematica to do it though.


Yes, I should've mentioned I found plus/3 for prolog. Not sure if it's available as something that can be coaxed into running in the browser, though.

https://www.swi-prolog.org/pldoc/man?predicate=plus%2f3

I've not been able to find something similar for datalog - but to be honest, I don't think I've managed to find solid documentation for datalog. Not that I've been able to really grok the language from enough to build an intuition for what's an idiomatic approach, at least.


No, plus/3 is just a simple addition predicate. You should take a look at the CLP(FD) functionality: https://www.swi-prolog.org/man/clpfd.html

You also mentioned CLP(Z) in the parent comment. Not sure if familiar you are with these tools, but here's a nice intro: https://www.metalevel.at/prolog/clpz

Avoid GNU Prolog. It sucks.


Thanks for the tip.

Ed: the introduction looks like exactly what I need from the prolog side, last I looked at this stuff in prolog, I only found some proof of concept/instructive texts using Peano numbers - which works after a fashion, but is slow and painful.

There's nothing analogous for datalog or some other minimal, embeddable logic/predicate system?


Hm, maybe this could be combined with reasonml/ocaml js target...

https://github.com/Emmanuel-PLF/facile


Tau Prolog[0] runs in the browser, but it's still a bit heavy for forms.

What someone needs to do is take an implementation of Hitchhiker Prolog[1] like this one[2] – because it's so lightweight – and make it JavaScript developer friendly so one can use it on a web page without a second thought.

[0] http://tau-prolog.org

[1] http://www.cse.unt.edu/~tarau/research/2017/eng.pdf

[2] https://github.com/CapelliC/hitchhicker-prolog




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: