grantjenks / python-pattern-matching Goto Github PK
View Code? Open in Web Editor NEWPython pattern matching like functional languages.
License: Other
Python pattern matching like functional languages.
License: Other
no doc available currently
Add ideas for regex patterns to pattern matching project. Something like: https://github.com/VerbalExpressions
See pampy: https://github.com/santinic/pampy/
TODO: Add comparison.
Idea: add dunder match example for hi/lo checking. Could call RangeMatch or Ranger or something.
Good article by Eli at https://eli.thegreenplace.net/2018/unification/
Should compare functional solution.
I'm not sure what use these have but their neat. Add Case Study: Generic Object Regular Expressions.
Add Case Study: JSON Schema Validation
Remember to link to fast JSON schema validation library.
"""Satisfiability and Propositional Logic
Consider the following constraints:
– Alice can only meet either on Monday, Wednesday or Thursday
– Bob cannot meet on Wednesday
– Carol cannot meet on Friday
– Dave cannot meet neither on Tuesday nor on Thursday
– Question: When can the meeting take place?
- Encode then into the following Boolean formula:
(Mon ∨ Wed ∨ Thu) ∧ (¬Wed) ∧ (¬Fri) ∧ (¬Tue ∧ ¬Thu)
The meeting must take place on Monday
"""
import logging
from patternmatching import *
logging.basicConfig(format='%(message)s', level=logging.DEBUG)
options = [
'monday', 'wednesday', 'thursday', 'X',
'monday', 'tuesday', 'thursday', 'friday', 'X',
'monday', 'tuesday', 'wednesday', 'thursday', 'X',
'monday', 'wednesday', 'friday', 'X',
True,
]
constraints = (
padding + anyone * group('alice') + padding + 'X'
+ padding + anyone * group('bob') + padding + 'X'
+ padding + anyone * group('carol') + padding + 'X'
+ padding + anyone * group('dave') + padding + 'X'
+ like(lambda _: bound.alice == bound.bob == bound.carol == bound.dave)
)
assert match(options, constraints) # <-- FAILS! and I don't know why :(
Hello, I am not sure if this project is still actively maintained. If so, I would really appreciate if someone can help me on the following issue.
I got an error for the following sample code:
import pypatt
@pypatt.transform
def factorial(num):
with match(num):
with 1:
return 1
with _:
return num * factorial(num - 1)
print factorial(3)
Error:
Traceback (most recent call last):
File "tt.py", line 29, in <module>
print factorial(3)
File "tt.py", line 769, in factorial
File "/usr/lib/python2.7/site-packages/pypatt/macro.py", line 174, in trybind
result = visitor(expr, value)
File "/usr/lib/python2.7/site-packages/pypatt/macro.py", line 158, in visitor
raise RuntimeError('unknown ast.Name')
RuntimeError: unknown ast.Name
Can anyone help me on this? Thank you
An integration with Z3 seems possible and useful. Z3 knows nothing about Python so I think the trick is to encode Python’s object model in Z3 code.
For example, when matching sequences, Z3 would be told that each matching element must be equal. This might also apply to NamedTuples and data classes pretty easily.
The general class of equality would be difficult to capture without translating Python byte code to Z3 statements but the common cases of built-in types (ints, strs, lists, etc) and class factories (data class, named tuple, etc) may be possible.
I know z3 supports integers but does it also handle strings and floats? Hi
Create Symbol Expressions using namedtuples and write optimizer rules using patternmatching.
Might be a good demo for the landing page -- I recall lots of complex clauses with patternmatching.
Functional language alternatives:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.