我打算以下面的內容爲出發點。這裏是我的形式化:
type(string).
意味着字符串是一個變量可用的類型是
signature(=, [X, X, nil]).
實例意味着管道符=需要兩個同類型的參數,並且沒有返回。
is_instance(X, Y)
意味着X
是Y
型要進行測試的一個實例,我創建包含在「聲明」的名單我我test/0
謂詞的變量Input
。然後,我遞歸測試如果事情順利。您必須將第三個子句作爲遞歸調用來查找表達式中的類型現在是否可用。
我做了什麼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).
我希望它能幫助您入門。
來源
2012-04-26 20:09:42
m09
在Prolog中爲像Java這樣的語言實現類型檢查器絕對不是Prolog中的第一個任務。你必須先學習Prolog! – false 2012-04-26 10:50:11
這是如何用一些語言整個學期!哦,井。我同意,不是最好的學習方式。 – Andy 2012-04-26 14:16:50
@Andy:請添加更多關於作業的信息,atm它不清楚問什麼,我們不能真正幫助 – m09 2012-04-26 14:51:24