Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prolog Noob : Constraint Programming library or syntax issue in SWI-Prolog

Tags:

prolog

clpfd

I'm just trying to figure out constraint programming in SWI-Prolog, looking at this tutorial : http://en.wikibooks.org/wiki/Prolog/Constraint_Logic_Programming

However I seem to be falling at the first hurdle.

?- use_module(library(clpfd)).
true.

?- X #> Y, X in 1..3, Y=2.
ERROR: Syntax error: Operator expected
ERROR: X 
ERROR: ** here **
ERROR: #> Y, X in 1..3, Y=2 . 
?- 

What's going wrong here? I seem to have included the library, but the first example line from the tutorial throws a syntax error.

All the tutorials I can find seem to use operators like #=, #< etc. But my SWI-Prolog baulks at them. Are they an extra syntax which comes with that constraint library? (And am I failing to load it?)

Or am I misreading the tutorial examples?

Update : Trying to understand things from Horsh's reply below. I can get this to work if I use the library and run the line in the interactive terminal. But if I try to import the library and use these operators in a source file, then it throws the error again. What am I not understanding?

Update 2 :

OK. If, in my source file, I invoke the library and then write a rule which contains a #>. Then I try to consult it from the command-line. It will throw an error and the #> syntax is un-recognised. If import the library to the command line before trying to consult the program, it works. Can this be right?

like image 780
interstar Avatar asked Apr 08 '11 12:04

interstar


2 Answers

Building on Horsh's answer, you should be importing the library in your source code, remembering to put ?- at the beginning of the line like so:

?- use_module(library(clpfd)).

The ?- tells SWI-Prolog to execute the line as if it were typed into the interpreter directly, instead of trying to declare it as a predicate in your program.

Don't be concerned about SWI-Prolog importing the library more than once, it knows to check if the library was modified and only reloads it if the library was changed since the last time it was loaded.

like image 152
Raceimaztion Avatar answered Oct 23 '22 03:10

Raceimaztion


For anyone else that finds this in the future, if you want to import a library in an SWI-Prolog source file, the following will also work:

:- use_module(library(clpfd)).

Note the :- and not ?-.

like image 9
Nick Knowlson Avatar answered Oct 23 '22 02:10

Nick Knowlson