prolog,counter,clpfd,restrictions,labeling , Manipulate labeling/2 output PROLOG

Manipulate labeling/2 output PROLOG


Tag: prolog,counter,clpfd,restrictions,labeling

I'm developing a PROLOG program with restrictions to find the solutions for a certain game.

Question#1: For debugging purposes, I want to count the number of possible solutions of the game. Is there any way to do this via the labeling/2 predicate?

My goal is to count the number of possible combinations between the cubes (Instant Insanity):

insanity2(Vars) :-
        Vars = [F1,B1,R1,L1,U1,D1,

        /* Colors range (4 colors) */
        domain(Vars, 1,4),

        /* Restrictions */
        %Color restrictions
          % Cube #1
          % Cube #2
          % Cube #3
          % Cube #4

        %Logic restrictions
        F1#\=F2, F1#\=F3, F1#\=F4, F2#\=F3, F2#\=F4, F3#\=F4,
        B1#\=B2, B1#\=B3, B1#\=B4, B2#\=B3, B2#\=B4, B3#\=B4,
        R1#\=R2, R1#\=R3, R1#\=R4, R2#\=R3, R2#\=R4, R3#\=R4,
        L1#\=L2, L1#\=L3, L1#\=L4, L2#\=L3, L2#\=L4, L3#\=L4,
        labeling([], Vars).

Question#2: Is it possible to manipulate labeling/2 output, in order to rearranje it in a proper way?


For your question #1 you can use the findall/3 predicate:


| ?- X=[X1,X2], domain(X,0,2), findall(X, labeling([],X), Xl ), length(Xl,L).            
X = [X1,X2],
Xl = [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]],
L = 9,
X1 in 0..2,
X2 in 0..2 ? 
| ?- 


What does `\+` signify as an operator in prolog?

I have encountered the symbol \+ in prolog and am not sure as to what it signifies. I have gone through the prolog documentation and am either not being too resourceful or have not really noticed it. Thank you for your answer! ...

SWI Prolog pass a goal with non-zero arity through the command line arguments

SWI Prolog allows the execution of a goal before top level with non zero arity as follows swipl -s -g start however when a predicate with non zero arity is specified as follows swipl -s -g start(1) It gives an error syntax error near unexpected token '(' What...

ANSI escape characters in gprolog

Trying to print bold and underlined text in prolog but can't write them write('\033[1mbold\033[0m') Makes this (expected) error: syntax error: \ expected in \constant\ sequence What's the correct way to do it with gprolog ? Maybe with format ?...

prolog rules as arguments

I'm building an expert system shell based on Luger & Stubblefield's ExShell. In their system, they define rules in the following way: rule((Goal :- (Premise)), CF_Rule). Ignore the CF_Rule. An example of this syntax is: rule((fix(Advice) :- (bad_component(X),fix(X, Advice))), 100). I want to add an OR in certain rules, but...

How can I call a function random inside other function in prolog?

I'm trying to call the random function inside another function. For example I want to do this assert(fact(random()). But it does not work. How can I insert a random number this way? Thanks.

Prolog: Summing elements of two lists representing an integer(restrictions inside not regular sum!!)

I was solving a problem: A list is representing an integer say 12345 by L=[12,34,5] each element should be from 0 to 99.The exercise is to write a function (sum) that sums two lists and gives the equivalent list of their sum which is representing the sum of two integers....

Translating a list to another list in prolog

I Tried to write a simple code in Prolog which translate a list to another list. for instance, if we call listtrans([a,b,c],L), L will become [1,2,3]. (a,b,c is replaced with 1,2,3). But i faced with a syntax error in last line. what is the problem? here is my code: trans(a,1)....

Why was the ISO standard for prolog a failure? [closed]

In the introduction here, it states that the ISO standard for Prolog was a failure. Can anyone elaborate on this?

Syntax Error, Operator Expected

I am trying to query the MS SQL Server database using Prolog. Prolog Code lemmas:- odbc_query('my_db', 'SELECT * ,case when ActualCost<EstimatedCost then 'true' else 'false' end as Value from Work_Order ' ). It gives an error that operator is expected after 'true' . Note: The Query works in SQL-Server but...

Wait the end of a predicate Prolog

I have a predicate list_moves(Move), that gives all the possible moves for a game. Then I have the following predicates : find_best_move:- nb_setval(best,0), list_moves(NewMove), nb_getval(best,OldMove), better(OldMove,NewMove). better(OldMove, NewMove):- NewMove > OldMove, nb_setval(best,NewMove), !. better(_,_). get_best_move(Move):- find_best_move, nb_getval(max,Move). I want to get only one move when I call get_best_move(Move) : the...

Why I can't get an answer for the Ship Puzzle with Prolog?

I need to solve the Ship Puzzle problem with using Prolog. Here are the facts. There are 5 ships. 1. The Greek ship leaves at six and carries coffee. 2. The Ship in the middle has a black chimney. 3. The English ship leaves at nine. 4. The French ship...

How to save the highest integer in UILabel

I have one label, named pointLabel, two buttons, named increases and decrease. When you click "increase" button, it will increase the point in pointLabel. and you click "decrease" button, it will reduce the point. It will end when it reaches 0 point in pointLabel. While the point is being increased...

Prolog rules and query

I need some help to find the rules and/or query for knowledgebase in Prolog with information about Costumers in a supermarket. For example I have: Customer(Name,Age,Sex,Wage). customer(John,30,M,2000). customer(Mary,35,F,2500). customer(Mark,40,M,2500). invoice(Number, CostumerName, Product, Price). invoice(001, John, Potatoes, 20). invoice(002, John, Tomatoes, 10). invoice(003, Mary, Soap, 50). invoice(004, Mark, Potatoes, 20). invoice(005,...

make all elements in list equal with minimal cost

I am trying to create a prolog program that allows to convert a list into a list with the same length consisting of only 1 element from the original list. This element must be chosen in such a way that a minimal number of elements from the original list needs...

Freezing goal in prolog

I want to freeze my goal until some variable, for example list, is unbounded, right now I have sieve(N,L) :- freeze(Aux,sieve(N,L,[],Aux)), numlist(2,N,Aux). sieve(N,L,R,[H|T]) :- freeze(X, X mod H =\= 0 ; X == H), findall(X,select(X,T,_),P), sieve(N,L,[H|R],P). sieve(_,L,L,[]). But it stop after some operations and waits forever. Could someone tell me...

Prolog binding arguments

In sicstus prolog, there's a predicate: maplist(:Pred, +List) Pred is supposed to take just one argument - List element. How can I pass a 2-argument predicate, with first argument defined? In other languages it would be written as: maplist(pred.bind(SomeValue), List) ...

prolog misunderstanding. Split list into two list with even and odd positions. where is my mistake?

I'm trying to split a list into two other list. List Even with the elemnts in even possition and a List Odd with the others. I've been looking over iinternet a solution, but all seem to me like chinese, i can not understand the process. So this is what i've...

Avoid recursion in predicate

I have the following question for the following predicate, how can i drop the recursive call f(T,S1) from both predicates. Flow model: (i,o) f([],0). f([H|T],S):- f(T,S1), S1 > 2,!, S is S1 + H. f([_|T],S):- f(T,S1), S is S1 + 1. This is a trick question, and I am not...

List Read and Pass it to Right Clause Functor In Prolog

This is my code: functor1(Action,[Action|_]) :- functor2(container(Action)). functor1(Action,[_|OtherActions]) :- functor1(Action,OtherActions). functor2(container([Abc,Def])):- append([Abc],[Def],Z),write(Z). Now, when I try to query, functor1(X,[[first, sign],[second, sign]]). What I expect with this query is that firstly X should read X = [first,sign] and then pass to functor2's container so that my functor2 can come into play....

How to add a print command to DCG syntax

I want to add a print command to my DCG syntax, here is what I have: program( (R0 --> R) ) --> [begin],instructs(( R0 --> R )),[end]. instructs( ( R0 --> R ) ) --> instr(( R0 --> R )). instructs( ( R0 --> R ) ) --> instr(( R0...

accessing program listing in prolog

I'm having some strange (or not so strange) problems defining variables in SWI-Prolog. Example: I'd like to do something like below: :- initialization(main). main :- X = listing(main), write(X). but it's simply printing "listing(main)" ...

prolog traverse nonstandard tree left to right

I need to implement a predicate trav(Tree,List) that performs a left to right tree traversal; Where: Tree is defined by the structure node(left,right), where left and right can be either another node or any Prolog data item. List is the list of leaf node values in the tree. For example:...

Constraint not propagated upon instantiation of list members

I am building a parser and generator for dates and times. In an ordinary programming language these would be written separately. In Prolog+CLP(FD) I can write 1 predicate that does both :-) In my use case it often makes sense to parse a number of digits and convert the to...

prolog need to compute the tree size

I need to get the size of a tree using: size(Tree,Size) What I have so far is wrong, please advise! size(empty, Size). size(tree(L, _, R), Size) :- size(L, Left_Size), size(R, Right_Size), Size is Left_Size + Right_Size + 1. Output should produce: ?- size(node(1,2),X). X = 2. ?- size(node(1,[2,3,4]),X). X =...

Unwanted logical variable in Prolog output

I have to do a Prolog homework for College and I'm mostly done, but i keep getting a _G variable in the output. We need to compare a List - L - to a regular term - K -, and, if the item in the list is bigger than the...

Predicate that pick elements which are on list twice not less not more

I'm trying to write a predicate twice(El,L) which will return true. when El is on list exactly twice. Here is what I have: twice(El,L) :- select(El,L,L1), member(El,L1), \+ twice(El,L1). It works nice for twice(2,[1,2,2,3,4]) but for twice(X,[1,1,2,2,3,3]) it doubles every number X = 1 ; X = 1 ; X...

Prolog isomorphic graphs

Good night. Trying to solve the isomorphic graphs problem here. Assignment info: Determine whether 2 undirected graphs are isomorphic. No isolated vertices. Number of vertices is less than 30 Edges of graphs are given as predicates, i.e. e(1, 2). f(1, 2). I'm trying to use the following approach: For every...

gprolog: Getting a stacktrace after an exception

While using gprolog I often have exceptions without any kind of line numbers or context like this one: uncaught exception: error(instantiation_error,(is)/2) Without any kind of context. I know I can do a trace but it would take very long to debug it with trace since I need to execute a...

Count Edges over specific Period of Time in VHDL

For speedmeasurement of an electric motor I would like to count the amount of rising and falling edges of an encoder-input in a time-intervall of 10ms. To do this I have implementet a clock divider for my 40 MHz Clock as follows: entity SpeedCLK is Port ( CLK : in...

Making a counter with angular and timout

hey i'm trying to build a simple counter that shows in my html. it kind of works but it doesn't listen to the timeout and gives a bunch of errors. This is the error i'm getting TypeError: fn is not a function This is the javascript function and object: $scope.funFacts...

Prolog-iterating through list

Let's say i have list Xs = [a,b,c]. Now i want to iterate through all elements and call another function for this elements. My question is: how to do this using head and tail? I would be grateful for help.

jQuery counter insert commas to break up string

I have a counter that increments every x seconds, here's my code: var counter=22000000000; if(typeof(localStorage.getItem('counts'))!='object') { counter=parseInt(localStorage.getItem('counts')); } $(".count").html(counter); setInterval(function () { $(".count").html(counter); ++counter; localStorage.setItem('counts',counter); }, 18000); Because the starting number is so high it doesn't read well, see it here: Ideally instead of outputting 22000000000 I'd like it...

Counter not working after jumps - assembly language

For some reason, when i switch to mouse input switch back to keyboard input for my program, increasing and decreasing the counter has no effect. It works perfectly in the first loop where we input characters. Here is the program guys, any advice? look at whatspeed jump for reference after...

Keep strings that occur N times or more

I have a list that is mylist = ['a', 'a', 'a', 'b', 'b', 'c', 'c', 'd'] And I used Counter from collections on this list to get the result: from collection import Counter counts = Counter(mylist) #Counter({'a': 3, 'c': 2, 'b': 2, 'd': 1}) Now I want to subset this...

Solving ship puzzle using prolog

I need to solve the following problem using prolog. These are the facts provided. 1. The Greek ship leaves at six and carries coffee. 2. The Ship in the middle has a black chimney. 3. The English ship leaves at nine. 4. The French ship with blue chimney is to...

Prolog- singleton variable in branch warning

Hello here is my code in Prolog: arc(a,h). arc(b,c). related_to(X, Ys) :- setof(Y, arc(X, Y), Ys). cut([H|T],Y) :- check(H,Y), T = [] -> cut(T,Y). check(X,Y) :- related_to(X,Xs), member(Y,Xs) -> write('There is a road'); cut(Xs,Y). When I am trying to run check(a,b) it doesn't run. I get the message Singleton variable...

How to loop each array for Counter function inside a list using Python?

I want to use counter function to count for the occurrences of the value 0 for each array inside a list. from collections import Counter [Counter(x) for x in a] [Counter(x)[0] for x in a] Using the above code it only applies to example like: a = [array([-2, 0, 0]),...

How does the predicate 'repeat' create infinite choice points in prolog?

I do not understand how the predicate repeat evaluates to be true at all times and create infinite choice points. For example, when I call: :- repeat, reset, do(task,job). Thank you for your help! ...

What does the ++counter mean? [duplicate]

This question already has an answer here: ++someVariable Vs. someVariable++ in Javascript 4 answers I don't consider myself to be bad at programming, but there's been something troubling me since the past few days. int counter = 3; ++counter; Is the following code above the same as counter++;....

Solving constraints with string concatenations in Prolog

Here, I tried to solve some simple string constraints in Prolog. While this problem seems to be straightforward, the main predicate still does not print the value of FinalString, which should be "hello world". Is it possible for Prolog to solve constraints that contain string concatenations, like this one? :-...

Base 36 counter without I or O

we have a requirement to make our serial numbers Base 36 (0-9,A-Z). My initial thought was store the counter in decimal and convert to hex only when required for display. This makes the counting simple, however there is another requirement to not use I or O because it'll be confused...

PROLOG: Summing up the series procedure

I have been trying to work on this problem that I found in a book, but am not able to make sense of it in my head. The problem asks me to use series(N, Total) for this procedure; 1 + 1/2 + 1/3 + 1/4 + ... + 1/(N-1). Any...

How does pruning choice points in the code below make it more efficient (Prolog)?

In the code given below, there is the ! (cut) that prunes the choice point for efficiency. I am pretty certain that the reverse predicate and the agent_do_moves predicate are essential. solve_task(Task,Cost):- agent_current_position(oscar,P), solve_task_a(Task,[b(0,0,P)],[],R,Cost,_NewPos),!, % prune choice point for efficiency reverse(R,[_Init|Path]), agent_do_moves(oscar,Path). ...

How to get Prolog to explain your result beyond the true statement

I have the following facts and rules: flight(sea,msp). flight(msp,jfk). route(A,B) :- flight(A,B). route(B,A) :- flight(A,B). route(A,C) :- flight(A,B) , flight(B,C). when query for route(sea,jfk) I get a result true but what I wish to get is the explination: sea-->msp-->jfk this way I can tell not only that it's true but...

Backwards Chaining With Variables

I have been reading up about inference in Prolog/Datalog and while forward chaining seems fairly simple to grasp, I have some issues with backward chaining with any sort of complex example which isn't simply propositional or used to determine a true or false value. I was reading an article the...

What is the difference between _ and _variable in prolog?

I have encountered a lot of problems with a literal that starts with a _ such as _Peter. So what exactly would be the difference between: good(_,_). and good(_,_Peter). Thank you for answering my question!...

How to write number classifiers in prolog?

Playing with Prolog for the first time and while I thought I knew what it basically is good for, I find it hard to get anything done in it. So, I tried to find the easiest possible task and even fail to accomplish that. I think it is due to...

freeze for more than one variable

I tried to write a predicate that takes a list and converts it to a balanced tree. My code looks as follows: /* make_tree(list, tree) * * list: list with the elements of the tree in prefix order * tree: balanced tree of the elements in the list * */...

Prolog- Returning elements from facts

i encounter a problem with facts. Let's say i got 3 facts and check(X) question. fact(a,b). fact(b,c). fact(a,d). check(X):- //some calculation with fact() How to make above question to return list of elements for given X from all facts? For instance: check(a) would give result b and d. So i...

Get operator operands

I have list: List = [a=3, b=2, c=0]. For example, first element a=3, how can I get value Left = a, Right = 3. Left side is atom and right side is number....