1:- module(type_list,
2 [
3 op(100, yf, []), 4 op(500, yfx, \\), 5 slice_parameters/4, 6 index_parameters/3 7 ]). 8
9:- current_module(arithmetic_types) -> true ; use_module(library(arithmetic_types)). 10
14
15:- arithmetic_function(new/2). 16:- arithmetic_function('[|]'/2). 17:- arithmetic_function([]/1). 18:- arithmetic_function([]/2). 19:- arithmetic_function(: /2). 20:- arithmetic_function(len/1). 21:- arithmetic_function(init/2). 22:- arithmetic_function(\\ /2). 23:- arithmetic_function(arange/2). 24:- arithmetic_function(arange/3). 25:- arithmetic_function(arange/4). 26
31slice_parameters(B:E,Len,SBegin,SLen) :-
32 item_eval(B,Br), (var(Br) -> Br=0 ; integer(Br)),
33 item_eval(E,Er), (var(Er) -> Er=Len ; integer(Er)),
34 (Br<0 -> SBegin is Len+Br ; SBegin=Br),
35 (Er<0 -> SLen is Len+Er-SBegin ; SLen is Er-SBegin).
36
38index_parameters(Ix,Len,I) :-
39 item_eval(Ix,EIx),
40 integer(EIx),
41 (EIx < 0 -> I is Len+EIx ; I = EIx),
42 I >= 0.
43
45item_eval(X,X) :- var(X), !. 46item_eval(N,N) :- number(N), !. 47item_eval(X,R) :-
48 catch(arithmetic_expression_value(X,R), _, R=X). 49
53':'(B,E,B:E).
54
58'[|]'(X,Xs,[X|Xs]). 59
63new(list,Size,L) :- integer(Size), Size >= 0, (nonvar(L) -> is_list(L) ; true), !,
64 length(L,Size).
65
66new(list,Xs,Xs) :- is_list(Xs).
67
71[](L, L) :- is_list(L).
72[]([I1,I2|IN],T,X) :- !, 73 T1 is T[I1], 74 X is T1[I2|IN].
75[]([B:E],L,X) :- is_list(L),
76 length(L,Len),
77 slice_parameters(B:E,Len,SB,SL), !,
78 sub_list(L,SB,SL,_,X).
79[]([Ix], L, R) :- is_list(L),
80 length(L,Len),
81 index_parameters(Ix,Len,I),
82 83 (I =< 28 -> skip_N(I,L,[X|_]) ; (T=..[$|L], arg(I,T,X))),
84 item_eval(X,R). 85
87sub_list(L,Before,Length,After,SubL) :- integer(Before), integer(Length), is_list(L),
88 skip_N(Before,L,L1), 89 next_N(Length,L1,SubL,L2), 90 length(L2,After), 91 !. 92
93skip_N(0,In,In):- !.
94skip_N(1,[_|In],In):- !.
95skip_N(N,[_,_|Xs],Out) :- 96 N1 is N-2,
97 skip_N(N1,Xs,Out).
98
99next_N(0,In,[],In) :- !.
100next_N(1,[X|In],[X],In) :- !.
101next_N(N,[X1,X2|In],[X1,X2|Out],Rem) :- 102 N1 is N-2,
103 next_N(N1,In,Out,Rem).
104
108len(L,N) :- is_list(L),
109 length(L,N).
110
114init(L, Value, L) :- is_list(L),
115 fill_each(L,Value).
116
117fill_each([],_).
118fill_each([X|Xs],Value) :-
119 (is_list(X)
120 -> fill_each(X,Value)
121 ; (var(X) -> X=Value ; true)
122 ),
123 fill_each(Xs,Value).
124
128\\(L1, L2, R) :- nonvar(L1), is_list(L2), 129 append_det(L1,L2,R).
130
131append_det([], L, L) :- !. 132append_det([H|T], L, [H|R]) :-
133 append_det(T, L, R).
134
138arange(list,N,L) :- number(N), N>0,
139 arange_(0,N,1,L).
140
141arange(list,B,E,L) :- number(B), number(E),
142 B>=0, E>B,
143 arange_(B,E,1,L).
144
145arange(list,B,E,S,L) :- number(B), number(E), number(S),
146 B>=0, E>B, S>0,
147 arange_(B,E,S,L).
148
149arange_(B,E,_S,[]) :- B>=E, !.
150arange_(B,E,S,[B|Vs]) :-
151 B1 is B+S,
152 arange_(B1,E,S,Vs)