Prolog(swi-pl)中长度/ 2的逻辑推断数

时间:2016-05-07 13:11:56

标签: prolog swi-prolog

我期望内置长度/ 2谓词在逻辑推理的数量上是线性的。但是,它似乎是不变的:

class Regex {
    let pattern: String
    let internalExpression: NSRegularExpression

    init?(_ pattern: String) {
        do {
            self.pattern = pattern
            self.internalExpression = try NSRegularExpression(pattern: pattern, options: .CaseInsensitive)
        } catch {
            print(error)
            return nil
        }
    }

    func test(input: String) -> Bool {
        let matches = self.internalExpression.matchesInString(input, options: [], range:NSMakeRange(0, input.characters.count))
        return matches.count > 0
    }
}

这是因为程序被委托给C吗?我在SWIPL代码库中找不到相关的代码。

1 个答案:

答案 0 :(得分:3)

length / 2在init.pl中的第3230行附近有一个浅但强大的接口。从那里它调用'$skip_list'(Length0, List, Tail),列表C接口的'瑞士刀'。

您可以在src / pl-prims.c,第2377行及以下内容中找到它:

/** '$skip_list'(-Length, +Xs0, -Xs) is det.

Xs0, Xs is a pair of list differences. Xs0   is the input list and Xs is
the minimal remaining list. Examination of   Xs  permits to classify the
list Xs0:

        Xs        | list type of Xs0   | Length
        []    ... | well formed        | length
        Var   ... | partial            | elements skipped
        [_|_] ... | infinite           | upper bound for cycle
        Term  ... | malformed          | elements skipped
*/
PRED_IMPL("$skip_list", 3, skip_list, 0)
...