从终端查询不会打印任何东西 [英] Querying from the terminal doesn't print anything

查看:220
本文介绍了从终端查询不会打印任何东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在命令行中运行时,此

  swipl -gwrite(42)-thalt

按预期打印42到 STDOUT p>

但是,

  swipl -gX = 42-t halt

不打印任何东西,只是返回。



如何让它打印在REPL中打印的内容(即 X = 42 )?



注意:这是在Windows终端。

解决方案

如预期的那样, X = 42 本身会产生没有任何输出,因为(=)/ 2 是一个完全纯的谓词不会产生任何副作用本身。这是Window,OSX和所有其他操作系统的情况。



即使有一种方式获取和重定向toplevel输出本身,事实仍然是, SWI  toplevel可能会发生变化,您不能依赖未来版本以与现在相同的方式运行。长期来说,你可能会更好地滚动自己的toplevel 并产生你想要的输出。



这不是那么难以滚动自己的toplevel。诀窍主要是在阅读术语时使用 variable_names / 1 选项,以便您可以跟踪要显示的变量名称在答案。这是一个非常简单的开始:

 
repl: -
read_line_to_codes(current_input,Codes) ,
read_term_from_codes(Codes,Term,[ variable_names(NameVars)]),
call(Term),
report_bindings(NameVars)。
repl: - repl。

report_bindings(NameVars): -
phrase(bindings(NameVars),Bs),
format(〜s,[Bs])。

bindings([]) - > []。
bindings([E]) - > name_var(E)。
bindings([E1,E2 | Rest]) - > name_var(E1),,\\\
,bindings([E2 | Rest])。

name_var(Name = Var) - >
format _(〜w =〜q,[Name,Var])。

format_(Format,Ls) - >
call(format_codes(Format,Ls))。

format_codes(Format,Ls,Cs0,Cs): -
格式(代码(Cs0,Cs),格式,Ls)。

示例:

 

|: X = 4,在(1,3,Y)之间。
X = 4,
Y = 1
true;
X = 4,
Y = 2
true;
X = 4,
Y = 3
true;
|: X = 7。
X = 7



请注意, variable_names / 1 / code>选项对于以这种方式读取术语至关重要,并且由于ISO标准化工作,越来越多的实现为 read_term / 2 相关谓词。



这种读取变量名称的能力是实现便携式Prolog toplevel的需求



我为你留下的主要练习是检查引用是否在所有情况下是正确的(如果需要),以产生答案,以便它们总是可以粘贴回到终端。要将此扩展为剩余约束,请使用 copy_term / 3 call_residue_vars / 2 您可以附加到绑定的约束。


When ran in the command line, this

swipl -g "write(42)" -t "halt"

prints 42 to STDOUT as expected.

However, this

swipl -g "X = 42" -t "halt"

does not print anything, it simply returns.

How do I get it to print what it prints in the REPL (that is, X = 42)?

Note: this is in a Windows terminal. Let me know if this actually works in a Linux terminal.

解决方案

As expected, X = 42 by itself produces no output whatsoever, because (=)/2 is a completely pure predicate that does not yield any side effects by itself. This is the case on Window, OSX and all other operating systems.

Even if there were a way to obtain and redirect the toplevel output itself, the fact remains that the SWI toplevel is subject to change and you cannot rely on future versions to behave in the same way as it does now. Long term, you will likely be better off to roll your own toplevel and produce exactly the output you want.

It is not so hard to roll your own toplevel. The trick is mainly to use the variable_names/1 option when reading terms, so that you can keep track of the variable names that you want to show in answers. Here is a very simplistic start:

repl :-
        read_line_to_codes(current_input, Codes),
        read_term_from_codes(Codes, Term, [variable_names(NameVars)]),
        call(Term),
        report_bindings(NameVars).
repl :- repl.

report_bindings(NameVars) :-
        phrase(bindings(NameVars), Bs),
        format("~s", [Bs]).

bindings([])           --> [].
bindings([E])          --> name_var(E).
bindings([E1,E2|Rest]) --> name_var(E1), ",\n", bindings([E2|Rest]).

name_var(Name=Var) -->
        format_("~w = ~q", [Name,Var]).

format_(Format, Ls) -->
        call(format_codes(Format, Ls)).

format_codes(Format, Ls, Cs0, Cs) :-
        format(codes(Cs0,Cs), Format, Ls).

Example:

?- repl.
|: X = 4, between(1, 3, Y).
X = 4,
Y = 1
true ;
X = 4,
Y = 2
true ;
X = 4,
Y = 3
true ;
|: X = 7.
X = 7

It is easy to modify this so that it works on terms that are specified as arguments.

Note that the variable_names/1 option is essential for reading terms in such a way, and thanks to the ISO standardization effort an increasing number of implementations provide it for read_term/2 and related predicates.

This ability to read variable names is a requirement for implementing a portable Prolog toplevel!

The main exercise that I leave for you is to check if the quoting is right in all cases and (if desired) to produce answers in such a way that they can always be pasted back on the terminal. To extend this to residual constraints, use copy_term/3 and call_residue_vars/2 to collect pending constraints that you can append to the bindings.

这篇关于从终端查询不会打印任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆