如何在Java或C#等语言中实现统一算法?

Mit*_*rax 16 artificial-intelligence predicate unification

我正在通过我的AI教科书工作,我已经完成了我的部分的最后一个作业问题:

"以您选择的任何语言实施第69页概述的统一算法."

在页69,您有统一算法的以下伪代码:

function unify(E1, E2);
    begin
        case
            both E1 and E2 are constants or the empty list:
                if E1 = E2 then return {}
                else return FAIL;
            E1 is a variable:
                if E1 occurs in E2 then return FAIL
                 else return {E2/E1}
            E2 is a variable
                if E2 occurs in E1 then FAIL
                    else return {E1/E2}
            either E1 or E2 are empty then return FAIL
            otherwise:
                begin
                    HE1 := first element of E1;
                    HE2 := first element of E2;
                    SUBS1 := unify(HE1, HE2);
                    if SUBS1 := FAIL then return FAIL;
                    TE1 := apply(SUBS1, rest of E1);
                    TE2 := apply(SUBS1, rest of E2);
                    SUBS2 := unify(TE1, TE2);
                    if SUBS2 = FAIL then return FAIL;
                         else return composition(SUBS1, SUBS2)
                end
            end
        end
Run Code Online (Sandbox Code Playgroud)

现在,我理解统一的一般概念,但我完全不知道如何开始用Java或C#这样的语言实现它.

我甚至不确定方法签名会是什么样子.需要什么类型的变量?我很确定我需要返回列表来表示谓词演算构造,但这是猜测.

例如,当它说"E1是一个变量"时,如果我将它传递给Unify方法,它怎么可能是?我可以检查null但是它会不同于"空列表"?

任何人都可以帮助我或指出我在C#或Java中实现Unificaiton算法的正确方向吗?

cdi*_*ins 9

对于任何感兴趣的人,我在http://www.cs.trincoll.edu/~ram/cpsc352/notes/unification.html上找到了相同的算法, 并提供了更多的上下文.

让我们看第一行:

function unify(E1, E2)
Run Code Online (Sandbox Code Playgroud)

E1和E2是表达式:列表,变量或常量.在传统的面向对象编程风格通常,我们将创建一个抽象基类Expression和派生其他类从中喜欢List,VariableConstant.但是在我看来这太过分了.我使用dynamic关键字在C#中实现了这一点.

接下来的问题是它返回了什么?绑定列表,可以实现为Dictionary<string, Object>.

下面是来自一个名为Jigsaw的库的实现的C#实现的片段,我正在开发它来教授如何实现C#语言.

public static Dictionary<string, object> Unify(dynamic e1, dynamic e2)
{
    if ((IsConstant(e1) && IsConstant(e2)))
    {
        if (e1 == e2)
            return new Dictionary<string,object>();
        throw new Exception("Unification failed");
    }

    if (e1 is string)
    {
        if (e2 is List && Occurs(e1, e2))
            throw new Exception("Cyclical binding");
        return new Dictionary<string, object>() { { e1, e2 } };
    }

    if (e2 is string)
    {
        if (e1 is List && Occurs(e2, e1))
            throw new Exception("Cyclical binding");
        return new Dictionary<string, object>() { { e2, e1 } };
    }

    if (!(e1 is List) || !(e2 is List))
        throw new Exception("Expected either list, string, or constant arguments");

    if (e1.IsEmpty || e2.IsEmpty)
    {
        if (!e1.IsEmpty || !e2.IsEmpty)
            throw new Exception("Lists are not the same length");

        return new Dictionary<string, object>(); 
    }

    var b1 = Unify(e1.Head, e2.Head);
    var b2 = Unify(Substitute(b1, e1.Tail), Substitute(b1, e2.Tail));

    foreach (var kv in b2)
        b1.Add(kv.Key, kv.Value);
    return b1;
}
Run Code Online (Sandbox Code Playgroud)

您可以在http://code.google.com/p/jigsaw-library/source/browse/trunk/Unifier.cs上在线查找其余的算法代码.不是在这个例子中,List该类实际上是我为库实现的Lisp样式列表.


Sam*_*son 1

表示类型变体的最佳方式是继承。例如,表达式序列仍然只是一个表达式。空列表将由序列对象中的零长度容器表示。因此,对于失败,返回 null 是可以接受的,我用“?”表示。我不确定 C# 是否真的有“?”,但应该明白了。

abstract class Expression { ... }
class Atom : Expression { ... }
class Variable : Expression { ... }
class Sequence : Expression { List <Expression> ... }

Expression? unify (Expression e1, Expression e2) { ... }
Run Code Online (Sandbox Code Playgroud)

编辑:该列表是协变的。看这里。我的 C# Vala 方言支持此功能(目前),但我不相信 .net 可以。