| English | 释义 | 拼音 |
|---|---|---|
| fact/fækt/ | 事实 | shì shí |
| declarative programming/dɪˈklærətɪv ˈprəʊɡræmɪŋ/ | 声明式编程 | shēng míng shì biān chéng |
| rule/ruːl/ | 规则 | guī zé |
| goal/ɡəʊl/ | 目标 | mù biāo |
| backtrack/ˈbæktræk/ | 回溯 | huí sù |
| unification/ˌjuːnɪfɪˈkeɪʃn/ | 合一 | hé yī |
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).
已知事实member(ben).和trained(ben, audio).,下列哪项陈述属于给定事实?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
member(ben).直接给出。知识库中不包含本的相机培训信息,且can_borrow是规则必须证明的目标。
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).

在规则can_borrow(Person, camera) :- member(Person), trained(Person, camera).中,两个条件之间的逗号表示____。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
同一人的会员资格和相机训练必须同时成立。
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.
俱乐部成员有亚、本和陈。亚和陈经过相机培训;本仅经过音频培训。规则can_borrow(Person, camera)要求同时具备会员资格和相机培训。谁满足can_borrow(Person, camera)?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Aya 和 陈 具备所有必需的事实。Ben 仅有音频训练。
在member(Person)中的一个Person变量,其值可能与trained(Person, camera)中的Person不同。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
在证明中,同一变量的所有出现必须保持相同的绑定。
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.
同一个绑定必须满足两个条件
根据给定事实追踪含两个条件的固定规则;候选失败后可尝试另一个绑定,缺少匹配事实则目标无法证明。
追踪Aya已被返回后的剩余搜索过程。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Ben是成员但缺乏匹配的相机培训事实;引擎随后尝试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.
将trained(Person, camera)与trained(chen, camera)匹配会将Person绑定到____。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
设备匹配,且第一个参数提供了绑定 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).
给定trained(aya, camera).和member(ben).,以及规则can_help(Mentor, Learner) :- trained(Mentor, camera), member(Learner).,can_help(aya, ben)是否成功?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Aya具备相机培训,Ben是成员;作为学员,Ben不必具备相机培训。
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.
俱乐部将事实trained(ben, camera).添加到知识库中。现在有多少人满足can_borrow(Person, camera)?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
本已经是会员并获得了相机培训,因此该目标有三个解:亚、本和陈。规则未改变,但一条新事实改变了答案。
目标失败证明该事件在此知识库之外从未发生过。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
失败意味着根据可用事实和规则无法证明该目标。