Skip to content

Instantly share code, notes, and snippets.

@tkob
Created June 24, 2018 22:58
Show Gist options
  • Select an option

  • Save tkob/b4fc811dab5a90ad75b2cbb08111861a to your computer and use it in GitHub Desktop.

Select an option

Save tkob/b4fc811dab5a90ad75b2cbb08111861a to your computer and use it in GitHub Desktop.
type_of(return(Values), _, [Arity]) :- length(Values, Arity).
type_of(if(Then, Else), Env, T) :-
type_of(Then, Env, T1),
type_of(Else, Env, T2),
union(T1, T2, T).
type_of(fun([]), _, [] -> []).
type_of(fun([Args->Body|Cdr]), Env, TArity->TBody) :-
length(Args, TArityCar),
type_of(Body, Env, TBodyCar),
type_of(fun(Cdr), Env, TArityCdr->TBodyCdr),
union([TArityCar], TArityCdr, TArity),
union(TBodyCar, TBodyCdr, TBody).
union2(S1, S2, U) :-
is_list(S1),
is_list(S2),
union(S1, S2, U).
union2(S1, (S2, Var), (U2, Var)) :-
is_list(S1),
is_list(S2),
union(S1, S2, U1),
lowpass(U1, Var, U2).
union2((S1, Var), S2, X) :-
is_list(S1),
is_list(S2),
union2(S2, (S1, Var), X).
union2((S1, Var1), (S2, Var2), X) :-
is_list(S1),
is_list(S2),
Var1 >= Var2, !,
union2(S1, (S2, Var2), X).
union2((S1, Var1), (S2, Var2), X) :-
is_list(S1),
is_list(S2),
Var1 < Var2,
union2(S2, (S1, Var1), X).
lowpass([], _, []).
lowpass([H|T], Cutoff, X) :-
H >= Cutoff,
lowpass(T, Var, X).
lowpass([H|T], Cutoff, [H|L]) :-
H < Cutoff,
lowpass(T, Var1, L).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment