我正在使用SWI-Prolog提供的JPL编写Java应用程序,从Java调用Prolog.
我正在使用Eclipse作为IDE.我不知道如何启动我在网上找到的这个例子:
这里是java代码:
package prolog;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import jpl.Atom;
import jpl.Compound;
import jpl.Variable;
import jpl.Term;
import jpl.Query;
import jpl.JPL;
@SuppressWarnings({ "unchecked", "deprecation", "serial" })
public class JavaProlog extends JFrame {
JButton startButton = new JButton("Start");
JTextArea textArea = new JTextArea("A Diagnostic Expert System \n" +
"for respiratory diseases and lung.");
/**
*/
JavaProlog(){
Container cp=getContentPane();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation (200,200);
setSize (300,200);
setLayout (new …Run Code Online (Sandbox Code Playgroud) 我有一些.pl文件,我想从python脚本调用其中声明的谓词。我怎样才能做到这一点?
例如, test.pl
rD( [], Ans, Ans ).
rD( [X|Xs], Ans, Acc ) :-
member( X, Acc ),
rD( Xs, Ans, Acc ), !.
rD( [X|Xs], Ans, Acc ) :-
\+member( X, Acc ),
append( Acc, [X], AccNew ),
rD( Xs, Ans, AccNew ), !.
Run Code Online (Sandbox Code Playgroud)
像
?- rD( [1,2,3,4,5,4], X ).
X = [1, 2, 3, 4, 5].
Run Code Online (Sandbox Code Playgroud)
我想以rD某种方式从python脚本调用并在结果变量中获取答案
result
[1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)
ps:这只是一个例子,我不想重写当前的Prolog程序。
当我按下向上/向下键时,我应该得到 - 就像 unix 一样 - 之前的命令,但我得到的是:
Welcome to SWI-Prolog (Multi-threaded, 32 bits, Version 6.0.0)
Copyright (c) 1990-2011 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.
For help, use ?- help(Topic). or ?- apropos(Word).
1 ?- ['nc'].
% nc compiled into nc 0.00 sec, 84 clauses
true.
2 ?- listing.
true.
Run Code Online (Sandbox Code Playgroud)
(我按“向上”箭头键返回“列表”命令……然后……)
3 ?- **^[[A**
Run Code Online (Sandbox Code Playgroud)
我从源代码编译,没有任何额外的“配置”或“制作”参数。
在以前版本的 swi-prolog …
我开始学习Prolog之后的Prolog 编程:使用ISO标准.在介绍到,他们所作的断言语言的7页:"在序言答案没有被用来指没有与问题相结合,以记住,这是非常重要的.没有是不一样的错误." 那么为什么SWI-Prolog使用false和true语句代替yes或no?
我似乎总是很难写DCG来解析输入文件.但它似乎应该很简单?有什么提示或技巧可以考虑这个问题吗?
举一个具体的例子,假设我要解析一个fasta文件.(https://en.wikipedia.org/wiki/FASTA_format).我想在回溯上阅读每个描述和每个序列.
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- portray_text(true).
:- set_prolog_flag(double_quotes, codes).
:- set_prolog_flag(back_quotes,string).
fasta_file([]) -->[].
fasta_file([Section|Sections]) -->
fasta_section(Section),
fasta_file(Sections).
fasta_section(Section) -->
fasta_description(Description),
fasta_seq(Sequence),
{Section =.. [section,Description,Sequence]}.
fasta_description(Description) -->
">",
string(Description),
{no_gt(Description),
no_nl(Description)}.
fasta_seq([]) --> [].
fasta_seq(Seq) -->
nt([S]),
fasta_seq(Ss),
{S="X"->Seq =Ss;Seq=[S|Ss]}.
nt("A") --> "A".
nt("C") --> "C".
nt("G") --> "G".
nt("T") --> "T".
nt("X") --> "\n".
no_gt([]).
no_gt([E|Es]):-
dif([E],">"),
no_gt(Es).
no_nl([]).
no_nl([E|Es]):-
dif([E],"\n"),
no_nl(Es).
Run Code Online (Sandbox Code Playgroud)
现在这显然是错误的.我想要的行为是
?-phrase(fasta_section(S),">frog\nACGGGGTACG\n>duck\nACGTTAG").
S = section("frog","ACGGGGTACG");
S = section("duck","ACGTTAG");
false.
Run Code Online (Sandbox Code Playgroud)
但是,如果我做了phrase(fasta_file(Sections),">frog\nACGGGGTACG\n>duck\nACGTTAG). Sections与一个/ 2的列表统一,这是我想要的,但我当前的代码似乎很hacky-我如何处理换行符例如.
假设我正在研究这个玩具示例(问题的重点显然不是解决这个示例):
p([]).
p([H|T]) :- H = 0, call_predicate(p,T).
call_predicate(Name,Arg) :- call(Name,Arg).
Run Code Online (Sandbox Code Playgroud)
到目前为止,一切都很好。现在假设我想添加一个call_predicate/1不需要谓词名称的谓词:
call_predicate(Arg) :- Name = ??, call(Name,Arg).
Run Code Online (Sandbox Code Playgroud)
这样我就可以使用 in p: call_predicate(T),隐式地知道我想调用同名的谓词。
那么问题是,在知道这是调用的谓词的名称的情况下,如何p从 中检索名称?call_predicate/1call_predicate/1
一个类似的问题是,如果它比第一个更容易,我如何检索执行中一次所处的当前谓词的名称?
我想找到argmax(x,y,z)-1/2(20x ^ 2 + 32xy + 16y ^ 2)+ 2x + 2y.
受制于:x> = 0,y> = 0,z> = 0且-x-y + z = 0.
我知道偏导数设置为0是:
-20x-16y + 2 = 0和-16x-16y + 2 = 0
所以我们可以得x = 0和y = 1/8,z = 1/8.
我如何在Swi-prolog中做到这一点?我看到有用于线性求解的库单形,但这是一个二次问题,但偏导数不是.(我有一点困惑!)
这就是我所拥有的:
:- use_module(library(simplex)).
my_constraints(S):-
gen_state(S0),
constraint([-20*x, -16*y] = 0, S0, S1),
constraint([-16*x,-16*y] = 0, S1,S2),
constraint([x] >= 0,S2,S3),
constraint([y] >= 0,S3,S4),
constraint([z] >= 0,S4,S5),
constraint([-x-y+z] = 0,S5,S).
?- my_constraints(S), variable_value(S,x,Val1),variable_value(S,y,Val2).
false.
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个在SWI-Prolog中在运行时生成新约束的程序.is_true([A,means,B])旨在在运行时生成另一个约束:
:- use_module(library(chr)).
:- chr_constraint is_true/1.
is_true([A,means,B]) ==> (is_true(A) ==> is_true(B),writeln('asserted')).
is_true([[A,is,true],means,[A,is,not,false]]).
is_true([something,is,true]).
Run Code Online (Sandbox Code Playgroud)
但是当我输入这些查询时,is_true约束似乎没有效果.is_true([something, is, not, false])不归还true:
?- is_true([something,is,true]).
true .
?- is_true([something,is,not,false]).
is_true([something, is, not, false]).
Run Code Online (Sandbox Code Playgroud)
在控制台中断言约束似乎没有任何影响:
?- asserta(is_true(A>B)==>(is_true(B<A),writeln("asserted"))).
true.
?- is_true(4>3).
is_true(4>3).
Run Code Online (Sandbox Code Playgroud)
还有另一种在运行时定义新CHR约束的方法吗?
我正在尝试开发有关使用 swi-prolog 的常见问题(常见问题解答)。我使用桌面版 swi-prolog(AMD64,多线程,版本 8.2.3)。\n常见问题解答中的问题和答案是用土耳其的母语编写的。当我运行代码文件(k-base.pl 和 user.pl)时,土耳其语字符 /like \xc5\x9f-\xc4\x9f-\xc3\xbc-\xc3\xb6/ 看起来已损坏。\n我想知道是否有utf-8 代码文件的任何语法或 swi-prolog 桌面中针对此语言问题的任何设置。
\n我正在通过“代码来临”挑战来学习 Prolog。
以下是《代码降临 2021》第 7 天的剧透:
目标是:给定一个自然数列表n_1,..., n_k,找到
min_(x\in \N) \sum_i=0^k |x - n_i|
Run Code Online (Sandbox Code Playgroud)
在下面的代码中norm1计算被加数,cost计算给定 处的总和x,并lowest_cost计算所有 s 的最小值x。
norm1(X, Y, N) :- N #= abs(X - Y).
cost(Nums, X, Cost) :-
max_list(Nums, MaxX),
min_list(Nums, MinX),
X in MinX..MaxX,
maplist(norm1(X), Nums, Costs),
sum(Costs, #=, Cost).
lowest_cost(Nums, Cost) :-
cost(Nums, X, Cost)
once(labeling([min(Cost)], [X, Cost])).
Run Code Online (Sandbox Code Playgroud)
一些示例查询:
?- lowest_cost([10,11,12], Cost).
Cost = 2.
?- cost([2,4,6,8], 4, Cost).
Cost = 8. …Run Code Online (Sandbox Code Playgroud)