Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
tic
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wenyuanbo
tic
Commits
2d5a0720
Commit
2d5a0720
authored
Mar 22, 2019
by
Wei Chen
Committed by
Tianqi Chen
Mar 22, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Relay] Add list update to prelude (#2866)
parent
46924406
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
0 deletions
+38
-0
python/tvm/relay/prelude.py
+20
-0
tests/python/relay/test_adt.py
+18
-0
No files found.
python/tvm/relay/prelude.py
View file @
2d5a0720
...
...
@@ -62,6 +62,25 @@ class Prelude:
s_case
=
Clause
(
PatternConstructor
(
self
.
s
,
[
PatternVar
(
y
)]),
self
.
nth
(
self
.
tl
(
x
),
y
))
self
.
mod
[
self
.
nth
]
=
Function
([
x
,
n
],
Match
(
n
,
[
z_case
,
s_case
]),
a
,
[
a
])
def
define_list_update
(
self
):
"""Defines a function to update the nth element of a list and return the updated list.
update(l, i, v) : list[a] -> nat -> a -> list[a]
"""
self
.
update
=
GlobalVar
(
"update"
)
a
=
TypeVar
(
"a"
)
l
=
Var
(
"l"
,
self
.
l
(
a
))
n
=
Var
(
"n"
,
self
.
nat
())
v
=
Var
(
"v"
,
a
)
y
=
Var
(
"y"
)
z_case
=
Clause
(
PatternConstructor
(
self
.
z
),
self
.
cons
(
v
,
self
.
tl
(
l
)))
s_case
=
Clause
(
PatternConstructor
(
self
.
s
,
[
PatternVar
(
y
)]),
self
.
cons
(
self
.
hd
(
l
),
self
.
update
(
self
.
tl
(
l
),
y
,
v
)))
self
.
mod
[
self
.
update
]
=
Function
([
l
,
n
,
v
],
Match
(
n
,
[
z_case
,
s_case
]),
self
.
l
(
a
),
[
a
])
def
define_list_map
(
self
):
"""Defines a function for mapping a function over a list's
elements. That is, map(f, l) returns a new list where
...
...
@@ -470,6 +489,7 @@ class Prelude:
self
.
define_nat_add
()
self
.
define_list_length
()
self
.
define_list_nth
()
self
.
define_list_update
()
self
.
define_list_sum
()
self
.
define_tree_adt
()
...
...
tests/python/relay/test_adt.py
View file @
2d5a0720
...
...
@@ -26,6 +26,7 @@ l = p.l
hd
=
p
.
hd
tl
=
p
.
tl
nth
=
p
.
nth
update
=
p
.
update
length
=
p
.
length
map
=
p
.
map
foldl
=
p
.
foldl
...
...
@@ -148,6 +149,23 @@ def test_nth():
assert
got
==
expected
def
test_update
():
expected
=
list
(
range
(
10
))
l
=
nil
()
# create zero initialized list
for
i
in
range
(
len
(
expected
)):
l
=
cons
(
build_nat
(
0
),
l
)
# set value
for
i
,
v
in
enumerate
(
expected
):
l
=
update
(
l
,
build_nat
(
i
),
build_nat
(
v
))
got
=
[]
for
i
in
range
(
len
(
expected
)):
got
.
append
(
count
(
intrp
.
evaluate
(
nth
(
l
,
build_nat
(
i
)))))
assert
got
==
expected
def
test_length
():
a
=
relay
.
TypeVar
(
"a"
)
assert
mod
[
length
]
.
checked_type
==
relay
.
FuncType
([
l
(
a
)],
nat
(),
[
a
])
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment