:- prompt(_, '').
:- use_module(library(readutil)).
% STLC type system specification
type
(C
,var(X
), T1
) :- first
(X
:T
,C
), instantiate
(T
,T1
). type(C,lam(X,E),A -> B) :- type([X:mono(A)|C],E,B).
type(C,X $ Y, B) :- type(C,X,A -> B), type(C,Y,A).
type(C,let(X=E0,E1), T) :- type(C,E0,A), type([X:poly(C,A)|C],E1,T).
instantiate
(poly
(C
,T
),T1
) :- copy_term(t
(C
,T
),t
(C
,T1
)). instantiate(mono(T),T).
first(K:V,[K1:V1|_]) :- K = K1, V=V1.
first(K:V,[K1:_|Xs]) :- K\==K1, first(K:V, Xs). % no cut but use var cmp
main:-
process,
type_and_print(C,E,T) :- type(C,E,T), print(E : T).
process:-
/* your code goes here */
type_and_print
([],lam
(x
,var(x
)),_
), nl, type_and_print
([],lam
(x
,lam
(y
,var(y
)$var
(x
))),_
), nl, type_and_print
([],let
(id
=lam
(x
,var(x
)),var(id
)$var
(id
)),_
), nl,
:- main.
Oi0gc2V0X3Byb2xvZ19mbGFnKHZlcmJvc2Usc2lsZW50KS4KOi0gcHJvbXB0KF8sICcnKS4KOi0gdXNlX21vZHVsZShsaWJyYXJ5KHJlYWR1dGlsKSkuCgo6LSBzZXRfcHJvbG9nX2ZsYWcob2NjdXJzX2NoZWNrLHRydWUpLgo6LSBvcCg1MDAseGZ5LCQpLgoKJSBTVExDIHR5cGUgc3lzdGVtIHNwZWNpZmljYXRpb24KCnR5cGUoQyx2YXIoWCksICAgICAgVDEpIDotIGZpcnN0KFg6VCxDKSwgaW5zdGFudGlhdGUoVCxUMSkuICAgICAgICAgICAgICAgICAgICAgICAgICAgIAp0eXBlKEMsbGFtKFgsRSksQSAtPiBCKSA6LSB0eXBlKFtYOm1vbm8oQSl8Q10sRSxCKS4gICAgICAgICAgICAgICAgICAgICAgICAgICAgIAp0eXBlKEMsWCAkIFksICAgICAgICBCKSA6LSB0eXBlKEMsWCxBIC0+IEIpLCB0eXBlKEMsWSxBKS4gICAgICAgICAgICAgICAgICA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