Hacker News new | past | comments | ask | show | jobs | submit login

Constraint logic programming is generally proposed as a more natural alternative for arithmetic, in Prolog, than Prolog's native arithmetic functions.

It's a biig discussion and I'm not the one to make the argument for CLP, but, for example, in straight-up Prolog arithmetic is implemented using the is/2 predicate, which looks a bit like this:

  A is 1 + 2.
Where A is the result of the addition of 1 and 2. In CLP on the other hand, particularly in the CLP library for reasoning over integers, CLP(FD), the addition above looks like this:

  X #= 1+2.
Note that here, "#=/2" ("equals") is a constraint, so the statement above is true, iff the expression on the left-hand side of the #= is equal to the right-hand side of it. Which lets you do things like this:

  ?- 3 #= Y+2.
  Y = 1.
Where trying to do the same thing with is/2 will give you an error (because Y is not sufficiently instantiated). And because of how constraint programming works, you can also do arithmetic with ranges, like so:

  ?- X #= A + B, A in 1..3, B in 4..6.
  X in 5..9,
  A+B#=X,
  A in 1..3,
  B in 4..6.
In fact, what comes out the other end of the query above (the bit after the query prompt, "?") is a set of new constraints, where the result of adding A in [1,3] and B in [4,6] is a number, X, in [5,9].

You can find more information in the CLP(FD) library documentation, here:

http://www.swi-prolog.org/man/clpfd.html

Or, if Markus Triska (the author of the library) is around, he can probably elucidate its use a bit better than I can :)




Consider applying for YC's first-ever Fall batch! Applications are open till Aug 27.

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

Search: