【问题标题】:Python: swiplserver load prolog filePython:swiplserver 加载 prolog 文件
【发布时间】:2022-01-08 11:20:36
【问题描述】:

基于此post 尝试按照提示设置一些 swiplserver

from pyswip import Prolog, Functor, Query
from swiplserver import PrologMQI, PrologThread


prolog = Prolog()
prolog.consult("Prolog/logic.pl")

with PrologMQI() as mqi:
    with mqi.create_thread() as prolog_thread:
        result = prolog_thread.query("member(X, [color(blue), color(red)])")
        print(result)

我现在的问题,我不知道如何加载“logic.pl”:

:-op(800, fx, ¬).
:-op(801, xfy, ∧).
:-op(802, xfy, ∨).
:-op(803, xfy, →).
:-op(804, xfy, ↔).
:-op(800, xfy, #).


m_Proposition_Binary_x_y(X ∨ Y, X, Y).
m_Proposition_Binary_x_y(X ∧ Y, X, Y).
m_Proposition_Binary_x_y(X → Y, X, Y).
m_Proposition_Binary_x_y(X ↔ Y, X, Y).

m_Proposition(X) :-
    m_Proposition_Atom(X).
m_Proposition(Binary) :-
    m_Proposition_Binary_x_y(Binary, X, Y),
    m_Proposition(X),
    m_Proposition(Y).
m_Proposition(¬ X) :-
    m_Proposition(X).



m_Proposition_Atom(p).
m_Proposition_Atom(q).

进入该服务器设置,所以我可以使用以下语句:

intersection([A,(A→B)], [p, (p→q)], Aim).

有没有办法使用类似于 pyswip 中的咨询命令的“logic.pl”?

【问题讨论】:

    标签: python file prolog load swi-prolog


    【解决方案1】:

    [复制自swi-prolog.discourse.group] 上的同一个交叉发布问题

    我认为学习如何使用 swiplserver 的一个好方法是首先找出使用 Prolog 命令行(即“顶级”)工作的正确查询集,然后将其转换为 Python。

    由于logic.pl 包含多字节字符,并且我使用的是默认未设置编码的 Macintosh,因此我需要:

    • 把这个放在 logic.pl 的顶部::- encoding(utf8).
    • 在文件顶部放置一个字节顺序标记
    • 先致电set_prolog_flag(encoding,utf8).(我选择这样做)

    一旦我设置了编码,它似乎至少已经运行(我不确定false 是否是最后一条语句的正确结果,因为我没有尝试解码logic.pl 正在做什么...... ):

    [debug] ?- set_prolog_flag(encoding,utf8).
    true.
    
    [debug] ?- consult("/Users/ericzinda/test/logic.pl").
    true.
    
    [debug] ?- result = intersection([A,(A→B)], [p, (p→q)], Aim).
    false.
    

    开始将其转换为swiplserver 的最简单方法是从字面上获取命令并将它们转换为字符串:

    # test.py
    from swiplserver import *
    
    with PrologMQI() as mqi:
        with mqi.create_thread() as prolog_thread:
            result = prolog_thread.query("set_prolog_flag(encoding,utf8).")
            print(result)
            result = prolog_thread.query("consult(\"/Users/ericzinda/test/logic.pl\").")
            print(result)
            result = prolog_thread.query("result = intersection([A,(A→B)], [p, (p→q)], Aim).")
            print(result)
    

    在 Macintosh 命令行上运行得到与 Prolog 命令行相同的结果:

    % python3 test.py
    True
    True
    False
    
    %
    

    一旦事情进展顺利,就值得考虑一下 Prolog 和 Python 代码之间的接口。您应该尽量避免使用“健谈”的界面(有一堆来回低级调用的界面)。尝试将您正在做的事情封装在更高级别的谓词中,这些谓词执行更大的工作并调用它们。编写和调试代码会更容易,而且可能会更快一些,因为您不会产生来回发送信息的成本。

    【讨论】:

      【解决方案2】:

      对我来说,解决方案是传递文件的完整路径(确保还导入并使用名为 create_posix_path() 的方法),然后这一切都对我有用:

      from swiplserver import PrologMQI, PrologThread, create_posix_path
      
      with PrologMQI() as mqi:
          with mqi.create_thread() as prolog:
              path = create_posix_path("C:\\full\\path\\to\\file.pl")
              prolog.query(f'consult("{path}").')
              result = prolog.query('some_query(X)')
              print(result)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多