PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label swi-prolog. Show all posts
Showing posts with label swi-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

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
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