Prologlike解释器和元组存储
natlog的Python项目详细描述
具有自然语言风格语法的轻量级类Prolog解释器
我们紧跟爱因斯坦的“每件事都应该尽可能简单,但不能简单。”
此时,我们依赖于Python的自然错误检查,而没有对语法或语义错误发出警告。这是可以添加的,但这意味着它是一种简单自然的逻辑语言的可执行规范,我们将其命名为Natlog。在
^{str>>- 在
术语用嵌套元组表示。在
在 - 在
使用了简化Prolog术语语法的解析器和扫描程序
将术语转换为嵌套的Python元组。在
在
事实的表面语法,从字符串中读取,只是空格分隔的单词
(元组用圆括号括起来)和
以.
或{}结尾的句子。
和Prolog一样,变量都是大写的,除非引用。示例程序位于文件夹natprogs
,例如tc.nat
:
cat is feline.
tiger is feline.
mouse is rodent.
feline is mammal.
rodent is mammal.
snake is reptile.
mammal is animal.
reptile is animal.
tc A Rel B : A Rel B.
tc A Rel C : A Rel B, tc B Rel C.
要查询它,请尝试:
^{pr2}$
它将返回is
关系的传递闭包。在
GOAL PARSED: (('tc', 0, 'is', 'animal'),)
ANSWER: ('tc', 'cat', 'is', 'animal')
ANSWER: ('tc', 'tiger', 'is', 'animal')
ANSWER: ('tc', 'mouse', 'is', 'animal')
ANSWER: ('tc', 'feline', 'is', 'animal')
ANSWER: ('tc', 'rodent', 'is', 'animal')
ANSWER: ('tc', 'snake', 'is', 'animal')
ANSWER: ('tc', 'mammal', 'is', 'animal')
ANSWER: ('tc', 'reptile', 'is', 'animal')
还支持列表处理,如下所示:
app () Ys Ys.
app (X Xs) Ys (X Zs) : app Xs Ys Zs.
解释器支持yield
机制,类似于Python的机制。有点像
^ my_answer X
结果我的答案X是屈服的作为一个答案。在
解释器还扩展到使用相同的前缀运算符语法处理对Python的简单函数和生成器调用:
`f A B .. Z R
,导致Python函数{}被调用并与其结果一致 ``f A B .. Z R
,导致Python生成器f(A,B,C)
被调用,R与它的多个结果统一,一次一个~R A B .. Z
用于将{}与术语库中的匹配事实统一起来 f A B .. Z
,导致调用f(A,B,C,..,Z)
,但没有返回结果
基于统一的元组挖掘的嵌套元组存储
索引器与统一算法相结合用于检索与包含逻辑变量的术语匹配的基本项。在
索引在中出现的所有常量上
数据库中的基本事实。在
事实是有根据的,
搜索时关闭了统一检查和尾随
为了比赛。在
要试用,请执行以下操作:
python3 -i db.py
>>> dbtest()
在消化文本并进行查询后,它给出:
John has (a car).
Mary has (a bike).
Mary is (a student).
John is (a pilot).
('John', 'has', ('a', 'car'))
('Mary', 'has', ('a', 'bike'))
('Mary', 'is', ('a', 'student'))
('John', 'is', ('a', 'pilot'))
Who has (a What)?
--> ('John', 'has', ('a', 'car'))
--> ('Mary', 'has', ('a', 'bike'))
Who is (a pilot)?
--> ('John', 'is', ('a', 'pilot'))
'Mary' is What?
--> ('Mary', 'is', ('a', 'student'))
'John' is (a What)?
--> ('John', 'is', ('a', 'pilot'))
Who is What?
--> ('Mary', 'is', ('a', 'student'))
--> ('John', 'is', ('a', 'pilot'))
标签:
- 项目
推荐PyPI第三方库
- 在
术语用嵌套元组表示。在
在 - 在
使用了简化Prolog术语语法的解析器和扫描程序 将术语转换为嵌套的Python元组。在
在
事实的表面语法,从字符串中读取,只是空格分隔的单词
(元组用圆括号括起来)和
以.
或{natprogs
,例如tc.nat
:
cat is feline.
tiger is feline.
mouse is rodent.
feline is mammal.
rodent is mammal.
snake is reptile.
mammal is animal.
reptile is animal.
tc A Rel B : A Rel B.
tc A Rel C : A Rel B, tc B Rel C.
要查询它,请尝试:
^{pr2}$它将返回is
关系的传递闭包。在
GOAL PARSED: (('tc', 0, 'is', 'animal'),)
ANSWER: ('tc', 'cat', 'is', 'animal')
ANSWER: ('tc', 'tiger', 'is', 'animal')
ANSWER: ('tc', 'mouse', 'is', 'animal')
ANSWER: ('tc', 'feline', 'is', 'animal')
ANSWER: ('tc', 'rodent', 'is', 'animal')
ANSWER: ('tc', 'snake', 'is', 'animal')
ANSWER: ('tc', 'mammal', 'is', 'animal')
ANSWER: ('tc', 'reptile', 'is', 'animal')
还支持列表处理,如下所示:
app () Ys Ys.
app (X Xs) Ys (X Zs) : app Xs Ys Zs.
解释器支持yield
机制,类似于Python的机制。有点像
^ my_answer X
结果我的答案X是屈服的作为一个答案。在
解释器还扩展到使用相同的前缀运算符语法处理对Python的简单函数和生成器调用:
`f A B .. Z R
,导致Python函数{}被调用并与其结果一致 ``f A B .. Z R
,导致Python生成器f(A,B,C)
被调用,R与它的多个结果统一,一次一个~R A B .. Z
用于将{}与术语库中的匹配事实统一起来 f A B .. Z
,导致调用f(A,B,C,..,Z)
,但没有返回结果
基于统一的元组挖掘的嵌套元组存储
索引器与统一算法相结合用于检索与包含逻辑变量的术语匹配的基本项。在
索引在中出现的所有常量上 数据库中的基本事实。在
事实是有根据的, 搜索时关闭了统一检查和尾随 为了比赛。在
要试用,请执行以下操作:
python3 -i db.py
>>> dbtest()
在消化文本并进行查询后,它给出:
John has (a car).
Mary has (a bike).
Mary is (a student).
John is (a pilot).
('John', 'has', ('a', 'car'))
('Mary', 'has', ('a', 'bike'))
('Mary', 'is', ('a', 'student'))
('John', 'is', ('a', 'pilot'))
Who has (a What)?
--> ('John', 'has', ('a', 'car'))
--> ('Mary', 'has', ('a', 'bike'))
Who is (a pilot)?
--> ('John', 'is', ('a', 'pilot'))
'Mary' is What?
--> ('Mary', 'is', ('a', 'student'))
'John' is (a What)?
--> ('John', 'is', ('a', 'pilot'))
Who is What?
--> ('Mary', 'is', ('a', 'student'))
--> ('John', 'is', ('a', 'pilot'))
- 项目
标签: