Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simpler way to type type.py #909

Merged
merged 1 commit into from
Jul 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 13 additions & 30 deletions src/python/ksc/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@ class KSTypeError(RuntimeError):
pass


# TODO: decide if we want to keep this approach, or move to module level functions
# https://github.com/microsoft/knossos-ksc/issues/888
class classproperty(object):
def __init__(self, getter):
self.getter = getter

def __get__(self, _, owner):
return self.getter(owner)


class Type:
"""
Knossos AST node type. Grouped into
Expand All @@ -26,6 +16,12 @@ class Type:
LM S T
"""

String: "Type"
Float: "Type"
Integer: "Type"
Bool: "Type"
Any: "Type"

node_kinds = {
"Tensor": 2, # two children (Rank, Type)
"Tuple": -1, # special case two or more
Expand Down Expand Up @@ -58,26 +54,6 @@ def __init__(self, kind, children=[]):
################
## Constructors

@classproperty
def String(cls) -> "Type":
return Type("String")

@classproperty
def Float(cls) -> "Type":
return Type("Float")

@classproperty
def Integer(cls) -> "Type":
return Type("Integer")

@classproperty
def Bool(cls) -> "Type":
return Type("Bool")

@classproperty
def Any(cls) -> "Type":
return Type("Any")

@staticmethod
def Tensor(rank, elem_type):
"""
Expand Down Expand Up @@ -371,3 +347,10 @@ def shape_type(t: Type) -> Type:
return Type.Tensor(t.tensor_rank, shape_type(t.tensor_elem_type))

raise NotImplementedError


Type.String = Type("String")
Type.Float = Type("Float")
Type.Integer = Type("Integer")
Type.Bool = Type("Bool")
Type.Any = Type("Any")