Prolog 🦉 | Basic Syntax
1. Run Prolog
- file name:
test.pl
- go to the location of the
test.pl
in terminal - input
swipl
- input
[test].
(remember '.') - do sth
- if you want to halt the file, input
halt
2. Facts
- comment: type '%':
% This is the syntax for comments
- facts
- fact:
pred(arg1, arg2, ..., argn).
- arg: integer, atom, string begin with lower letter etc
- variable: item begin with '_' or UPPER LETTER, structure etc
- e.g.
- fact:
3. Simple Query
-
?-
: the prompt for the interpreter -
query: type
pred(something)
-
query principle inside
- prolog will match user's target to find the answer; when input
;
, it will redo (release the variable first and start the search from the last token) - prolog's target has four ports to control the processes
- call: find the clause
- exit: target matched successfully, mark the success clause, and bind the variable
- redo: release the variable first and start the search from the last token
- fail: can not find more success clause
- prolog will match user's target to find the answer; when input
-
debug: input
debug
-
trace: input
trace
4. Hybrid Query
-
hybrid query: input more query like:
-
some interval predicate
write
(1 arg): print some stringnl
: print an entertab
(1 arg: n): print n\space
5. Rules
-
rule:
head :- body
,head
is predicate,:-
means "if",body
is target- e.g.
-
append more rules: we can append more rules to define a predicate
- e.g.
-
rules basic principle: in fact rules are multiple layers of query. we can see the trace of last instance:
6. Arithmetic
7. Data Management
-
asserta
&assert
asserta(X)
: it will append X to the beginning of the dynamic databaseassert(X)
: it will append X to the end of the dynamic databasedynamic pred/arg_size
: if you want to useasserta
&assert
, please typedynamic pred/arg_size
to run at the front of the program, which allowspred
can be modified dynamically
- e.g.
-
retract(X)
: removeX
from dynamic database(rememberdynamic
) -
use in rules
8. Recursion
-
recursion:
-
Which is faster?
is_contained_in(T1,T2):- location(X,T2), is_contained_in(T1,X).
is_contained_in(T1,T2):- location(T1,X), is_contained_in(X,T2).
- if we ask
is_contained_in(X, desk)
,1.
is faster!
9. Unification
-
unification
- variable & any items
- original item & original item(atom or integer): when they equal
- structure & structure: when each corresponding parameter of two structures can be associated
-
= (arg1, arg2)
orarg1 = arg2
-
difference between unification and math expression: unification will not compute, but
is
will do
10. Data Structure
11. List
-
List:
[obj1, obj2, ...]
-
empty list:
[]
ornil
-
head and tail
-
member(obj, list)
-
append(list1, list2, dest)
-
findall(result, target, result_list)
: find all elements intarget
mode and store them inresult_list
inresult
mode
12. Operator
-
operator
- infix:
3 + 10
- prefix:
-13
- postfix:
8 factorial
- infix:
-
operator precedence
fix name
trait Infix xfx
non-associative Infix xfy
right to left Infix yfx
left to right Prefix fx
non-associative Prefix fy
left to right Postfix xf
non-associative Postfix yf
right to left -
:
operator
13. Cut
-
cut
!
: add!
in the rule will cut the search -
!
suppresses the traceback of the left target, while the target to its right is unaffected -
not
basic principle
14. Process Control
-
repeat
: always succeed, provide an infinite number of choice points -
how to create a endless loop:
-
loop: read in a simple command and echoed on the screen until the user enters
end
-
Tail Recursion
- Tail Resursion: using recursion to repeat; its form is a recursive statement at the end of the function, and the computation of each layer does not need to use the return information of the next layer so that a good Prolog interpreter does not need to rely on a stack
- e.g.
factorial(number, 1, answer)
->answer := number!
5! = 120;
15. Natural Language
-
And Proof: first find out all the possible breakdown of a sentence, then test whether each part of breakdown is legal
-
Diff Table: including two tables: full table and remainder table, which can be used as two arguments of the predicate; we use
-
to connect two tables to make it easier to read, in the form ofX - Y
-
Definite Clause Grammar -- DCG
- DCG: Diff tables are often used in Prolog, so many Prolog versions have good support for diff tables. This syntax is called DCG, and it looks very similar to a normal Prolog clause, except that
:-
is replaced by-->
, which is translated by Prolog into the normal difference table pred --> arg
=pred([arg|X], X)
- using DCG, the first part of the last instance can be:
- DCG: Diff tables are often used in Prolog, so many Prolog versions have good support for diff tables. This syntax is called DCG, and it looks very similar to a normal Prolog clause, except that
__EOF__

本文链接:https://www.cnblogs.com/RadiumGalaxy/p/17231404.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理