You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:调试GNU Prolog版《I'm My Own Grandpa》亲属关系程序

《I'm My Own Grandpa》GNU Prolog代码问题排查与修正

核心错误分析

你的代码运行失败主要源于以下几个问题:

  1. daughter谓词语义混淆:事实daughter(redhair, widow)的第二个参数是母亲,但规则daughter(Child, Father)的第二个参数限定为父亲,导致规则调用时无法匹配女性的widow,第一个查询直接失败。
  2. 缺少核心事实:未定义baby的父母关系,所有涉及baby的查询必然失败。
  3. sibling规则逻辑错误:基于女婿关系推导兄弟姊妹,完全不符合亲属关系的真实逻辑。
  4. 部分查询逻辑错误:例如Is baby the brother of dad,baby是dad的儿子而非兄弟,该查询本身违背剧情。
  5. grandparent规则不完整:仅覆盖父系祖父,未考虑母系或继亲情况,无法推导剧情中的外祖父关系。

修正后的完整代码

% 基础婚姻事实
wife(widow, i).       % widow是i的妻子
wife(redhair, dad).   % redhair是dad的妻子

% 基础亲子事实
daughter(redhair, widow).  % redhair是widow的女儿
father(dad, i).            % dad是i的父亲
father(dad, baby).         % dad是baby的父亲
mother(redhair, baby).     % redhair是baby的母亲

% 推导规则:女儿与父亲的关系(含继女)
daughter(Child, Father) :-
    wife(Mother, Father),
    daughter(Child, Mother).

% 推导规则:母亲与孩子的关系(通过婚姻和父亲关联)
mother(Mother, Child) :-
    wife(Mother, Father),
    father(Father, Child).

% 推导规则:女婿关系(配偶的女儿的丈夫)
son_in_law(SonInLaw, Parent) :-
    daughter(Daughter, Parent),
    wife(Daughter, SonInLaw).

% 推导规则:兄弟姊妹(共享父亲或母亲)
sibling(Child1, Child2) :-
    father(Father, Child1),
    father(Father, Child2),
    Child1 \= Child2.
sibling(Child1, Child2) :-
    mother(Mother, Child1),
    mother(Mother, Child2),
    Child1 \= Child2.

% 推导规则:祖父母(覆盖所有父系/母系、亲生/继亲场景)
grandparent(Grandparent, Grandchild) :-
    father(Grandparent, Parent),
    father(Parent, Grandchild).
grandparent(Grandparent, Grandchild) :-
    father(Grandparent, Parent),
    mother(Parent, Grandchild).
grandparent(Grandparent, Grandchild) :-
    mother(Grandparent, Parent),
    father(Parent, Grandchild).
grandparent(Grandparent, Grandchild) :-
    mother(Grandparent, Parent),
    mother(Parent, Grandchild).
grandparent(Grandparent, Grandchild) :-
    daughter(Parent, Grandparent),
    mother(Parent, Grandchild).

% 推导规则:叔叔/伯父(父亲的兄弟)
uncle(Uncle, NieceNephew) :-
    sibling(Uncle, Parent),
    father(Parent, NieceNephew).

% 主程序:修正后的剧情查询
main :-
    write('Is redhair the daughter of i ?: '),
    (daughter(redhair, i) -> write('yes'), nl ; write('no'), nl),
    write('Is dad the son in law of i ?: '),
    (son_in_law(dad, i) -> write('yes'), nl ; write('no'), nl),
    write('Is baby the brother of i ?: '),
    (sibling(i, baby) -> write('yes'), nl ; write('no'), nl),
    write('Is i the grandfather of baby ?: '),
    (grandparent(i, baby) -> write('yes'), nl ; write('no'), nl),
    write('Is widow the mother of redhair ?: '),
    (mother(widow, redhair) -> write('yes'), nl ; write('no'), nl),
    write('Is i a grandfather (of someone) ?: '),
    (grandparent(i, _) -> write('yes'), nl ; write('no'), nl),
    halt.

关键修正说明

  1. 统一谓词语义:调整daughter规则,允许第二个参数为任意父母,确保继女关系能正确推导。
  2. 补充baby的事实:添加baby的父母关系,为后续亲属关系查询提供基础。
  3. 重写sibling规则:改为基于共享父母的逻辑,符合真实亲属关系定义。
  4. 修正查询逻辑:将错误的兄弟查询改为符合剧情的baby与i的兄弟关系查询,同时使用分支处理成功/失败输出,避免单个查询失败导致程序终止。
  5. 完善grandparent规则:添加继亲场景的推导,确保能正确识别i作为baby外祖父的关系。

内容的提问来源于stack exchange,提问作者Michael Ferguson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 06:50:55