YAP 7.1.0
arrays.yap
Go to the documentation of this file.
1/*************************************************************************
2* *
3* YAP Prolog *
4* *
5* Yap Prolog was developed at NCCUP - Universidade do Porto *
6* *
7* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
8* *
9**************************************************************************
10* *
11* File: arrays.yap *
12* Last rev: *
13* mods: *
14* comments: Array Manipulation *
15* *
16*************************************************************************/
17
18%% @file pl/arrays.yap
19
20%% @brief Prolog support for old arrays code
21
22/**
23 @addtogroup YAPArrays
24 @{
25
26*/
27%
28% These are the array built-in predicates. They will only work if
29% YAP_ARRAYS is defined in Yap.h
30%
31
32/** @pred array( +Name, +Size )
33
34
35Creates a new dynamic array. The array is allocated in the global stack (or heap). Elements can be any term.xs
36
37The _Size_ must evaluate to an
38integer. The _Name_ may be either an atom (named array) or an
39unbound variable (anonymous array).
40
41Dynamic arrays are just a way to refer compound terms. Hence, space for
42the array is recovered automatically on backtracking.
43
44
45*/
46array(Obj, Size) :-
47 '$create_array'(Obj, Size).
48
49
50% arithmetical optimization
51'$c_arrays'((P:-Q),(NP:-QF)) :- '$c_arrays',
52 '$c_arrays_body'(Q, QI),
53 '$c_arrays_head'(P, NP, QI, QF).
54'$c_arrays'(P, NP) :-
55 '$c_arrays_fact'(P, NP).
56
57'$c_arrays_body'(P, P) :-
58 var(P), var.
59'$c_arrays_body'((P0,Q0), (P,Q)) :- '$c_arrays_body',
60 '$c_arrays_body'(P0, P),
61 '$c_arrays_body'(Q0, Q).
62'$c_arrays_body'((P0;Q0), (P;Q)) :- '$c_arrays_body',
63 '$c_arrays_body'(P0, P),
64 '$c_arrays_body'(Q0, Q).
65'$c_arrays_body'((P0->Q0), (P->Q)) :- '$c_arrays_body',
66 '$c_arrays_body'(P0, P),
67 '$c_arrays_body'(Q0, Q).
68'$c_arrays_body'(P, NP) :- '$c_arrays_lit'(P, NP).
69
70%
71% replace references to arrays to references to built-ins.
72%
73'$c_arrays_lit'(G, GL) :-
74 '$array_references'(G, NG, VL),
75 '$add_array_entries'(VL, NG, GL).
76
77'$c_arrays_head'(G, NG, B, NB) :-
78 '$array_references'(G, NG, VL),
79 '$add_array_entries'(VL, B, NB).
80
81'$c_arrays_fact'(G, NG) :-
82 '$array_references'(G, IG, VL),
83 (VL = [] -> NG = G;
84 NG = (IG :- NB), '$add_array_entries'(VL, true, NB)).
85
86'$add_array_entries'([], NG, NG).
87'$add_array_entries'([Head|Tail], G, (Head, NG)) :-
88 '$add_array_entries'(Tail, G, NG).
89
90
91/** @pred static_array_properties(? _Name_, ? _Size_, ? _Type_)
92
93
94Show the properties size and type of a static array with name
95 _Name_. Can also be used to enumerate all current
96static arrays.
97
98This built-in will silently fail if the there is no static array with
99that name.
100
101
102*/
103static_array_properties(Name, Size, Type) :-
104 atom(Name), atom,
105 '$static_array_properties'(Name, Size, Type).
106static_array_properties(Name, Size, Type) :-
107 var(Name), var,
108 current_atom(Name),
109 '$static_array_properties'(Name, Size, Type).
110static_array_properties(Name, Size, Type) :-
111 '$do_error'(type_error(atom,Name),static_array_properties(Name,Size,Type)).
112
113%% @}
114
current_atom( A)
static_array_properties(+Name,+Size,+Type)
array( +Name, +Size )
atom( T)
var( T)