֪

ҳ
ǰλ: ҳ > > Ajaxʵ̳ >

Ϊʲôѧϰ Prolog

ʱ:2017-12-04 21:53Դ:֪www.zhixing123.cn ༭:

Prolog ߼ʽı. ҵһμֵʱԼʵѧϰ SICP Ĺ, ʵһ򵥵߼ʽԵĽ. ֮ CTMCP һе Relational Programming Logical Programming жμ Prolog Ӱ, ұڸֱԷdzظȤ, ѡѧϰ, ¼һҵѧϰĽȺ;, ҲϣΪѧϰ Prolog ṩһ΢İ.

##鼮ѡ

ѡѧϰһµı֮, һѯһЩΪ Prolog ʹ߼, ҲdzԲ, һʼҪʹõ Prolog by Example, ΪҵԾѡ Learn Prolog Now.

##Ĵ

ܿٵĴ Prolog Ŀ, õ Mac.

 

brew cask install swi-prolog

ûװ brew cask, google ҵйߵİװ. ҲͲڶ˵.

õı༭ Emacs, ʹõIJԵ. װ֮, ǾͿԿʼ Prolog ֮.

##Prolog

Prolog ַdzṹ:

  • ʵ(facts)
  • (rules)
  • ѯ(queries)

ʵ͹򱻽 knowledge base ߽ database, Prolog ıʵд knowledge base, Щ knowledge base ͶͱǸȤȫ֪ʶ. ʹһ Prolog ? Ƿѯ, ͨ knowledge base д洢, Ȼȡش. ƽʱʹ Objective-C Java ʽżIJͬ.

###Facts

д¼ Prolog е facts, Ҫһļ, ׺ .pl.

 

programmer(linux).
programmer(bill).
designer(jonathan).

ļ facts.pl һЩ facts ļ, ע, ÿһ fact . β. ôʹ, л facts.pl Ŀ¼, Ȼ swipl.

 

Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 6.6.6)
Copyright (c) 1990-2013 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).

?-

ȻЩϢ, ʹ consult('fact.pl'). Prolog . : programmer(linux).

 

?- programmer(linux).
true

Prolog ᷵ true. : programmer(jonathan).

 

?- programmer(jonathan).
false

ͻ᷵ false. ܼ򵥰.

Ȼ facts Ҳж

 

loves(i,u).
loves(he,u).

һڹ, ͻ Undefined procedure.

###Rules

ǽһʲôǹ rules, dz, Prolog ʵһЩ, ʹ :-.

 

sunny(today).
happy(tom).
football(tom) :- sunny(today),happy(tom).

ǼǵĴ, ѯ football(tom). Prolog ᷵ true, ʲô, ǰ벿Ҳ football(tom) ǹͷ(head), ĺ벿 sunny(today),happy(tom) ǹĿ(goals). goals ʱ, head ͻҲ:

ܺ, tom Ҳܿ, ôͻȥ.

Prolog ʹ , ʾ , ʹ ; ʾ . , Ҫ. Prolog Ƴ football(tom).

ǿ Rules ӱǿǵı, :

 

father(tom,john).
father(john,ive).
grandfather(X,Z):-father(X,Y),father(Y,Z).

Ƿdz, Ҳ˵.

###Queries

ʲô queries, ʵϱ football(tom)., programmer(jonathan). һֲѯ. ֻDzѯǷ, ǾҪڲѯ(variable).

 

programmer(linux).
programmer(bill).
designer(jonathan).

һδ, ǿͨѯ Prolog:

 

?- programmer(X).

Ȼͻ

 

X = linux

Prolog ÿֻȷĽ, 뿴Ľ, ; Ҳǻ, 鿴Ƿ, ; ֮, :

 

X = bill

ٴ֮, ͻ᷵ false , ΪûзĽ.

##Prolog ﷨

Prolog еĻԪ֮, Ҫ֪, Prolog еĻԪصʲô, һ Prolog еĻ﷨.

Prolog еĻԪض term(֪ô). Prolog 4 ֲͬ term

  • atom
  • number
  • variable
  • complex term

###Atom

֮һľ Prolog е atom:

  1. ɴдĸ, Сдַ, ֺ»ɵIJСдĸͷַ, : john tom big_big a_bc.
  2. İװڵ ' еַ. : 'dsada' *((&@!MBAS)) Fjdh_da.
  3. ַ, :- ; @=.

###Numbers

Prolog вǷdzҪ, Prolog ִֵ֧ıʾֵ.

###Variables

ɴдĸ, Сдַ, ֺ»ɵIJԴдĸ»߿ͷַ, X Variable _dsal.

_ Prolog зdzⱻΪ.

###Complex Terms

Complex terms ʵϾһֽṹ, ֮ǰ

 

programmer(linux).
programmer(bill).
designer(jonathan).

Щ complex terms, ҲԶӸӵĽṹ

 

father(father(fahter(john))).

complex term ӵеIJ arity, arity complex term мҪ, progammer(linux) е arity = 1, love(i,u) е arity = 2.

Prolog пԶͬ, arity ͬ predicate.

۵ predicate ʱ, Ǿʹ / arity ʾ predicate.

:

 

happy/1
love/2

##ܽ

ĿǰΪֹ, Ѿ Prolog һ˽, ҲܸоԵڲͬ, ֲͬʹѧϰ.

һ
(1)
100%
һ
(0)
0%
ǩ(Tag):Prolog
------ָ----------------------------
------ָ----------------------------
Ծػص߷棬Ͻɫ顢ۡ
:
:
֤:ҸͼƬ
Ȥ