adt.py 4.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
17
# pylint: disable=no-else-return, unidiomatic-typecheck, invalid-name, unused-import
18
"""Algebraic data types in Relay."""
19
from tvm.ir import Constructor, TypeData
20 21
from tvm.runtime import Object
import tvm._ffi
22

23 24
from .base import RelayNode
from . import _ffi_api
25
from .ty import Type
26
from .expr import ExprWithOp, RelayExpr, Call
27 28 29 30


class Pattern(RelayNode):
    """Base type for pattern matching constructs."""
31

32

33
@tvm._ffi.register_object("relay.PatternWildcard")
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
class PatternWildcard(Pattern):
    """Wildcard pattern in Relay: Matches any ADT and binds nothing."""

    def __init__(self):
        """Constructs a wildcard pattern.

        Parameters
        ----------
        None

        Returns
        -------
        wildcard: PatternWildcard
            a wildcard pattern.
        """
49
        self.__init_handle_by_constructor__(_ffi_api.PatternWildcard)
50 51


52
@tvm._ffi.register_object("relay.PatternVar")
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
class PatternVar(Pattern):
    """Variable pattern in Relay: Matches anything and binds it to the variable."""

    def __init__(self, var):
        """Construct a variable pattern.

        Parameters
        ----------
        var: tvm.relay.Var

        Returns
        -------
        pv: PatternVar
            A variable pattern.
        """
68
        self.__init_handle_by_constructor__(_ffi_api.PatternVar, var)
69 70


71
@tvm._ffi.register_object("relay.PatternConstructor")
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
class PatternConstructor(Pattern):
    """Constructor pattern in Relay: Matches an ADT of the given constructor, binds recursively."""

    def __init__(self, constructor, patterns=None):
        """Construct a constructor pattern.

        Parameters
        ----------
        constructor: Constructor
            The constructor.
        patterns: Optional[List[Pattern]]
            Optional subpatterns: for each field of the constructor,
            match to the given subpattern (treated as a variable pattern by default).

        Returns
        -------
        wildcard: PatternWildcard
            a wildcard pattern.
        """
        if patterns is None:
            patterns = []
93
        self.__init_handle_by_constructor__(_ffi_api.PatternConstructor, constructor, patterns)
94 95


96
@tvm._ffi.register_object("relay.PatternTuple")
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
class PatternTuple(Pattern):
    """Constructor pattern in Relay: Matches a tuple, binds recursively."""

    def __init__(self, patterns=None):
        """Construct a tuple pattern.

        Parameters
        ----------
        patterns: Optional[List[Pattern]]
            Optional subpatterns: for each field of the constructor,
            match to the given subpattern (treated as a variable pattern by default).

        Returns
        -------
        wildcard: PatternWildcard
            a wildcard pattern.
        """
        if patterns is None:
            patterns = []
116
        self.__init_handle_by_constructor__(_ffi_api.PatternTuple, patterns)
117 118


119
@tvm._ffi.register_object("relay.Clause")
120
class Clause(Object):
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    """Clause for pattern matching in Relay."""

    def __init__(self, lhs, rhs):
        """Construct a clause.

        Parameters
        ----------
        lhs: tvm.relay.Pattern
            Left-hand side of match clause.
        rhs: tvm.relay.Expr
            Right-hand side of match clause.

        Returns
        -------
        clause: Clause
            The Clause.
        """
138
        self.__init_handle_by_constructor__(_ffi_api.Clause, lhs, rhs)
139 140


141
@tvm._ffi.register_object("relay.Match")
142
class Match(ExprWithOp):
143 144
    """Pattern matching expression in Relay."""

145
    def __init__(self, data, clauses, complete=True):
146 147 148 149 150 151
        """Construct a Match.

        Parameters
        ----------
        data: tvm.relay.Expr
            The value being deconstructed and matched.
152

153 154
        clauses: List[tvm.relay.Clause]
            The pattern match clauses.
155 156 157 158 159

        complete: Optional[Bool]
            Should the match be complete (cover all cases)?
            If yes, the type checker will generate an error if there are any missing cases.

160 161 162 163 164
        Returns
        -------
        match: tvm.relay.Expr
            The match expression.
        """
165
        self.__init_handle_by_constructor__(_ffi_api.Match, data, clauses, complete)