Last active
September 14, 2018 17:26
-
-
Save pmoura/c04dedc03ce4e0e820e82bd1afc56dc8 to your computer and use it in GitHub Desktop.
Interpolation polymorphic example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% use the apply module instead of the Logtalk meta | |
% library object to illustrate interoperability | |
:- use_module(library(apply)). | |
:- use_module(library(apply_macros)). | |
:- category(math). | |
:- public(interpolate/4). | |
interpolate(K1, X1, X2, Y) :- | |
K2 is 1 - K1, | |
::mul(K1, X1, KX1), | |
::mul(K2, X2, KX2), | |
::add(KX1, KX2, Y). | |
:- protected([mul/3, add/3]). | |
:- end_category. | |
:- object(floats, imports(math)). | |
add(X, Y, Z) :- Z is X + Y. | |
mul(X, Y, Z) :- Z is X * Y. | |
:- end_object. | |
:- object(vector, imports(math)). | |
add(X, Y, Z) :- apply:maplist(plus, X, Y, Z). | |
plus(X, Y, Z) :- Z is X+Y. | |
mul(X, Y, Z) :- apply:maplist(times(X), Y, Z). | |
times(X, Y, Z) :- Z is X*Y. | |
:- end_object. | |
% sample calls: | |
% | |
% ?- floats::interpolate(0.2, 10, 20, F). | |
% F = 18.0. | |
% | |
% ?- vector::interpolate(0.2, [10,5,2], [20,30,10], V). | |
% V = [18.0, 25.0, 8.4]. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment