prolog

Problem with `\+` in Prolog queries with variables

白昼怎懂夜的黑 提交于 2019-12-22 06:35:45
问题 I'm reading "Seven languages in seven weeks" atm, and I'm stumped over some Prolog query that I don't understand the 'no' response to. The friends.pl file looks like this: likes(wallace, cheese). likes(grommit, cheese). likes(wendolene, sheep). friend(X, Y) :- \+(X = Y), likes(X, Z), likes(Y, Z). I can do some trivial queries on it, such as: | ?- ['friends']. compiling /home/marc/btlang-code/code/prolog/friends.pl for byte code... /home/marc/btlang-code/code/prolog/friends.pl compiled, 12

How to convert vectors to arrays in ECLiPSe (CLP)? (or Prolog)

假如想象 提交于 2019-12-22 06:29:00
问题 I have to solve Sudoku puzzles in the format of a vector containing 9 vectors (of length 9 each). Seeing as vectors are linked lists in Prolog, I figured the search would go faster if I transformed the puzzles in a 2D array format first. Example puzzle: puzzle(P) :- P = [[_,_,8,7,_,_,_,_,6], [4,_,_,_,_,9,_,_,_], [_,_,_,5,4,6,9,_,_], [_,_,_,_,_,3,_,5,_], [_,_,3,_,_,7,6,_,_], [_,_,_,_,_,_,_,8,9], [_,7,_,4,_,2,_,_,5], [8,_,_,9,_,5,_,2,3], [2,_,9,3,_,8,7,6,_]]. I'm using ECLiPSe CLP to implement

How to convert vectors to arrays in ECLiPSe (CLP)? (or Prolog)

北城以北 提交于 2019-12-22 06:28:23
问题 I have to solve Sudoku puzzles in the format of a vector containing 9 vectors (of length 9 each). Seeing as vectors are linked lists in Prolog, I figured the search would go faster if I transformed the puzzles in a 2D array format first. Example puzzle: puzzle(P) :- P = [[_,_,8,7,_,_,_,_,6], [4,_,_,_,_,9,_,_,_], [_,_,_,5,4,6,9,_,_], [_,_,_,_,_,3,_,5,_], [_,_,3,_,_,7,6,_,_], [_,_,_,_,_,_,_,8,9], [_,7,_,4,_,2,_,_,5], [8,_,_,9,_,5,_,2,3], [2,_,9,3,_,8,7,6,_]]. I'm using ECLiPSe CLP to implement

catch/3 and call_with_time_limit/2 predicates in SWI-Prolog

北慕城南 提交于 2019-12-22 04:35:47
问题 I want to use catch(:Goal, +Catcher, :Recover) where Goal is call_with_time_limit(+Time, :Goal) It's messed up and I can't find the right way to know when one of the above happened: 1) Goal stopped because of time out. 2) Goal failed (it's suppose to fail sometimes). I tried: (catch(call_with_time_limit(Time, Goal), Catcher, Recover) -> (ground(Catcher), Catcher = time_limit_exceeded), **TIMEOUT ACTIONS**) ; (**SUCCESS ACTIONS**)) ; **FAILURE ACTIONS** ) * EDIT * Pattern: I use the following

read numbers from file in prolog and sorting

允我心安 提交于 2019-12-22 01:18:14
问题 how to read numbers from file and sorting that in (prolog programming) 回答1: You can first try the following, reading multiple lines from the console: ?- repeat, read(X), (X==end_of_file, !, fail; true). 1. X = 1 ; 2. X = 2 ; No Explanation: The repeat/0 predicate repeatedly succeeds so that read/1 is called over and over. Calling read/1 only stops when end_of_file has been reached because of the cut that follows it. Then you can wrap it into a findall/3 and call sort/2: ?- findall(X,(repeat,

How do I include a .pl file in Prolog?

旧街凉风 提交于 2019-12-22 01:13:29
问题 I'd like to include code from another source file. Does anyone know how to do that? 回答1: If your file is called foo.pl , you can include it using :- [foo]. or, equivalently and a bit more explicit :- consult(foo). or, if you're worried it may be loaded several times in a larger app :- ensure_loaded(foo). or, if you're using full-blown modules :- use_module(foo). though the exact name of the last predicate differs between Prolog versions. 回答2: If you want to include the file literally -

What does the operator `-->` in Prolog do?

霸气de小男生 提交于 2019-12-21 19:22:19
问题 What does the --> operator do in Prolog and what is the difference between it and :- ? I'm using SWI Prolog. 回答1: It is used to define a DCG ( D efinite C lause G rammar) rule as opposed to a normal predicate. See this tutorial for a very nice explanation of DCG rules. There are many examples here on Stack Overflow. See the DCG tag. Here is one very simple solution, showing both the DCG and the normal predicate. Note that you have to use phrase/2 or phrase/3 to evaluate a DCG. Make sure to

Prolog predicate with variable number of arguments [closed]

。_饼干妹妹 提交于 2019-12-21 17:28:35
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . I am writing a Sudoku-Solver with PROLOG. I want the solver to work with all possible sizes of Sudokus, so naturally I need to construct predicates which take a variable number of arguments. (For example to construct the "blocks" in the Sudoku.) How can I construct or simulate

How to create a rule that makes all relations symmetric in Prolog?

一笑奈何 提交于 2019-12-21 16:21:11
问题 What I want is when I define: marriedTo(martin, annie). It also makes the following true: marriedTo(annie, martin). I have tried the following, but it's (obviously) an infinite loop. marriedTo(X,Y) :- marriedTo(Y,X). How would I do this in Prolog? 回答1: The most simple way to solve it is: marriedTo(martin, annie). ... married(X,Y) :- marriedTo(X,Y). married(X,Y) :- marriedTo(Y,X). Then there are plenty of other ways, implementations and semantics that came up to solve the problem of infinite

What does the s() predicate do in Prolog?

耗尽温柔 提交于 2019-12-21 11:55:31
问题 I have been trying to learn Prolog, and am totally stumped on what the predicate s() does. I see it used often and there is so little resources on the internet about Prolog that I cannot find an answer. Ex. /* sum(Is,S) is true if S is the sum of the list of integers Is. */ sum([],0). sum([0|Is],S):-sum(Is,S). sum([s(I)|Is], s(Z) ):-sum([I|Is],Z). 回答1: s/1 does not do anything in itself, and it's not really a predicate. They are just terms, a representation of the successor of their argument.