swi-prolog

Implementing partial evaluation in SWI-Prolog

为君一笑 提交于 2019-12-08 17:43:20
问题 I'm writing a partial-evaluator for Prolog queries. I tried to expand a query using expand_goal/2, but it simply unifies the Input with the Output in this case: :- initialization(main). main :- Input=is_between(1,A,3),expand_goal(Input,Output),writeln(Output). is_between(A,B,C) :- B>A,B<C. I also tried using term_expansion/2, but this causes the program to fail: :- initialization(main). main :- Input=is_between(1,A,3),term_expansion(Input,Output),writeln(Output). is_between(A,B,C) :- B>A,B<C.

SWI-Prolog Editor for OS X

拈花ヽ惹草 提交于 2019-12-08 08:56:42
问题 I was searching for a SWI-Prolog editor for Mac OS X but i could not find one, so is there one or do I have to use another editor to build Prolog files? Thanks 回答1: I do not use mac OS-X myself, but there are two very good SWI-Prolog editors that are cross-platform (thus also work on OS-X): PDT works with the Eclipse IDE. It has good syntax coloring, an in-IDE Prolog console, a dynamically generated visualization of the call graph, to name only a few of the many features it has. See http:/

Processing XML based DSL

微笑、不失礼 提交于 2019-12-08 05:38:46
问题 SWI-Prolog has plenty to offer as a generalized XML processor. library(sgml) for read/write XML structured formats, library(xpath) for navigation and more... but if I attempt to read a SVG, that it is valid XML, I get the message: ERROR: SGML2PL(xml): ...my_file...svg:2: file "...myfile.path.../http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" does not exist false. Does anyone has an hint on this? I'd also want to rewrite the eventually modified file content preserving of course the

Printing the source code of a predicate in Prolog

扶醉桌前 提交于 2019-12-08 01:59:45
问题 I'm writing a Prolog meta-interpreter that needs to obtain the source code of a predicate that I defined. I thought this would be possible using expand_term/2 , but it returned a recursive data structure instead of the predicate's source code: :- initialization(main). main :- expand_term(quadratic_formula(X,A,B,C) :- Z,Z),writeln(Z). %This prints @(S_1,[S_1=(quadratic_formula(_3068,_3090,_3112,_3134):-S_1)]) instead of the predicate's source code. quadratic_formula(X,A,B,C) :- X is -B + sqrt

Parsing command line arguments

限于喜欢 提交于 2019-12-07 10:14:11
问题 I'm very confused with prolog, it's way different to any language I've ever used (many languages) How do I go about getting argv[0] from: current_prolog_flag(argv, Argv), write(Argv). Now if I tried to type Argv[0] or Argv(0) or Argv<0> it fails.. this leaves me with no clue and very little help from the documentation.. it seems that they expect you to already be a prolog expert :D Another question, how would I assign Argv[0] to a variable so I can print it later using "write" ? 回答1: Prolog

Optimisation in swi prolog

荒凉一梦 提交于 2019-12-07 06:22:42
问题 Say I want to find argmax(x,y,z) -1/2(20x^2+32xy +16y^2)+2x+2y. subject to: x>=0, y>=0,z>=0 and -x-y+z =0. I know the partial derivatives being set to 0 is : -20x-16y+2=0 and -16x-16y+2 =0 so we could have x= 0 and y =1/8 and z=1/8. How would I do this in Swi-prolog? I see that there is library simplex for linear solving, but this is a quadratic problem but the partial derivatives are not. (I am a bit confused!) This is what I have: :- use_module(library(simplex)). my_constraints(S):- gen

Printing the source code of a predicate in Prolog

徘徊边缘 提交于 2019-12-06 09:17:33
I'm writing a Prolog meta-interpreter that needs to obtain the source code of a predicate that I defined. I thought this would be possible using expand_term/2 , but it returned a recursive data structure instead of the predicate's source code: :- initialization(main). main :- expand_term(quadratic_formula(X,A,B,C) :- Z,Z),writeln(Z). %This prints @(S_1,[S_1=(quadratic_formula(_3068,_3090,_3112,_3134):-S_1)]) instead of the predicate's source code. quadratic_formula(X,A,B,C) :- X is -B + sqrt(B*B-4*A*C)/2*A; X is -B - sqrt(B*B-4*A*C)/2*A. Is there some other way to obtain the source code of a

SWI Prolog ensure_loaded error

我的梦境 提交于 2019-12-06 09:01:34
问题 I am using SWI Prolog for a mathematical logic book and the book provided source code for some of the algorithms in Prolog. The problem is that when I try to load a file, the interpreter just prompt something like: load_files/2: No permission to load source `**' (Non-module file already loaded into module **; trying to load into io) I looked into the source code and found that most of the files start with: :- module(**,[***]). followed by user:file_search_path(common,'../common'). :- ensure

How do I show the results of pattern-matching goals in SWI-Prolog from a shell invocation?

纵然是瞬间 提交于 2019-12-06 08:27:52
I am wondering how one gets output from SWI-Prolog when invoking it from the shell. Say I have a simple knowledge base, kb.pl : dad(elvis, lisaMarie). dad(john, julian). I can invoke SWI-Prolog from the shell: $ swipl --quiet -s kb.pl -t listing and a listing of my knowledge base is printed to stdout . If I try this: $ swipl --quiet -s kb.pl -t "dad(elvis, X)" $ echo $? 0 No output is printed, but I know that it found matches because I get zero when I then query for the return code. Similarly: $ swipl --quiet -s kb.pl -t "dad(morrisey, X)" $ echo $? 1 Shows that that the Prolog is correctly

read line to atomic list in prolog

感情迁移 提交于 2019-12-06 05:07:16
问题 I need to read any line (from user_input) into an atomic list, e.g.: Example line, which contains any ASCII chars. into: [Example,'line,',which,contains,any,ASCII,'chars.'] what I've got so far: read_line_to_codes(user_input, Input), atom_codes(IA,Input), atomic_list_concat(AlistI,' ',IA). but that only works w/ single words, because of atom_codes. read/2 also complains about spaces, so is there any way to do this? oh and maybe then splitting at comma into 2d-lists, appending the dot