Skip to main content

miniadt

how to use

## create Type

>>> from miniadt import ADTTypeProvider
>>> TreeType = ADTTypeProvider("Tree")

>>> Node = TreeType("Node", "e children")
>>> Leaf = TreeType("Leaf", "e")


## printing value

>>> Leaf(e=10)
Leaf(e=10)
>>> Node(e=10, children=[Leaf(e=20)])
Node(e=10, children=[Leaf(e=20)])


## use pattern match

>>> @TreeType.match
... class depth(object):
...     def Node(e, children):
...         return max(depth(e)for e in children) + 1
...
...     def Leaf(e):
...         return 1

>>> depth(Leaf(e=10))
1

>>> depth(Node(e=10, children=[Leaf(e=20), Node(e=30, children=[Leaf(e=40)])]))
3

miniadt has comprehensive check function.

## not comprehensive definition on pattern matching function error is occur

### 1. lack of dispatch andidates
>>> class invalid_dispatch(object):
...     def Node(e, children):
...         return "foo"

>>> TreeType.match(invalid_dispatch)
Traceback (most recent call last):
 ...
miniadt.NotComprehensive: Leaf is not found. expected=['Node', 'Leaf']


### 2. dispatch function's arguments are invalid.
>>> class invalid_dispatch2(object):
...     def Node(e):  ## correct argsspec is "e, children"
...         return "foo"
...     def Leaf(e):
...         return "foo"

>>> TreeType.match(invalid_dispatch2)
Traceback (most recent call last):
 ...
miniadt.NotComprehensive: on Tree.Node:  expected=['e', 'children'] != actual=['e']

similar functions

  • match

  • match_instance

  • classify

from miniadt import ADTTypeProvider
Tree = ADTTypeProvider("Tree")
Node = Tree("Node", "e children")
Leaf = Tree("Leaf", "e")

print(Leaf(e=10))  # => Leaf(e=10)
print(Node(e=10, children=[Leaf(e=20)]))  # => Node(e=10, children=[Leaf(e=20)])


@Tree.match
class depth(object):
    def Leaf(e):
        return 1

    def Node(e, children):
        return max(depth(e) for e in children) + 1


print(depth(Leaf(e=10)))  # => 10
print(depth(Node(e=10, children=[Leaf(e=20)])))  # 2


@Tree.match_instance
class Applicator(object):
    def __init__(self, name):
        self.name = name

    def Leaf(self, e):
        return self.name

    def Node(self, e, children):
        return [self.name, [self(x) for x in children]]

print(Applicator("foo")(Leaf(e=10)))  # => foo
print(Applicator("foo")(Node(e=10, children=[Leaf(e=20)])))  # => ['foo', ['foo']]


@Tree.classify
class ToDict(object):
    def Leaf(self, leaf):
        return leaf.e

    def Node(self, node):
        return {"e": node.e, "children": [self(e) for e in node.children]}

todict = ToDict()
print(todict(Leaf(e=10)))  # => 10
print(todict(Node(e=10, children=[Leaf(e=20)])))  # => {'e': 10, 'children': [20]}

Metadata

Release files for miniadt 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for miniadt 0.4.0
File Size Uploaded Provenance Actions
miniadt-0.4.0.tar.gz 5.9 kB Without provenance Details for miniadt-0.4.0.tar.gz

Release files / miniadt-0.4.0.tar.gz

Download URL miniadt-0.4.0.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
ed46c1a676be64f33dd47797929938286236a40eff8f9dca095d6b7fb4ca4c48
BLAKE2b-256 checksum
How to use checksums
eca7fe66caed24e82e4e01d7164aa99eec8db2f02dc16451df0ec736ecdc992b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for miniadt 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page