我打算将以下内容作为起点。这是我的形式化:
type(string).
意味着 string 是变量的可用类型
signature(=, [X, X, nil]).
意味着中缀运算符 = 接受两个相同类型的参数并且不返回任何内容。
is_instance(X, Y)
意味着这X
是一个类型的实例Y
为了测试事情,我创建了一个包含在我的谓词变量中的“语句”Input
列表test/0
。然后我递归地测试事情是否正常。您必须将第三个子句实现为递归调用,以查明现在表达式中的类型是否正常。
我所做的 atm 是在我的第一个主要check/2
条款中,我处理instance/2
术语,而在接下来的一个中,我处理所有其余部分。
:- dynamic is_instance/2.
type(string).
type(int).
signature(=, [X, X, nil]).
test :-
retractall(is_instance(_, _)),
Input = [instance(s, string), instance(i, int), =(i, length(s))],
check(Input, ReturnTypes),
check([], []).
check([instance(Variable, Type)|Terms], [nil|ReturnTypes]) :-
!,
( is_instance(Variable, _) -> syntax_error('Variable already declared')
; \+ type(Type) -> syntax_error('Using a non-existing type'),
; Term =.. [is_instance, Variable, Type],
assertz(Term)),
check(Terms, ReturnTypes).
check([Term|Terms], [Type|ReturnTypes]) :-
Term =.. [Name|Arguments],
% Here we have to call ourselves with our list of arguments
% and then check that everything is fine and then we'll unify Type
% with the return value of Name.
check(Terms, ReturnTypes).
我希望它能帮助你开始。