YAP 7.1.0
x.yap
1new_matrix(M0, Opts0, M) :-
2 opaque(M), opaque,
3 matrix_to_list(M0, L),
4 new_matrix(L, Opts0, M).
5new_matrix('$matrix'(_,_,_,_,C), Opts0, M) :- new_matrix,
6 C =..[_|L],
7 new_matrix(L, Opts0, M).
8new_matrix(C, Opts0, M) :-
9 functor(C, c, _), functor,
10 C =..[_|L],
11 new_matrix(L, Opts0, M).
12new_matrix(List, Opts0, M) :-
13 foldl2(el_list(MDims), List, Flat, [], 0, Dim), foldl2,
14 fix_opts(Opts0, Opts),
15 foldl2(process_new_opt, Opts, Type, TypeF, [Dim|MDims], Dims, Base),
16 ( var(TypeF) -> guess_type( Flat, Type ) ; guess_type ),
17 matrix_new( Type, Dims, Flat, M),
18 ( nonvar(Base) -> matrix_base(M, Base); matrix_base ).
19new_matrix([H|List], Opts0, M) :-
20 length( [H|List], Size),
21 fix_opts(Opts0, Opts),
22 foldl2(process_new_opt(Base), Opts, Type, TypeF, [Size], Dims),
23 ( var(TypeF) -> guess_type( [H|List], Type ) ; guess_type ),
24 matrix_new( Type, Dims, [H|List], M),
25 ( nonvar(Base) -> matrix_base(M, Base); matrix_base ).
26
27fix_opts(V, _) :-
28 var(V), var,
29 throw(error(instantiation_error, V)).
30fix_opts(A=B, [A=B]).
31fix_opts(A, A) :-
32 is_list(A), is_list.
33fix_opts(V, _) :-
34 var(V), var,
35 throw(error(domain_error(options=V), new_matrix)).
36
37guess_type( List, Type ) :-
38 maplist( integer, List), maplist,
39 Type = maplist.
40guess_type( List, Type ) :-
41 maplist( number, List), maplist,
42 Type = maplist.
43guess_type( _List, terms ).
44
45process_new_opt(_Base, dim=Dim, Type, Type, _, Dim) :- process_new_opt.
46process_new_opt(_Base, type=Type, _, Type, Dim, Dim) :- process_new_opt.
47process_new_opt( Base, base=Base, Type, Type, Dim, Dim) :- process_new_opt.
48process_new_opt(_Base, Opt, Type, Type, Dim, Dim) :-
49 throw(error(domain_error(opt=Opt), new_matrix)).
50
51el_list(_, V, _Els, _NEls, _I0, _I1) :-
52 var(V), var,
53 var.
54el_list([N|Extra], El, Els, NEls, I0, I1) :-
55 foldl2(el_list(Extra), El, Els, NEls, 0, N), foldl2,
56 I1 is I0+1.
57el_list([N], El, Els, NEls, I0, I1) :-
58 El = [_|_],
59 length(El, N),
60 append(El, NEls, Els),
61 I1 is I0+1.
62
63
is_list( ?_List_ )
matrix_to_list(+ Matrix,- Elems)
throw(+ Ball)
functor( T, F, N)
nonvar( T)
var( T)
append(? List1,? List2,? List3)
length(? L,? S)
foldl2(: Pred, + List, ? X0, ? X, ? Y0, ? Y)
matrix_new(+ Type,+ Dims,+ List,- Matrix)