-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetric_space.lgt
More file actions
64 lines (54 loc) · 2.05 KB
/
Copy pathmetric_space.lgt
File metadata and controls
64 lines (54 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
:- protocol(metric_space).
:- public([distance/3]).
:- end_protocol.
%% The input set of a metric space is a domain as a set.
%% Doesn't like floats of course, but this is the most perfect solution symbolically
:- object(integer_cartesian(_S_), implements(metric_space)).
:- use_module(library(clpfd)).
squared_pythagorean_distance(X1, X2, P) :-
X1 \= (_, _), X2 \= (_, _),
X #= X1 - X2, P #= X ^ 2.
squared_pythagorean_distance((X1, Rest1), (X2, Rest2), P) :-
X #= X1 - X2,
P #= X ^ 2 + P1,
squared_pythagorean_distance(Rest1, Rest2, P1).
distance(X, Y, D) :-
implements_protocol(_S_, set),
_S_::member(X), _S_::member(Y),
squared_pythagorean_distance(X, Y, Square),
Square #= D * D.
:- end_object.
%% Allows some completeness, but requires integers and outputs floats
:- object(pseudointeger_cartesian(_S_), implements(metric_space)).
:- use_module(library(clpfd)).
squared_pythagorean_distance(X1, X2, P) :-
X1 \= (_, _), X2 \= (_, _),
X #= X1 - X2, P #= X ^ 2.
squared_pythagorean_distance((X1, Rest1), (X2, Rest2), P) :-
X #= X1 - X2,
P #= X ^ 2 + P1,
squared_pythagorean_distance(Rest1, Rest2, P1).
distance(X, Y, D) :-
implements_protocol(_S_, set),
_S_::member(X), _S_::member(Y),
squared_pythagorean_distance(X, Y, Square),
D is sqrt(Square).
:- end_object.
%% Pure floats, takes anything, also means it will generate anything
:- object(float_cartesian(_S_), implements(metric_space)).
:- use_module(library(clpfd)).
squared_pythagorean_distance(X1, X2, P) :-
X1 \= (_, _), X2 \= (_, _),
X is X1 - X2,
P is X ** 2.
squared_pythagorean_distance((X1, Rest1), (X2, Rest2), P) :-
X is X1 - X2,
XS is X ** 2,
squared_pythagorean_distance(Rest1, Rest2, P1),
P is P1 + XS.
distance(X, Y, D) :-
implements_protocol(_S_, set),
_S_::member(X), _S_::member(Y),
squared_pythagorean_distance(X, Y, Square),
D is sqrt(Square).
:- end_object.