Repository navigation
Expand file tree
/
Copy pathautocurry.py
More file actions
121 lines (91 loc) · 5.04 KB
/
Copy pathautocurry.py
File metadata and controls
121 lines (91 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# -*- coding: utf-8 -*-
"""Automatic currying. Transforms both function definitions and calls."""
__all__ = ["autocurry"]
from ast import Call, Lambda, FunctionDef, AsyncFunctionDef
from mcpyrate.quotes import macros, q, a, h # noqa: F401
from mcpyrate.quotes import is_captured_value
from mcpyrate.walkers import ASTTransformer
from .util import (suggest_decorator_index, isx, has_curry, sort_lambda_decorators)
# CAUTION: unpythonic.syntax.lambdatools.namedlambda depends on the exact names
# "curryf" and "currycall" to detect an auto-curried expression with a final lambda.
from ..fun import curry as curryf, _currycall as currycall
def autocurry(tree, *, syntax, expander, **kw): # technically a list of trees, the body of the with block
"""[syntax, block] Automatic currying.
Usage::
from unpythonic.syntax import macros, autocurry
with autocurry:
...
All **function calls** and **function definitions** (``def``, ``lambda``)
*lexically* inside the ``with autocurry`` block are automatically curried.
**CAUTION**: Some builtins are uninspectable or may report their arities
incorrectly; in those cases, ``curry`` may fail, occasionally in mysterious
ways.
The function ``unpythonic.arity.arities``, which ``unpythonic.fun.curry``
internally uses, has a workaround for the inspectability problems of all
builtins in the top-level namespace (as of Python 3.7), but e.g. methods
of builtin types are not handled.
Lexically inside a ``with autocurry`` block, the auto-curried function calls
will skip the curry if the function is uninspectable, instead of raising
``TypeError`` as usual.
Example::
from unpythonic.syntax import macros, autocurry
from unpythonic import foldr, composerc as compose, cons, nil, ll
with autocurry:
def add3(a, b, c):
return a + b + c
assert add3(1)(2)(3) == 6
assert add3(1, 2)(3) == 6
assert add3(1)(2, 3) == 6
assert add3(1, 2, 3) == 6
mymap = lambda f: foldr(compose(cons, f), nil)
double = lambda x: 2 * x
assert mymap(double, ll(1, 2, 3)) == ll(2, 4, 6)
# The definition was auto-curried, so this works here too.
assert add3(1)(2)(3) == 6
"""
if syntax != "block":
raise SyntaxError("autocurry is a block macro only") # pragma: no cover
if syntax == "block" and kw['optional_vars'] is not None:
raise SyntaxError("autocurry does not take an as-part") # pragma: no cover
tree = expander.visit(tree)
return _autocurry(block_body=tree)
_iscurry = lambda name: name in ("curry", "currycall")
def _autocurry(block_body):
class AutoCurryTransformer(ASTTransformer):
def transform(self, tree):
# Ignore hygienically captured values, and don't recurse in them.
# In `mcpyrate`, they are represented by Call nodes that match
# `mcpyrate.quotes.is_captured_value`.
if is_captured_value(tree):
return tree
hascurry = self.state.hascurry
# Curry all calls; except as a small optimization, skip `Values(...)`,
# which accepts any args and kwargs, so currying it does not make sense.
# (It represents multiple-return-values in `unpythonic`.)
if type(tree) is Call and not isx(tree.func, "Values"):
if has_curry(tree): # detect decorated lambda with manual curry
# the lambda inside the curry(...) is the next Lambda node we will descend into.
hascurry = True
if not isx(tree.func, _iscurry):
tree.args = [tree.func] + tree.args
tree.func = q[h[currycall]]
if hascurry: # this must be done after the edit because the edit changes the children
self.generic_withstate(tree, hascurry=True)
elif type(tree) in (FunctionDef, AsyncFunctionDef):
if not any(isx(item, _iscurry) for item in tree.decorator_list): # no manual curry already
k = suggest_decorator_index("curry", tree.decorator_list)
if k is not None:
tree.decorator_list.insert(k, q[h[curryf]])
else: # couldn't determine insert position; just plonk it at the end and hope for the best
tree.decorator_list.append(q[h[curryf]])
elif type(tree) is Lambda:
if not hascurry:
thelambda = tree
tree = q[h[curryf](a[thelambda])] # plonk it as innermost, we'll sort them later
# don't recurse on the lambda we just moved, but recurse inside it.
self.withstate(thelambda.body, hascurry=False)
thelambda.body = self.visit(thelambda.body)
return tree
return self.generic_visit(tree)
newbody = AutoCurryTransformer(hascurry=False).visit(block_body)
return sort_lambda_decorators(newbody)