Lập trình khai báo: sự thật, quy tắc và mục tiêu
Đã giới thiệu
13:52
Mô hình lập trình
Đây là một công việc: liệt kê tất cả sinh viên có điểm A. Một lập trình viên viết một vòng lặp — bắt đầu từ bản ghi đầu tiên, kiểm tra điểm, thêm tên vào danh sách, di chuyển…
Thuyết minh tiếng Anh · Có thể chuyển đổi phụ đề tiếng Anh và tiếng Trung
Trong bài học này
| English | Ý nghĩa |
|---|---|
| fact/fækt/ | Sự thật |
| declarative programming/dɪˈklærətɪv ˈprəʊɡræmɪŋ/ | Lập trình khai báo |
| rule/ruːl/ | Quy tắc |
| goal/ɡəʊl/ | Mục tiêu |
| backtrack/ˈbæktræk/ | Hồi quy |
| unification/ˌjuːnɪfɪˈkeɪʃn/ | Thống nhất hóa |
Ask who can borrow a camera
- A media club keeps facts about its members and completed training. It wants to ask who can borrow a camera without writing a new loop for every question.
- Declarative programming 声明式编程 describes facts, rules and a goal 目标. A logic engine searches for values that make the goal true. The programmer still has to write correct rules; the engine does not read minds or club noticeboards.
Facts state what is known
- A fact 事实 is an assertion in the knowledge base. In
member(aya)., the predicatememberrelates one person to club membership.trained(aya, camera).relates a person to a piece of equipment. - This lesson uses Prolog-style notation: lowercase names are constants or predicates; uppercase names are variables. A full stop ends each fact or rule.
member(aya).
member(ben).
member(chen).
trained(aya, camera).
trained(chen, camera).
trained(ben, audio).
Cho các sự thật member(ben). và trained(ben, audio)., phát biểu nào sau đây là một sự thật đã cho?
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
member(ben). được cung cấp trực tiếp. Cơ sở tri thức không bao gồm đào tạo camera cho Ben, và can_borrow là mục tiêu mà một quy tắc phải chứng minh.
A rule combines conditions
- A rule 规则 states when a conclusion holds. Read
:-as “if” and the comma as AND: a person may borrow a camera if they are a member and have camera training. Personnames the same variable in all three places. The engine must find one value that satisfies both conditions, not one member and a different trained person.
can_borrow(Person, camera) :- member(Person), trained(Person, camera).

Trong quy tắc can_borrow(Person, camera) :- member(Person), trained(Person, camera)., dấu phẩy giữa hai điều kiện có nghĩa là ____.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Cả thành viên và huấn luyện camera đều phải đúng cho cùng một Người.
A goal asks a question
- The goal
?- can_borrow(aya, camera).asks for a yes/no result. The engine matches the rule and checksmember(aya)andtrained(aya, camera): both facts exist, so the goal succeeds. - The goal
?- can_borrow(Person, camera).asks for bindings of the variable. With this knowledge base, the answers arePerson = ayaandPerson = chen. These are goals to reason about; the displayed blocks are notation, not a runnable playground.
Thành viên câu lạc bộ là Aya, Ben và Chen. Aya và Chen được đào tạo về camera; Ben chỉ được đào tạo về âm thanh. Quy tắc can_borrow(Person, camera) yêu cầu cả thành viên lẫn đào tạo camera. Ai thỏa mãn can_borrow(Person, camera)?
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Aya và Chen có cả hai sự thật cần thiết. Ben chỉ có huấn luyện âm thanh.
Biến Person trong member(Person) có thể có giá trị khác với Person trong trained(Person, camera) trong suốt một chứng minh.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Tất cả các xuất hiện của cùng một biến phải giữ nguyên liên kết giống nhau trong lần chứng minh đó.
Worked example: trace a failed candidate
- Start with
Person = ben.member(ben)succeeds.trained(ben, camera)fails: the available training fact is for audio, which does not match camera. - The engine backtracks 回溯 to try another candidate,
Person = chen. Both conditions succeed. If more answers are requested, it continues searching; if no candidates remain, there are no more answers.
One binding must satisfy both conditions
Trace a fixed two-condition rule through supplied facts. A failed candidate can lead to another binding; a missing fact leaves a goal unproved.
Theo dõi quá trình tìm kiếm còn lại sau khi Aya đã được trả về.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Ben là thành viên nhưng thiếu một sự kiện khớp về việc đào tạo camera; sau đó động cơ sẽ thử Chen.
Matching shares values across a rule
- Unification 合一 matches terms while consistently binding variables. Matching
trained(Person, camera)withtrained(chen, camera)bindsPersontochen; matching it withtrained(ben, audio)fails because the equipment differs. - A failed goal means it cannot be proved from these facts and rules. It does not prove that Ben has never used a camera in real life; the knowledge base may simply be incomplete.
Gắn kết trained(Person, camera) với trained(chen, camera) sẽ gán Person cho ____.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Thiết bị khớp và đối số đầu tiên cung cấp liên kết chen.
Worked example: write a new rule
- Requirement: a mentor can help someone only when the mentor has camera training and the learner is a member. Add the rule below, then ask
?- can_help(aya, ben).. trained(aya, camera)andmember(ben)both exist, so the goal succeeds. This rule allows Aya to help herself too. If self-help must be excluded, the requirement needs another condition; do not invent it silently.
can_help(Mentor, Learner) :- trained(Mentor, camera), member(Learner).
Cho trained(aya, camera). và member(ben)., cùng quy tắc can_help(Mentor, Learner) :- trained(Mentor, camera), member(Learner)., liệu can_help(aya, ben) có thành công không?
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Aya có đào tạo camera và Ben là thành viên; Ben không cần phải có đào tạo camera để trở thành người học.
Marks that slip away
- A fact states something known; a rule derives a conclusion when its conditions hold; a goal asks whether a conclusion can be proved or which values make it true.
- Keep repeated variables consistent.
member(X), trained(Y, camera)would let an untrained member qualify because someone else has training. Changing a variable name can change the rule's meaning.
From a requirement to a proof
- Write facts, translate each requirement into a rule, and pose a goal. Trace each variable binding through every condition and try alternatives when a candidate fails.
- Here only Aya and Chen may borrow a camera. Ben's audio training is not camera training. Results follow from the supplied knowledge base, not a guess about real people.
Câu lạc bộ thêm sự thật trained(ben, camera). vào cơ sở tri thức. Bây giờ có bao nhiêu người thỏa mãn can_borrow(Person, camera)?
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Ben đã là thành viên và hiện có đào tạo camera, nên mục tiêu có ba giải pháp: aya, ben và chen. Quy tắc không thay đổi; chỉ một sự thật mới làm thay đổi câu trả lời.
Một mục tiêu thất bại chứng minh rằng sự kiện tương ứng chưa từng xảy ra bên ngoài cơ sở tri thức này.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Lỗi có nghĩa là mục tiêu không thể chứng minh được từ các dữ liệu và quy tắc hiện có.
Đã xảy ra vấn đề gì? Chỉ đội ngũ trang web nhìn thấy thông tin này.