ndtypes 0.2.0dev4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CONTRIBUTING.md +50 -0
- data/Gemfile +2 -0
- data/History.md +0 -0
- data/README.md +19 -0
- data/Rakefile +125 -0
- data/ext/ruby_ndtypes/extconf.rb +55 -0
- data/ext/ruby_ndtypes/gc_guard.c +36 -0
- data/ext/ruby_ndtypes/gc_guard.h +12 -0
- data/ext/ruby_ndtypes/ndtypes/AUTHORS.txt +5 -0
- data/ext/ruby_ndtypes/ndtypes/INSTALL.txt +101 -0
- data/ext/ruby_ndtypes/ndtypes/LICENSE.txt +29 -0
- data/ext/ruby_ndtypes/ndtypes/MANIFEST.in +3 -0
- data/ext/ruby_ndtypes/ndtypes/Makefile.in +87 -0
- data/ext/ruby_ndtypes/ndtypes/README.rst +47 -0
- data/ext/ruby_ndtypes/ndtypes/config.guess +1530 -0
- data/ext/ruby_ndtypes/ndtypes/config.h.in +67 -0
- data/ext/ruby_ndtypes/ndtypes/config.sub +1782 -0
- data/ext/ruby_ndtypes/ndtypes/configure +5260 -0
- data/ext/ruby_ndtypes/ndtypes/configure.ac +161 -0
- data/ext/ruby_ndtypes/ndtypes/doc/Makefile +14 -0
- data/ext/ruby_ndtypes/ndtypes/doc/_static/copybutton.js +66 -0
- data/ext/ruby_ndtypes/ndtypes/doc/conf.py +26 -0
- data/ext/ruby_ndtypes/ndtypes/doc/grammar/grammar.rst +27 -0
- data/ext/ruby_ndtypes/ndtypes/doc/index.rst +56 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/context.rst +131 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/encodings.rst +68 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/fields-values.rst +175 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/functions.rst +72 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/index.rst +43 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/init.rst +48 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/io.rst +100 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/memory.rst +124 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/predicates.rst +110 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/typedef.rst +31 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/types.rst +594 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/util.rst +166 -0
- data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/buffer-protocol.rst +27 -0
- data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/index.rst +21 -0
- data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/pattern-matching.rst +330 -0
- data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/quickstart.rst +144 -0
- data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/types.rst +544 -0
- data/ext/ruby_ndtypes/ndtypes/doc/releases/index.rst +35 -0
- data/ext/ruby_ndtypes/ndtypes/install-sh +527 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/Makefile.in +271 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/Makefile.vc +269 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/alloc.c +230 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/attr.c +268 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/attr.h +109 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/Makefile.in +73 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/Makefile.vc +70 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/README.txt +16 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.c +2179 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.h +134 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.y +428 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.c +2543 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.h +735 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.l +176 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/export.c +543 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/import.c +110 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/context.c +228 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/copy.c +634 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/encodings.c +116 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/equal.c +288 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.c +3067 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.h +180 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.y +417 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/io.c +1658 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.c +2773 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.h +734 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.l +222 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/match.c +1132 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/ndtypes.c +2323 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/ndtypes.h.in +893 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/overflow.h +161 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/parsefuncs.c +473 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/parsefuncs.h +92 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/parser.c +246 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/seq.c +269 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/seq.h +197 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/Makefile.in +48 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/Makefile.vc +46 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/deserialize.c +1007 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/serialize.c +442 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/slice.h +42 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/substitute.c +238 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/substitute.h +50 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/symtable.c +371 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/symtable.h +100 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/Makefile.in +55 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/Makefile.vc +45 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/alloc_fail.c +82 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/alloc_fail.h +49 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/runtest.c +1657 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test.h +85 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_array.c +115 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_buffer.c +137 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_indent.c +201 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_match.c +2397 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_numba.c +57 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse.c +349 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse_error.c +27839 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse_roundtrip.c +350 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_record.c +231 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_typecheck.c +375 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_typedef.c +65 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/valgrind.supp +30 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/bench.c +79 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/indent.c +94 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/print_ast.c +96 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/util.c +474 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/values.c +228 -0
- data/ext/ruby_ndtypes/ndtypes/python/bench.py +49 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndt_randtype.py +409 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndt_support.py +14 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndtypes/__init__.py +70 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndtypes/_ndtypes.c +1332 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndtypes/docstrings.h +319 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndtypes/pyndtypes.h +154 -0
- data/ext/ruby_ndtypes/ndtypes/python/test_ndtypes.py +1977 -0
- data/ext/ruby_ndtypes/ndtypes/setup.py +288 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/INSTALL.txt +41 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/runtest32.bat +15 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/runtest64.bat +13 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/vcbuild32.bat +38 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/vcbuild64.bat +38 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/vcclean.bat +13 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/vcdistclean.bat +14 -0
- data/ext/ruby_ndtypes/ruby_ndtypes.c +1003 -0
- data/ext/ruby_ndtypes/ruby_ndtypes.h +37 -0
- data/ext/ruby_ndtypes/ruby_ndtypes_internal.h +28 -0
- data/lib/ndtypes.rb +45 -0
- data/lib/ndtypes/errors.rb +2 -0
- data/lib/ndtypes/version.rb +6 -0
- data/ndtypes.gemspec +47 -0
- data/spec/gc_table_spec.rb +10 -0
- data/spec/ndtypes_spec.rb +289 -0
- data/spec/spec_helper.rb +241 -0
- metadata +242 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
.. meta::
|
|
2
|
+
:robots: index,follow
|
|
3
|
+
:description: libndtypes documentation
|
|
4
|
+
|
|
5
|
+
.. sectionauthor:: Stefan Krah <skrah at bytereef.org>
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
Utilities
|
|
9
|
+
=========
|
|
10
|
+
|
|
11
|
+
This section contains utility functions that are meant to be used by other
|
|
12
|
+
applications. Some of these functions are not yet in the stable API and
|
|
13
|
+
are subject to change.
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
Stable API
|
|
17
|
+
----------
|
|
18
|
+
|
|
19
|
+
.. topic:: ndt_strdup
|
|
20
|
+
|
|
21
|
+
.. code-block:: c
|
|
22
|
+
|
|
23
|
+
char *ndt_strdup(const char *s, ndt_context_t *ctx);
|
|
24
|
+
|
|
25
|
+
Same as :func:`strdup`, but uses libndtypes's custom allocators. On failure,
|
|
26
|
+
set an error in the context and return :c:macro:`NULL`. The result must be
|
|
27
|
+
deallocated using :func:`ndt_free`.
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
.. topic:: ndt_asprintf
|
|
31
|
+
|
|
32
|
+
.. code-block:: c
|
|
33
|
+
|
|
34
|
+
char *ndt_asprintf(ndt_context_t *ctx, const char *fmt, ...);
|
|
35
|
+
|
|
36
|
+
Print to a string allocated by libndtypes's custom allocators. On failure,
|
|
37
|
+
set an error in the context and return :c:macro:`NULL`. The result must be
|
|
38
|
+
deallocated using :func:`ndt_free`.
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
.. topic:: ndt_strtobool
|
|
42
|
+
|
|
43
|
+
.. code-block:: c
|
|
44
|
+
|
|
45
|
+
bool ndt_strtobool(const char *v, ndt_context_t *ctx);
|
|
46
|
+
|
|
47
|
+
Convert string *v* to a bool. *v* must be "true" or "false". Return *0*
|
|
48
|
+
and set :c:macro:`NDT_InvalidArgumentError` if the conversion fails.
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
.. topic:: ndt_strtochar
|
|
52
|
+
|
|
53
|
+
.. code-block:: c
|
|
54
|
+
|
|
55
|
+
char ndt_strtochar(const char *v, ndt_context_t *ctx);
|
|
56
|
+
|
|
57
|
+
Convert string *v* to a char. *v* must have length *1*. Return *0* and
|
|
58
|
+
set :c:macro:`NDT_InvalidArgumentError` if the conversion fails.
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
.. topic:: ndt_strtol
|
|
63
|
+
|
|
64
|
+
.. code-block:: c
|
|
65
|
+
|
|
66
|
+
char ndt_strtol(const char *v, ndt_context_t *ctx);
|
|
67
|
+
|
|
68
|
+
Convert string *v* to a long. In case of an error, use the return value
|
|
69
|
+
from :func:`strtol`.
|
|
70
|
+
|
|
71
|
+
If *v* is not an integer, set :c:macro:`NDT_InvalidArgumentError`.
|
|
72
|
+
|
|
73
|
+
If *v* is out of range, set :c:macro:`NDT_ValueError`.
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
.. code-block:: c
|
|
77
|
+
|
|
78
|
+
long long ndt_strtoll(const char *v, long long min, long long max, ndt_context_t *ctx);
|
|
79
|
+
|
|
80
|
+
Convert string *v* to a long long.
|
|
81
|
+
|
|
82
|
+
If *v* is not an integer, set :c:macro:`NDT_InvalidArgumentError`.
|
|
83
|
+
|
|
84
|
+
If *v* is not in the range [*min*, *max*] , set :c:macro:`NDT_ValueError`.
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
.. code-block:: c
|
|
89
|
+
|
|
90
|
+
unsigned long long ndt_strtoll(const char *v, long long min, long long max, ndt_context_t *ctx);
|
|
91
|
+
|
|
92
|
+
Convert string *v* to an unsigned long long.
|
|
93
|
+
|
|
94
|
+
If *v* is not an integer, set :c:macro:`NDT_InvalidArgumentError`.
|
|
95
|
+
|
|
96
|
+
If *v* is not in the range [*min*, *max*] , set :c:macro:`NDT_ValueError`.
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
.. code-block:: c
|
|
100
|
+
|
|
101
|
+
float ndt_strtof(const char *v, ndt_context_t *ctx);
|
|
102
|
+
|
|
103
|
+
Convert string *v* to a float.
|
|
104
|
+
|
|
105
|
+
If *v* is not an integer, set :c:macro:`NDT_InvalidArgumentError`.
|
|
106
|
+
|
|
107
|
+
If *v* is out of range, set :c:macro:`NDT_ValueError`.
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
.. code-block:: c
|
|
111
|
+
|
|
112
|
+
double ndt_strtod(const char *v, ndt_context_t *ctx);
|
|
113
|
+
|
|
114
|
+
Convert string *v* to a double.
|
|
115
|
+
|
|
116
|
+
If *v* is not an integer, set :c:macro:`NDT_InvalidArgumentError`.
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
If *v* is out of range, set :c:macro:`NDT_ValueError`.
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
Unstable API
|
|
123
|
+
------------
|
|
124
|
+
|
|
125
|
+
.. topic:: ndt_dtype
|
|
126
|
+
|
|
127
|
+
.. code-block:: c
|
|
128
|
+
|
|
129
|
+
const ndt_t *ndt_dtype(const ndt_t *t);
|
|
130
|
+
|
|
131
|
+
Return the dtype (element type) of an array. If the argument is not an array,
|
|
132
|
+
return *t* itself. The function cannot fail.
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
.. topic:: ndt_dims_dtype
|
|
136
|
+
|
|
137
|
+
.. code-block:: c
|
|
138
|
+
|
|
139
|
+
int ndt_dims_dtype(const ndt_t *dims[NDT_MAX_DIM], const ndt_t **dtype, const ndt_t *t);
|
|
140
|
+
|
|
141
|
+
Extract constant pointers to the dimensions and the dtype of an array and return
|
|
142
|
+
the number of dimensions. The function cannot fail.
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
.. topic:: ndt_as_ndarray
|
|
146
|
+
|
|
147
|
+
.. code-block:: c
|
|
148
|
+
|
|
149
|
+
int ndt_as_ndarray(ndt_ndarray_t *a, const ndt_t *t, ndt_context_t *ctx);
|
|
150
|
+
|
|
151
|
+
Convert *t* to its ndarray representation *a*. On success, return 0.
|
|
152
|
+
If *t* is abstract or not representable as an ndarray, set an error in
|
|
153
|
+
the context and return -1.
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
.. topic:: ndt_hash
|
|
157
|
+
|
|
158
|
+
.. code-block:: c
|
|
159
|
+
|
|
160
|
+
ndt_ssize_t ndt_hash(ndt_t *t, ndt_context_t *ctx);
|
|
161
|
+
|
|
162
|
+
Hash a type. This is currently implemented by converting the type to its
|
|
163
|
+
string representation and hashing the string.
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
.. meta::
|
|
2
|
+
:robots: index,follow
|
|
3
|
+
:description: ndtypes
|
|
4
|
+
:keywords: ndtypes, buffer protocol
|
|
5
|
+
|
|
6
|
+
.. sectionauthor:: Stefan Krah <skrah at bytereef.org>
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
Buffer protocol
|
|
10
|
+
===============
|
|
11
|
+
|
|
12
|
+
ndtypes supports conversion from PEP-3118 format strings to datashape:
|
|
13
|
+
|
|
14
|
+
.. doctest::
|
|
15
|
+
|
|
16
|
+
>>> from ndtypes import ndt
|
|
17
|
+
>>> ndt.from_format("T{<b:a:Q:b:}")
|
|
18
|
+
ndt("{a : <int8, b : uint64}")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
Note that there are a couple of open issues around the buffer protocol,
|
|
22
|
+
e.g. https://bugs.python.org/issue26746 .
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
.. meta::
|
|
2
|
+
:robots: index, follow
|
|
3
|
+
:description: ndtypes documentation
|
|
4
|
+
:keywords: Python, array computing
|
|
5
|
+
|
|
6
|
+
.. sectionauthor:: Stefan Krah <skrah at bytereef.org>
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
ndtypes
|
|
10
|
+
-------
|
|
11
|
+
|
|
12
|
+
ndtypes is a Python module based on libndtypes.
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
.. toctree::
|
|
17
|
+
|
|
18
|
+
quickstart.rst
|
|
19
|
+
types.rst
|
|
20
|
+
pattern-matching.rst
|
|
21
|
+
buffer-protocol.rst
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
.. meta::
|
|
2
|
+
:robots: index,follow
|
|
3
|
+
:description: ndtypes pattern matching
|
|
4
|
+
:keywords: ndtypes, pattern matching
|
|
5
|
+
|
|
6
|
+
.. sectionauthor:: Stefan Krah <skrah at bytereef.org>
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
################
|
|
11
|
+
Pattern matching
|
|
12
|
+
################
|
|
13
|
+
|
|
14
|
+
The libndtypes implementation of datashape is dynamically typed with strict type
|
|
15
|
+
checking. Static type checking of datashape would be far more complex, since
|
|
16
|
+
datashape allows dependent types [#f1]_, i.e. types depending on values.
|
|
17
|
+
|
|
18
|
+
Dynamic pattern matching is used for checking function arguments, return
|
|
19
|
+
values, broadcasting and general array functions.
|
|
20
|
+
|
|
21
|
+
Again, we will be using the :py:mod:`ndtypes` module included in
|
|
22
|
+
`ndtypes <https://github.com/plures/ndtypes>`_ to demonstrate
|
|
23
|
+
datashape pattern matching. The rest of this document assumes that the
|
|
24
|
+
:py:mod:`ndtypes` module has been imported:
|
|
25
|
+
|
|
26
|
+
.. testcode::
|
|
27
|
+
|
|
28
|
+
from ndtypes import ndt
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
*************
|
|
32
|
+
General notes
|
|
33
|
+
*************
|
|
34
|
+
|
|
35
|
+
:py:class:`ndt` instances have a :py:meth:`match` method for determining
|
|
36
|
+
whether the argument type is compatible with the instance type. The match
|
|
37
|
+
succeeds if and only if the set of types described by the right hand side
|
|
38
|
+
is a subset of the set of types described by the left hand side.
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
Simple example
|
|
42
|
+
--------------
|
|
43
|
+
|
|
44
|
+
.. doctest::
|
|
45
|
+
|
|
46
|
+
>>> p = ndt("Any")
|
|
47
|
+
>>> c = ndt("int32")
|
|
48
|
+
>>> p.match(c)
|
|
49
|
+
True
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
Non-commutativity
|
|
53
|
+
-----------------
|
|
54
|
+
|
|
55
|
+
From the above definition it follows that pattern matching is not commutative:
|
|
56
|
+
|
|
57
|
+
.. doctest::
|
|
58
|
+
|
|
59
|
+
>>> p = ndt("int32")
|
|
60
|
+
>>> c = ndt("Any")
|
|
61
|
+
>>> p.match(c)
|
|
62
|
+
False
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
Concrete matching
|
|
66
|
+
-----------------
|
|
67
|
+
|
|
68
|
+
Much like members of the alphabet in regular expressions, concrete types
|
|
69
|
+
match themselves:
|
|
70
|
+
|
|
71
|
+
.. doctest::
|
|
72
|
+
|
|
73
|
+
>>> p = ndt("int32")
|
|
74
|
+
>>> c = ndt("int32")
|
|
75
|
+
>>> p.match(c)
|
|
76
|
+
True
|
|
77
|
+
|
|
78
|
+
>>> p = ndt("10 * float64")
|
|
79
|
+
>>> c = ndt("10 * float32")
|
|
80
|
+
>>> p.match(c)
|
|
81
|
+
False
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
.. _type-kinds-matching:
|
|
85
|
+
|
|
86
|
+
**********
|
|
87
|
+
Type kinds
|
|
88
|
+
**********
|
|
89
|
+
|
|
90
|
+
:ref:`Type kinds <type-kinds>` are named subsets of :ref:`types <types>`.
|
|
91
|
+
|
|
92
|
+
Unlike :ref:`dtype variables <dtype-vars-matching>`, matching type kinds
|
|
93
|
+
does not require that a well defined substitution exists. Two instances
|
|
94
|
+
of a type kind can match different types:
|
|
95
|
+
|
|
96
|
+
.. doctest::
|
|
97
|
+
|
|
98
|
+
>>> p = ndt("(Any, Any)")
|
|
99
|
+
>>> c = ndt("(float64, int32)")
|
|
100
|
+
>>> p.match(c)
|
|
101
|
+
True
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
Any
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
The *Any* type kind is the most general and describes the set of all
|
|
108
|
+
:ref:`types <types>`.
|
|
109
|
+
|
|
110
|
+
Here's how to match a dtype against the set of all types:
|
|
111
|
+
|
|
112
|
+
.. doctest::
|
|
113
|
+
|
|
114
|
+
>>> p = ndt("Any")
|
|
115
|
+
>>> c = ndt("int32")
|
|
116
|
+
>>> p.match(c)
|
|
117
|
+
True
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
This matches an array type against the set of all types:
|
|
121
|
+
|
|
122
|
+
.. doctest::
|
|
123
|
+
|
|
124
|
+
>>> p = ndt("Any")
|
|
125
|
+
>>> c = ndt("10 * 5 * { v: float64, t: float64 }")
|
|
126
|
+
>>> p.match(c)
|
|
127
|
+
True
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
Scalar
|
|
131
|
+
------
|
|
132
|
+
|
|
133
|
+
The *Scalar* type kind stands for the set of all :ref:`scalars <scalars>`.
|
|
134
|
+
|
|
135
|
+
:c:type:`int32` is a member of the set of all scalars:
|
|
136
|
+
|
|
137
|
+
.. doctest::
|
|
138
|
+
|
|
139
|
+
>>> p = ndt("Scalar")
|
|
140
|
+
>>> c = ndt("int32")
|
|
141
|
+
>>> p.match(c)
|
|
142
|
+
True
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
Unlike with type variables, different types match a type kind:
|
|
146
|
+
|
|
147
|
+
.. doctest::
|
|
148
|
+
|
|
149
|
+
>>> p = ndt("(Scalar, Scalar)")
|
|
150
|
+
>>> c = ndt("(uint8, float64)")
|
|
151
|
+
>>> p.match(c)
|
|
152
|
+
True
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
FixedString
|
|
156
|
+
-----------
|
|
157
|
+
|
|
158
|
+
The set of all :ref:`fixed string <fixed-string>` types.
|
|
159
|
+
|
|
160
|
+
.. doctest::
|
|
161
|
+
|
|
162
|
+
>>> p = ndt("FixedString")
|
|
163
|
+
>>> c = ndt("fixed_string(100)")
|
|
164
|
+
>>> p.match(c)
|
|
165
|
+
True
|
|
166
|
+
|
|
167
|
+
>>> p = ndt("FixedString")
|
|
168
|
+
>>> c = ndt("fixed_string(100, 'utf16')")
|
|
169
|
+
>>> p.match(c)
|
|
170
|
+
True
|
|
171
|
+
|
|
172
|
+
>>> p = ndt("FixedString")
|
|
173
|
+
>>> c = ndt("string")
|
|
174
|
+
>>> p.match(c)
|
|
175
|
+
False
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
FixedBytes
|
|
179
|
+
----------
|
|
180
|
+
|
|
181
|
+
The set of all :ref:`fixed bytes <fixed-bytes>` types.
|
|
182
|
+
|
|
183
|
+
.. doctest::
|
|
184
|
+
|
|
185
|
+
>>> p = ndt("FixedBytes")
|
|
186
|
+
>>> c = ndt("fixed_bytes(size=100)")
|
|
187
|
+
>>> p.match(c)
|
|
188
|
+
True
|
|
189
|
+
|
|
190
|
+
>>> p = ndt("FixedBytes")
|
|
191
|
+
>>> c = ndt("fixed_bytes(size=100, align=2)")
|
|
192
|
+
>>> p.match(c)
|
|
193
|
+
True
|
|
194
|
+
|
|
195
|
+
>>> p = ndt("FixedBytes")
|
|
196
|
+
>>> c = ndt("bytes(align=2)")
|
|
197
|
+
>>> p.match(c)
|
|
198
|
+
False
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
***************
|
|
202
|
+
Dimension kinds
|
|
203
|
+
***************
|
|
204
|
+
|
|
205
|
+
:ref:`Dimension kinds <arrays>` stand for the set of all instances of
|
|
206
|
+
the respective kind.
|
|
207
|
+
|
|
208
|
+
Fixed
|
|
209
|
+
-----
|
|
210
|
+
|
|
211
|
+
The set of all instances of the :ref:`fixed dimension <arrays>` kind.
|
|
212
|
+
|
|
213
|
+
.. doctest::
|
|
214
|
+
|
|
215
|
+
>>> p = ndt("Fixed * 20 * bool")
|
|
216
|
+
>>> c = ndt("10 * 20 * bool")
|
|
217
|
+
>>> p.match(c)
|
|
218
|
+
True
|
|
219
|
+
|
|
220
|
+
>>> p = ndt("Fixed * Fixed * bool")
|
|
221
|
+
>>> c = ndt("var * var * bool")
|
|
222
|
+
>>> p.match(c)
|
|
223
|
+
False
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
.. _dtype-vars-matching:
|
|
227
|
+
|
|
228
|
+
***************
|
|
229
|
+
Dtype variables
|
|
230
|
+
***************
|
|
231
|
+
|
|
232
|
+
:ref:`dtype variables <dtype-variables>` are placeholders for dtypes. It is important
|
|
233
|
+
to note that they are *not* general type variables. For example, they do not match
|
|
234
|
+
:ref:`array types <arrays>`, a concept which is used in general array functions [#f2]_,
|
|
235
|
+
whose base cases may operate on a dtype.
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
This matches a record against a single :ref:`dtype <dtypes>` variable:
|
|
239
|
+
|
|
240
|
+
.. doctest::
|
|
241
|
+
|
|
242
|
+
>>> p = ndt("T")
|
|
243
|
+
>>> c = ndt("{v: float64, t: float64}")
|
|
244
|
+
>>> p.match(c)
|
|
245
|
+
True
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
Match against several dtype variables in a tuple type:
|
|
249
|
+
|
|
250
|
+
.. doctest::
|
|
251
|
+
|
|
252
|
+
>>> p = ndt("T")
|
|
253
|
+
>>> c = ndt("(int32, int32, bool)")
|
|
254
|
+
>>> p.match(c)
|
|
255
|
+
True
|
|
256
|
+
|
|
257
|
+
>>> p = ndt("(T, T, S)")
|
|
258
|
+
>>> c = ndt("(int32, int64, bool)")
|
|
259
|
+
>>> p.match(c)
|
|
260
|
+
False
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
.. _symbolic-dim-matching:
|
|
264
|
+
|
|
265
|
+
*******************
|
|
266
|
+
Symbolic dimensions
|
|
267
|
+
*******************
|
|
268
|
+
|
|
269
|
+
Recall that :ref:`array <arrays>` types include the dimension kind, which
|
|
270
|
+
can be symbolic.
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
Simple symbolic match
|
|
274
|
+
---------------------
|
|
275
|
+
|
|
276
|
+
This matches a concrete fixed size array against the set of all one-dimensional
|
|
277
|
+
fixed size arrays:
|
|
278
|
+
|
|
279
|
+
.. doctest::
|
|
280
|
+
|
|
281
|
+
>>> p = ndt("N * float64")
|
|
282
|
+
>>> c = ndt("100 * float64")
|
|
283
|
+
>>> p.match(c)
|
|
284
|
+
True
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
Symbolic+Dtypevar
|
|
288
|
+
-----------------
|
|
289
|
+
|
|
290
|
+
Symbolic dimensions can be used in conjunction with dtype variables:
|
|
291
|
+
|
|
292
|
+
.. doctest::
|
|
293
|
+
|
|
294
|
+
>>> p = ndt("N * T")
|
|
295
|
+
>>> c = ndt("10 * float32")
|
|
296
|
+
>>> p.match(c)
|
|
297
|
+
True
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
Ellipsis match
|
|
301
|
+
--------------
|
|
302
|
+
|
|
303
|
+
Finally, all dimension kinds (including multiple dimensions) match against
|
|
304
|
+
ellipsis dimensions (named or unnamed):
|
|
305
|
+
|
|
306
|
+
.. doctest::
|
|
307
|
+
|
|
308
|
+
>>> p = ndt("... * float64")
|
|
309
|
+
>>> c = ndt("10 * 2 * float64")
|
|
310
|
+
>>> p.match(c)
|
|
311
|
+
True
|
|
312
|
+
|
|
313
|
+
>>> p = ndt("Dim... * float64")
|
|
314
|
+
>>> c = ndt("10 * 20 * float64")
|
|
315
|
+
>>> p.match(c)
|
|
316
|
+
True
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
This is used in broadcasting [#f2]_.
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
|
324
|
+
|
|
|
325
|
+
|
|
326
|
+
.. [#f1] An argument is often made that the term *dependent types* should
|
|
327
|
+
be reserved for static type systems. We use it here while explicitly
|
|
328
|
+
acknowledging that the datashape implementation is dynamically typed.
|
|
329
|
+
|
|
330
|
+
.. [#f2] Additional section needed.
|