swi-prolog

Generating subsets using length/2 and ord_subset/2

两盒软妹~` 提交于 2019-12-20 04:32:07
问题 I am a beginner in prolog. I tried this in swipl interpreter: ?- length(Lists, 3), ord_subset(Lists, [1, 2, 3, 4]). false. expecting to get all length-3 lists that are subsets of [1, 2, 3, 4] like [1, 2, 3] or [1, 2, 4]. Why do i get false? Notice: both length and ord_subset are builtin functions (or whatever they are called) in SWI-Prolog. 回答1: You don't get a solution because the ord_subset/2 predicate only checks if a list is a subset of another list; it does not generate subsets. Here is

Predsort/3 like msort/2

纵饮孤独 提交于 2019-12-19 18:28:14
问题 I would like to know is it possible to use predsort/3 without losing duplicate values? If not, that how should I sort this list of terms? Current sort function: compareSecond(Delta, n(_, A, _), n(_, B, _)):- compare(Delta, A, B). Result: predsort(compareSecond, [n(3, 1, 5), n(0, 0, 0), n(8, 0, 9)], X). X = [n(0, 0, 0), n(3, 1, 5)]. You see, that term n(8,0,9) is gone and that's not what I need. 回答1: predsort will remove duplicates, but it leaves it to the comparison predicate to define which

Verifying a signature chain SWI-Prolog

那年仲夏 提交于 2019-12-19 04:08:08
问题 This question is related to Opening and checking a Pem file in SWI-Prolog Once I have downloaded and opened the certificates how do I verify the signature chain? I have: :-use_module(library(http/http_client)). url('https://s3.amazonaws.com/echo.api/echo-api-cert-4.pem'). url_data1(Url,Certs):- http_open(Url,Stream,[]), all_certs(Stream,Certs), forall(member(C,Certs),my_validate(C)), close(Stream). all_certs(Stream,[C1|Certs]):- catch(load_certificate(Stream,C1),_,fail), all_certs(Stream

How to use list constructors (./2) in SWI-Prolog

∥☆過路亽.° 提交于 2019-12-18 08:53:06
问题 I am trying to use list constructor in SWI-Prolog, but am getting 'dict' expected error. For example, .(a, []) == [a]. ERROR: Type error: `dict' expected, found `a' (an atom) ERROR: In: ERROR: [11] throw(error(type_error(dict,a),_14808)) ERROR: [10] '$type_error'(dict,a) at /Applications/SWI-Prolog.app/Contents/swipl/boot/init.pl:3369 ERROR: [9] '$dicts':'.'(a,[],_14874) at /Applications/SWI-Prolog.app/Contents/swipl/boot/dicts.pl:46 ERROR: [8] '<meta-call>'(user:(...,...)) <foreign> ERROR:

How to use SWI-Prolog ./2 function?

牧云@^-^@ 提交于 2019-12-17 23:36:10
问题 Need example using SWI-Prolog ./2. The signature is .(+Int,[]) Also if there is a name for this operator it would be nice to know. Searching for '.' is senseless. The closest to a name is from SWI documentation section F.3 Arithmetic Functions List of one character: character code What I tried ?- X is .(97,[]). results in ERROR: Type error: `dict' expected, found `97' (an integer) ERROR: In: ERROR: [11] throw(error(type_error(dict,97),_5812)) ERROR: [8] '<meta-call>'(user:(...,...)) <foreign>

SWI-Prolog how to show entire answer (list)?

时光怂恿深爱的人放手 提交于 2019-12-17 16:47:26
问题 I'm trying to convert a string to a list of ascii-codes like so: 7 ?- string_to_list("I'm a big blue banana in space!", C). C = [73, 39, 109, 32, 97, 32, 98, 105, 103|...]. 8 ?- This doesn't give me the entire list as you can see, but I need it. This solution does not work: I can't press w since it gives me the answer and does a full stop. Neither does this: I can call the function alright, and it returns true, but the list still isn't fully displayed. 11 ?- set_prolog_flag(toplevel_print

SWI-Prolog - show long list

安稳与你 提交于 2019-12-17 06:49:47
问题 I'm using SWI-Prolog and I'm trying to print a list but if the list has more than 9 items - it look like that - [1, 15, 8, 22, 5, 19, 12, 25, 3|...] is there a way to show the whole list? 回答1: Have a look at: http://www.swi-prolog.org/FAQ/AllOutput.html The simple solution is to type w after the answer is given, i.e.: ?- n_queens_problem(10,X). X = [1, 3, 6, 8, 10, 5, 9, 2, 4|...] [write] X = [1, 3, 6, 8, 10, 5, 9, 2, 4, 7] After you have pressed the "w"-key "[write]" is displayed at the end

Read a file line by line in Prolog

为君一笑 提交于 2019-12-17 06:48:06
问题 I'd like to read a plain text file and apply a predicate to each line (the predicates contain write which does the output). How would I do that? 回答1: In SWI-Prolog, the cleanest solution is to write a DCG that describes what a "line" is, then call a predicate for each line. Use library(pio) to apply the DCG to a file. EDIT : As requested, consider: :- use_module(library(pio)). lines([]) --> call(eos), !. lines([Line|Lines]) --> line(Line), lines(Lines). eos([], []). line([]) --> ( "\n" ; call

SWI Prolog escape quotes

梦想的初衷 提交于 2019-12-14 01:32:52
问题 I need to put " " around a String in prolog. I get the input from another program and as it looks I can't escape the " in this program, so i have to add the " in prolog otherwise the prolog statement doesn't work. Thanks for your help! 回答1: For a discussion of strings see here, they are SWI-Prolog specific but use the same escape rules as atoms. There are many ways to enter quotes into an atom in a Prolog text: 1) Doubling them. So for example 'can''t be' is an atom, with a single quote as

SIGSEGV in DSO, mixed C/C++

百般思念 提交于 2019-12-13 17:36:34
问题 I'm using the SWI-Prolog foreign language interface for C++, attempting to integrate some other resource. It mostly works, but any attempt to throw an exception result in a SIGSEGV. Exceptions are routinely used in validation of user parameters, and thus are a fundamental part of the interface. I'm compiling SWI-Prolog from source (via supplied script), and the CXX flags are -c -O2 -gdwarf-2 -g3 -Wall -pthread -fPIC I'm using the same flags to compile my C++ code, that is assembled in a .so,