Pemrograman deklaratif: fakta, aturan, dan tujuan
Diperkenalkan
13:52
Paradigma Pemrograman
Berikut adalah satu pekerjaan: daftarkan setiap siswa dengan nilai A. Satu programmer menulis sebuah perulangan — mulai dari rekaman pertama, periksa nilainya, tambahkan nama ke daftar, pindahkan…
Narasi bahasa Inggris · takarir bahasa Inggris dan 中文 yang dapat dipilih
In this lesson
| English | Makna |
|---|---|
| fact/fækt/ | fakta |
| declarative programming/dɪˈklærətɪv ˈprəʊɡræmɪŋ/ | pemrograman deklaratif |
| rule/ruːl/ | aturan |
| goal/ɡəʊl/ | tujuan |
| backtrack/ˈbæktræk/ | backtracking |
| unification/ˌjuːnɪfɪˈkeɪʃn/ | unifikasi |
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).
Mengingat fakta member(ben). dan trained(ben, audio)., pernyataan mana di bawah ini merupakan fakta yang disediakan?
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
member(ben). diberikan secara langsung. Basis pengetahuan tidak mencakup pelatihan kamera untuk Ben, dan can_borrow adalah tujuan yang harus dibuktikan oleh sebuah aturan.
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).

Dalam aturan can_borrow(Person, camera) :- member(Person), trained(Person, camera)., koma antara dua kondisi berarti ____.
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
Kedua keanggotaan dan pelatihan kamera harus berlaku untuk Orang yang sama.
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.
Anggota klub adalah Aya, Ben, dan Chen. Aya dan Chen terlatih dalam kamera; Ben hanya terlatih dalam audio. Aturan can_borrow(Person, camera) memerlukan keanggotaan sekaligus pelatihan kamera. Siapa yang memenuhi can_borrow(Person, camera)?
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
Aya dan Chen memiliki kedua fakta yang diperlukan. Ben hanya memiliki pelatihan audio.
Variabel Person dalam member(Person) dapat memiliki nilai berbeda dari Person dalam trained(Person, camera) selama satu pembuktian.
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
Semua kemunculan variabel yang sama harus tetap mempertahankan pengikatan yang sama dalam pembuktian tersebut.
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.
Lacak pencarian yang tersisa setelah Aya telah dikembalikan.
Kesulitan 3/5 · 200 koin + bonus kecil pada penyelesaian pertama hari ini
Ben adalah anggota tetapi tidak memiliki fakta pelatihan kamera yang sesuai; mesin kemudian mencoba 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.
Mencocokkan trained(Person, camera) dengan trained(chen, camera) mengikat Person ke ____.
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
Peralatan cocok dan argumen pertama menyediakan pengikatan 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).
Diberikan trained(aya, camera). dan member(ben)., serta aturan can_help(Mentor, Learner) :- trained(Mentor, camera), member(Learner)., apakah can_help(aya, ben) berhasil?
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
Aya memiliki pelatihan kamera dan Ben adalah anggota; Ben tidak perlu memiliki pelatihan kamera untuk menjadi peserta belajar.
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.
Klub menambahkan fakta trained(ben, camera). ke basis pengetahuan. Sekarang berapa banyak orang yang memenuhi can_borrow(Person, camera)?
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
Ben sudah menjadi anggota dan kini memiliki pelatihan kamera, sehingga tujuannya memiliki tiga solusi: Aya, Ben, dan Chen. Aturan tersebut tidak berubah; satu fakta baru mengubah jawabannya.
Tujuan yang gagal membuktikan bahwa peristiwa yang bersangkutan tidak pernah terjadi di luar basis pengetahuan ini.
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
Kegagalan berarti tujuan tidak dapat dibuktikan dari fakta dan aturan yang tersedia.