The Prolog Cabin

A place to learn and discuss about Prolog.
mat
Aug 27, 2018 14:15
Let us unfreeze the room pre-emptively!
mat
Aug 13, 2018 13:56
One thing seems clear: Even if you run it as is, you get error messages in Try it online: "Type error: character_code' expected, found -1' (an integer)". So, it seems to have problems with this input in the first place. I recommend you also try it on the terminal for comparison.
mat
Jun 3, 2018 07:40
you cane use once/1 to restrict yourself to the first answer!
mat
May 23, 2018 19:10
Is everything alright?
mat
May 10, 2018 17:15
You can use string_code/3. But overall, I recommend to simply set double_quotes to chars, and forget about strings. It is much nicer to work on lists of characters.
mat
May 5, 2018 20:05
Maybe use ignore/1?
mat
Apr 18, 2018 06:36
@Michelle: you need a separate utility to write files. For example, try Emacs! You can then consult these files in Prolog.
mat
Mar 17, 2018 22:15
@dzalma: You are doing "generate and test", which tries many, many possibilities that can never be completed to a solution. It should be quite easy to turn this to "constrain and enumerate", by first stating the relevant constraints, and then trying values. Constraints can significantly prune the search space much earlier.
mat
Mar 9, 2018 07:21
In my opinion, the textual tracer is unusable. In SWI-Prolog, try the graphical debugger: ?- gtrace, your_goal.
mat
Mar 8, 2018 20:10
In SWI-Prolog, you can use ?- help(foldl/4).
mat
Mar 8, 2018 20:04
@Laikoni: Yes, that's one way to solve it! And interestingly, you can express both clauses with a single auxiliary predicate over which you can use foldl/4, and retain the determinism.
mat
Mar 8, 2018 20:02
Regarding the performance, I suggest for example the following query: ?- time((between(1, 1 000 000,_), X is 1+3, false))., and then to replace "is" by "#=". In recent SWI versions, I measure about 11% overhead.
mat
Mar 8, 2018 20:01
@DLsoc: In my personal opinion, it would be a great improvement in SWI-Prolog if you *didn't* have to add `:-
use_module(library(clpfd)).` to use declarative integer arithmetic. That's how it is for example in GNU Prolog. It is my hope that with enough interest from application programmers, we can together effect such a change!
mat
Mar 8, 2018 09:38
@DLosc: Using CLP(FD), you can shave off 4 bytes from this: P#=N-1 and also F#=G*N.
mat
Mar 8, 2018 08:42
ascending_([], _). and further: ascending_([L|Ls], L0) :- ... I leave the rest as a challenge. An advantage of this way to formulate the task is that the predicate is deterministic if the list is already fully instantiated. This means that it will succeed without leaving choice-points if the argument is a list of ascending (instantiated) integers.
mat
Mar 8, 2018 08:41
Second, what about the following: The empty list is ascending, i.e., ascending([]). holds. Next, what about lists with one element: ascending([L|Ls) :- ascending_(Ls, L). For this, we now consider an auxiliary relation:
mat
Mar 8, 2018 08:40
Regarding sorted/1, I have a few comments: First, the name "sorted" is rather imperative (at least it is a form of a verb). It suggests that "someone has done something", not that "something is of some kind", which would be a more declarative view. So, I would first suggest a more declarative name. For example, what about ascending/1 ?
 

 Brachylog

Discussions about the Brachylog language. github.com/JCumin/Br...
mat
Jul 4, 2017 19:28
As another example, consider "all lists with 3 elements": Xs = [,,_]. Again, an answer, describing infinitely many solutions, such as [a,a,a], [a,a,b], [f(1),3,-] etc. When you say length(Xs, 3), you post one constraint: Xs has 3 elements. But nothing further!
mat
Jul 4, 2017 19:26
To produce concrete solutions (if there are any), you have to actually use the enumeration predicates like labeling/2. Only this will guarantee to produce concrete solutions, using exhaustive search. As long as there are pending constraints (like in your X in -8\/8 example) there may have one, more or even no solutions.
mat
Jul 4, 2017 19:25
Conversely, We can say for example X #> Y, X #< Y, and the constraint solver will report this as an answer. But there are no concrete integers that satisfy these constraints! This is because the constraint solver is incomplete: It cannot derive everything that does or cannot hold. This is necessarily so, because constraints over integers are actually undecidable!
mat
Jul 4, 2017 19:23
CLP(FD) lets us carry over this notion with the additional restriction that the domain is the set of integers. For example, when we get as answer X in inf..sup, then X must be an integer, but there are still infinitely many. So X = 0, X = 1, X = -1 are all concrete solutions that are described by this answer.
mat
Jul 4, 2017 19:23
@LeakyNun: It it actually quite the same in plain Prolog. The key issue is the difference between answers and solutions. We call it a solution if it is a concrete, ground value. But one answer may describe many, even infinitely many solutions, or even none at all. For example, in plain Prolog, consider the answer you get for X = f(Y). This is an answer. But concrete solutions are: Y = a, Y = b, Y = c, ... so this one term f(Y) describes infinitely many concrete solutions.
mat
Jun 30, 2017 17:13
You will, in due time.
mat
Jun 30, 2017 17:12
Yes, when you reach a certain age you get automatically blurred on stackexchange.
mat
Jun 30, 2017 17:09
It would have been tough financially given that I had not even finished my military service when you all were born...
mat
Jun 28, 2017 22:04
thank you! the new fork construct is great!
mat
May 19, 2017 18:02
Malheureusement, le père de Prolog est décédé.
mat
Jan 14, 2016 13:13
that's OK ;-) one day you may be able to learn a living from these new abilities. So, have fun, and thank you for sharing many interesting snippets!
mat
Jan 14, 2016 13:12
that's OK. everything that's worth learning takes ages.
mat
Jan 14, 2016 13:11
Java code sucks balls in any case, so one remaining recommendation I have: if possible, use Prolog to either translate or interpret the Brachylog code. A DCG might be useful for parsing the program and obtain a syntax tree, and once you have that available, further analysis is a lot easier to do in Prolog. So, I hope this helps, and I'm very interested in seeing what you will come up with. Enjoy!
mat
Jan 14, 2016 13:05
in any case, there are several possible solutions for this. One is make an explicit labeling also avaiable. another is to have special primitives for writing.
mat
Jan 14, 2016 13:04
even better of course would be a way to integrate side-effects in a purer way, maybe you can come up with something very interesting for this.
mat
Jan 14, 2016 13:04
you will certainly have to analyze the given program in some way, but I think it would be doable at least for a very useful subset of programs.
mat
Jan 14, 2016 13:03
that would be doable I think. use an implicit labeling/2 before side effects.
mat
Jan 14, 2016 12:56
And then,simply label all remaining CLP(FD) variables automaticall. This is always a valid operation to do.
mat
Jan 14, 2016 12:56
Then, filter the CLP(FD) variables from that list, using include(fd_var, Ls0, Ls).
mat
Jan 14, 2016 12:55
I would do it like this: wrap the whole Brachylog execution into a call of call_residue_vars/2. This yields the list of variables that are still involved in constraints after the goal has finished.
mat
Jan 14, 2016 12:55
For labeling, you can simply label everything implicitly at the end!
mat
Jan 14, 2016 12:54
first, float would require different operators (which it also currently does: compare CLP(FD) and CLP(Q) syntax).
mat
Jan 14, 2016 12:29
Hi Fatalize!