<< Chapter < Page Chapter >> Page >

Cú pháp:

<Tên vị từ>(<danh sách các tham số>)<kí hiệu>

<Tên vị từ 1>(<danh sách các tham số 1>)<kí hiệu>

………

<Tên vị từ N>(<danh sách các tham số N>)<kí hiệu>

Trong đó: Tên vị từ phải là các tên vị từ đã được khai báo trong phần predicates. Các tham số có thể là các hằng hoặc biến có kiểu tương thích với các kiểu tương ứng đã được khai báo trong các vị từ ở trong phần predicates; các tham số được viết cách nhau bởi dấu phẩy. Các kí hiệu bao gồm:

:- (điều kiện nếu).

, (điều kiện và).

; (điều kiện hoặc).

. (kết thúc vị từ)

Ví dụ:

Clauses

so_nguyen_to(2):-!.

so_nguyen_to(N):-N>0,

so_nguyen_to(M),

M<N,

N MOD M<>0.

so_huu(“Nguyen Van A”, sach(“Do Xuan Loi”, “Cau truc DL”, “Khoa hoc Ky thuat”, nxb(8,1985))).

Chú ý: Nếu trong các tham số của một vị từ có biến thì biến này phải xuất hiện ít nhất 2 lần trong vị từ đó hoặc trong các vị từ dùng để suy diễn ra vị từ đó. Nếu chỉ xuất hiện một lần thì bắt buộc phải dùng biến tự do.

Ví dụ: Để diễn tả sự kiện: Tổ hợp chập 0 của N (N bất kỳ) bằng 1, ta không thể viết Tohop(N,0,1) vì biến N chỉ xuất hiện đúng một lần trong vị từ này, do đó ta phải viết Tohop(_,0,1) .

Phần goal

Bao gồm các mục tiêu mà ta yêu cầu Turbo Prolog xác định và tìm kết quả. Đây là phần không bắt buộc phải có. Nếu ta viết sẵn trong chương trình thì đó gọi là goal nội; Nếu không, khi chạy chương trình Turbo Prolog sẽ yêu cầu ta nhập goal vào, lúc này gọi là goal ngoại.

Cú pháp phần goal giống như cú pháp phần clauses. Tức là ta đưa vào một hoặc một số các vị từ.

Nếu tất cả các tham số của vị từ là hằng thì kết quả nhận được là Yes (đúng) hoặc No (sai). Nếu trong các tham số của vị từ có biến thì kết quả trả về sẽ là các giá trị của biến.

Ngoài các phần chủ yếu nói trên, ta có thể đưa vào các phần liên quan đến khai báo hằng, các tập tin liên quan hoặc chỉ thị dịch.

Ví dụ:

Constants

Pi = 3.141592653

Một số ví dụ về chương trình prolog

Ví dụ 1: Xét xem một số N có phải là số nguyên tố hay không.

domains

so_nguyen = integer

predicates

so_nguyen_to(so_nguyen)

Clauses

so_nguyen_to(2):-!.

so_nguyen_to(N):-N>0,

so_nguyen_to(M),

M<N,

N MOD M<>0.

goal

so_nguyen_to(13).

Ví dụ 2: Giả sử ta có bảng số liệu như sau:

Tên người giới tính Đặc điểm Tiêu chuẩn kết bạn
lan nữ đẹp, khoẻ, tốt, khoẻ, thông minh, đẹp
hồng nữ đẹp, thông minh, giàu khoẻ, thông minh, giàu
thuỷ nữ tốt, khoẻ, giàu đẹp, khoẻ, thông minh
anh nam khoẻ, giàu, thông minh đẹp, thông minh, tốt
bình nam đẹp, khoẻ, thông minh đẹp, khoẻ
hùng nam giàu, thông minh, khoẻ tốt, thông minh, khoẻ

Tiêu chuẩn kết bạn là hai người khác phái, người này hội đủ các tiêu chuẩn của người kia và ngược lại. Hãy viết chương trình để tìm ra các cặp có thể kết bạn với nhau.

domains

ten, g_tinh = symbol

predicates

gioi_tinh(ten, g_tinh)

dep(ten)

tot(ten)

giau(ten)

thong_minh(ten)

khoe(ten)

thich(ten,ten)

ket_ban(ten,ten)

clauses

gioi_tinh(lan,nu).

gioi_tinh(hong,nu).

gioi_tinh(thuy,nu).

gioi_tinh(anh,nam).

gioi_tinh(binh,nam).

gioi_tinh(hung,nam).

dep(lan).

dep(hong).

dep(binh).

khoe(thuy).

khoe(lan).

khoe(binh).

khoe(anh).

khoe(hung).

tot(lan).

tot(thuy).

thong_minh(hong).

thong_minh(anh).

thong_minh(hung).

thong_minh(binh).

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Ngôn ngữ lập trình. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10783/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Ngôn ngữ lập trình' conversation and receive update notifications?

Ask