PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label prolog. Show all posts
Showing posts with label prolog. Show all posts

Sunday, October 30, 2022

[FIXED] How to send EOF on Prolog swipl REPL on Windows to close the pseudo user file?

 October 30, 2022     eof, prolog, prolog-toplevel, swi-prolog     No comments   

Issue

I'm using the swipl.exe Prolog REPL on Windows and trying to use the user pseudo file opened with [user]. but I can't figure out the key shortcut to leave the pseudo file:

c:\code>swipl.exe
Welcome to SWI-Prolog (threaded, 64 bits, version 8.0.3)

1 ?- [user].
|: hello :- format('Hello world~n').
|: ^Z

.

ERROR: user://1:9:3: Syntax error: illegal_character
|:
Action (h for help) ? ^C
c:\code>

Starting at the ^Z I typed the keys

  • CTRL+Z
  • ENTER (I expected here should have sent the EOF)
  • ENTER
  • .
  • ENTER (this shows an error, and puts me back into the pseudo file)
  • CTRL+C (this kills only one of the two swipl processes, leaving the console in a broken state with some keystrokes going to swipl and some going to cmd(!))

I know that on a blank line pressing CTRL+Z then ENTER normally works to send EOF, like if in more.com I type A ENTER B ENTER CTRL+Z ENTER everything works on my terminal.


If I run the swipl-win.exe GUI following the same key steps, immediately when I press CTRL+Z it closes the pseudo file and returns me to the top-level query:


?- [user].
|: hello :- format('Hello world~n').
|: 
% user://1 compiled 0.00 sec, 1 clauses
true.

?- hello.
Hello world
true.

?- 

What do I press to get [user]. to work in swipl.exe?


Solution

As you noticed, when you type [user], you're consulting a pseudo source file, which is made of terms. Therefore, simply type the term end_of_file. For example:

?- [user].
|: a.
|: b.
|: end_of_file.

% user://1 compiled 0.00 sec, 2 clauses
true.


Answered By - Paulo Moura
Answer Checked By - Robin (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, October 7, 2022

[FIXED] How to find the mode of a list in prolog?

 October 07, 2022     list, prolog, statistics     No comments   

Issue

Can someone please tell me how to find the mode of a list in Prolog?

I have tried this:

count_elt([], _, 0) :- !. 
count_elt([H|T], H, P) :- 
  count_elt(T, H, P1),
  P is P1 + 1, !. 
count_elt([_|T], E, P) :- count_elt(T, E, P). 

listMode([], 0) :- !. 
listMode([_], 1) :- !. 
listMode([H1|[H2|T]], M) :- 
  count_elt([H1|[H2|T]], H1, C),
  listMode([H2|T], C1),
  C > C1,
  M is C, !. 
listMode([_|[H2|T]], M) :- listMode([H2|T], M).

which only returns the maximum occurrences in the list. But I need to return the element which has the maximum occurrence (The most frequent value in the list).


Solution

Hope you already got number of suggestions. Here this is working code for you.

count_elt([],_,0):-!.
count_elt([H|T],H,C):-count_elt(T,H,C1),C is C1+1,!.
count_elt([_|T],E,C):-count_elt(T,E,C).

listMode([],0) :- !.
listMode([_],1) :- !.
listMode([H1|[H2|T]], M) :-count_elt([H1|[H2|T]],H1,C),listMode([H2|T],C1),C > C1,M is C,!.
listMode([_|[H2|T]],M) :- listMode([H2|T],M).

get_mode([H|T],M):-listMode([H|T],K),count_elt([H|T],M,K).


Answered By - Madusanka
Answer Checked By - Robin (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Sunday, September 18, 2022

[FIXED] How to print this in one line statement in Prolog

 September 18, 2022     printing, prolog, swi-prolog     No comments   

Issue

How can i print this string in prolog in the best way:

predicate([], L, Id, L2):-
   length(L2, N),
   write('The length '),
   write(Id),
   write(' is '),
   write(N),
   write(' elements.'),
   nl.

Solution

In SWI-Prolog, you can use the built-in predicate format/2:

predicate([], L, Id, L2):-
   length(L2, N),
   format('The length of ~w is ~w elements\n', [Id, N]).


Answered By - slago
Answer Checked By - Senaida (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, August 25, 2022

[FIXED] How to list the clauses inside a module in SWI-Prolog?

 August 25, 2022     module, prolog, reflection, swi-prolog     No comments   

Issue

SWI-Prolog has for example the library(dcgbasics) for use with DCGs.

While referencing the module is easy with use_module/1, e.g.

:- use_module(library(dcg/basics)).

trying to use listing/1 with it is not so easy.

?- listing(dcg:_).
true.

?- listing(dcgbasics:_).
true.

?- basics:listing.
true.

What is the correct way to get a listing of the clauses in library(dcg/basics)?


Follow up after answer given.

To list a specific clause, e.g. blanks//0 the query is

?- listing(dcg_basics:blanks).
blanks(A, B) :-
    blank(A, C),
    !,
    D=C,
    blanks(D, B).
blanks(A, A).

true.

Solution

Use either:

?- dcg_basics:listing.

Or:

?- listing(dcg_basics:_).

The first argument of use_module/1-2 is a file specification, not a module name. But listing the module contents requires the actual module name, which may be different (as it is the case here) from the module file basename. But how to find the module name from the file specification? In the particular case of SWI-Prolog:

?- absolute_file_name(library(dcg/basics), Path, [extensions([pl])]),
   module_property(Module, file(Path)).
Path = '/Users/pmoura/lib/swipl/library/dcg/basics.pl',
Module = dcg_basics.


Answered By - Paulo Moura
Answer Checked By - Willingham (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Saturday, July 9, 2022

[FIXED] What is the difference between the keyword is and = in prolog?

 July 09, 2022     keyword, operators, prolog     No comments   

Issue

I'd appreciate it if someone could elaborate on the difference between the is keyword and the = operator in prolog. I saw this discussion in == and =, but it excludes is. The documentation talks about an unclear to me "unbound left operand." Can anyone elaborate?

I have a following example of is:

age(Person,X) :-
birth_year(Person,Y1),
current_year(Y2),
X is Y2-Y1. 

Is the difference assignment vs comparison? Any help is appreciated!

Edit: What is the relationship between == and is? I am not asking the relationship of == and =, unless I have a misunderstanding of the aforementioned relationship.


Solution

As usual, a bit of poking around helps:

?- X = 2 + 1. % unify X with 2 + 1
X = 2+1.

?- X = 2 + 1, write_canonical(X). % how does Prolog see X?
+(2,1)
X = 2+1.

?- is(X, +(2,1)). % evaluate the term +(2,1) as an arithmetic expression
                  % and unify X with the result
X = 3.

The point about X being a free variable is that since the result of the arithmetic expression is unified with it, you might get surprises when the terms are not the same even though the arithmetic expression seem as if they should be:

?- 1+2 is 2+1. % Evaluate 2+1 and try to unify with +(1,2)
false.

?- 1 is (1.5*2)-2. % Evaluates to 1.0 (float), unify with 1 (integer)
false.

?- 1+2 =:= 2+1.
true.

?- 1 =:= (1.5*2)-2.
true.

And please keep in mind that both =/2 and is/2 are predicates. They can also be just atoms, so they can also be names of functors. Both happen to be declared as operators. I don't think either should be called a "keyword".



Answered By - user1812457
Answer Checked By - Senaida (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, May 13, 2022

[FIXED] How do I append lists in Prolog?

 May 13, 2022     append, concatenation, list, prolog     No comments   

Issue

How do I append lists in Prolog? I've searched on the Internet and I found this (from http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_7.html)

append([X|Y],Z,[X|W]) :- append(Y,Z,W).  
append([],X,X).

So it gets the Z by removing the elements of [X|Y] in [X|W]. But how do I append two lists together?

Example,

appendlist([1,2],[3,4,5],X).

The result will be X = [1,2,3,4,5].

Also I don't know what happening in the recursion. (I traced it but didn't understand)

EDIT: What I want to know is how it should be coded to function like the predefined append() in Prolog.


Solution

The code as you've posted it is (almost) OK. The order of clauses just needs to be swapped (in order to make this predicate definition productive, when used in a generative fashion):

append( [], X, X).                                   % (* your 2nd line *)
append( [X | Y], Z, [X | W]) :- append( Y, Z, W).    % (* your first line *) 

This defines a relationship between the three arguments, let's say A, B and C.

Your first line says, " C is the result of appending A and B if A and C are non-empty lists, they both have the same head (i.e. first element), and the tail of C is the result of appending the tail of A with the same 2nd argument, B".

  a        a
  ----------
  b        b
  c        c
  .    d   d
       e   e
       .   .

Or from left to right:

         a | b c .
           |     d e .
         a | b c d e .

append(         [], 
                 Z,
                 Z ).       
append( [X | Y   ],
                 Z,
        [X |         W ] ) :- append(
             Y,  Z,  W).

Think about it, it makes perfect sense. What it does is, we want to define the append/3 relationship, and we know what we want it to be, so we just write down some obvious facts about it that we want it to fulfill, the laws that it must follow if you will.

So assuming we have this code already defined for us, what laws must it follow? Obviously, appending a tail of some list with another list gives us a tail of result of appending the full list with that 2nd list.

This defines how we "slide along" the first list. But what if there's nowhere more to slide? What if we've reached the end of that list? Then we've arrived at the empty list, and appending an empty list with another list gives us that list as the result. Obviously. And that's what that 2nd line in your code is telling us, it says, "appending an empty list with another list produces that list as the result".

Amazingly, having written down these two laws that append/3 must follow, is the same as writing down the definition itself.

addition: this explains it from a declarative point of view; do check out an answer by m09 which shows it more from the operational point of view.



Answered By - Will Ness
Answer Checked By - Mary Flores (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing