-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from brandonwillard/tail-call-optimization
Introduce stream/trampoline-based reification and unification
- Loading branch information
Showing
15 changed files
with
532 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -198,3 +198,5 @@ tags | |
[._]*.un~ | ||
|
||
# End of https://www.gitignore.io/api/vim,emacs,python | ||
|
||
.benchmarks/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,34 @@ | ||
from functools import partial | ||
from collections import defaultdict | ||
from unification import * | ||
from unification.match import * | ||
|
||
from unification import var | ||
from unification.match import match, VarDispatcher | ||
|
||
|
||
match = partial(match, Dispatcher=VarDispatcher) | ||
|
||
balance = defaultdict(lambda: 0) | ||
|
||
name, amount = var('name'), var('amount') | ||
name, amount = var("name"), var("amount") | ||
|
||
|
||
@match({'status': 200, 'data': {'name': name, 'credit': amount}}) | ||
@match({"status": 200, "data": {"name": name, "credit": amount}}) | ||
def respond(name, amount): | ||
balance[name] +=amount | ||
balance[name] += amount | ||
|
||
|
||
@match({'status': 200, 'data': {'name': name, 'debit': amount}}) | ||
@match({"status": 200, "data": {"name": name, "debit": amount}}) | ||
def respond(name, amount): | ||
balance[name] -= amount | ||
|
||
|
||
@match({'status': 404}) | ||
@match({"status": 404}) | ||
def respond(): | ||
print("Bad Request") | ||
|
||
|
||
if __name__ == '__main__': | ||
respond({'status': 200, 'data': {'name': 'Alice', 'credit': 100}}) | ||
respond({'status': 200, 'data': {'name': 'Bob', 'debit': 100}}) | ||
respond({'status': 404}) | ||
if __name__ == "__main__": | ||
respond({"status": 200, "data": {"name": "Alice", "credit": 100}}) | ||
respond({"status": 200, "data": {"name": "Bob", "debit": 100}}) | ||
respond({"status": 404}) | ||
print(dict(balance)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import pytest | ||
|
||
from toolz import assoc | ||
|
||
from unification import unify, reify, var, isvar | ||
from unification.utils import transitive_get as walk | ||
|
||
from tests.utils import gen_long_chain | ||
|
||
|
||
nesting_sizes = [10, 35, 300] | ||
|
||
|
||
def unify_stack(u, v, s): | ||
|
||
u = walk(u, s) | ||
v = walk(v, s) | ||
|
||
if u == v: | ||
return s | ||
if isvar(u): | ||
return assoc(s, u, v) | ||
if isvar(v): | ||
return assoc(s, v, u) | ||
|
||
if isinstance(u, (tuple, list)) and type(u) == type(v): | ||
for i_u, i_v in zip(u, v): | ||
s = unify_stack(i_u, i_v, s) | ||
if s is False: | ||
return s | ||
|
||
return s | ||
|
||
return False | ||
|
||
|
||
def reify_stack(u, s): | ||
|
||
u_ = walk(u, s) | ||
|
||
if u_ is not u: | ||
return reify_stack(u_, s) | ||
|
||
if isinstance(u_, (tuple, list)): | ||
return type(u_)(reify_stack(i_u, s) for i_u in u_) | ||
|
||
return u_ | ||
|
||
|
||
@pytest.mark.benchmark(group="unify_chain") | ||
@pytest.mark.parametrize("size", nesting_sizes) | ||
def test_unify_chain_stream(size, benchmark): | ||
a_lv = var() | ||
form = gen_long_chain(a_lv, size) | ||
term = gen_long_chain("a", size) | ||
|
||
res = benchmark(unify, form, term, {}) | ||
assert res[a_lv] == "a" | ||
|
||
|
||
@pytest.mark.benchmark(group="unify_chain") | ||
@pytest.mark.parametrize("size", nesting_sizes) | ||
def test_unify_chain_stack(size, benchmark): | ||
a_lv = var() | ||
form = gen_long_chain(a_lv, size) | ||
term = gen_long_chain("a", size) | ||
|
||
res = benchmark(unify_stack, form, term, {}) | ||
assert res[a_lv] == "a" | ||
|
||
|
||
@pytest.mark.benchmark(group="reify_chain") | ||
@pytest.mark.parametrize("size", nesting_sizes) | ||
def test_reify_chain_stream(size, benchmark): | ||
a_lv = var() | ||
form = gen_long_chain(a_lv, size) | ||
term = gen_long_chain("a", size) | ||
|
||
res = benchmark(reify, form, {a_lv: "a"}) | ||
assert res == term | ||
|
||
|
||
@pytest.mark.benchmark(group="reify_chain") | ||
@pytest.mark.parametrize("size", nesting_sizes) | ||
def test_reify_chain_stack(size, benchmark): | ||
a_lv = var() | ||
form = gen_long_chain(a_lv, size) | ||
term = gen_long_chain("a", size) | ||
|
||
res = benchmark(reify_stack, form, {a_lv: "a"}) | ||
assert res == term |
Oops, something went wrong.