mini-egison: Template Haskell Implementation of Egison Pattern Matching
This package provides the pattern-matching facility that fulfills the following three criteria for practical pattern matching for non-free data types: (i) non-linear pattern matching with backtracking; (ii) extensibility of pattern-matching algorithms; (iii) ad-hoc polymorphism of patterns. Non-free data types are data types whose data have no standard forms. For example, multisets are non-free data types because the multiset '[a,b,b]' has two other equivalent but literally different forms '[b,a,b]' and '[b,b,a]'.
The design of the pattern-matching facility is originally proposed in this paper and implemented in the Egison programming language.
Samples
We can extract all twin primes from the list of prime numbers by pattern matching:
take 10 (matchAll primes (List Integer) [[mc| join _ (cons $p (cons #(p+2) _)) => (p, p+2) |]]) -- [(3,5),(5,7),(11,13),(17,19),(29,31),(41,43),(59,61),(71,73),(101,103),(107,109)]
We can describe patterns for each poker hand utilizing pattern matching for a multiset:
poker cs = match cs (Multiset CardM) [[mc| cons (card $s $n) (cons (card #s #(n-1)) (cons (card #s #(n-2)) (cons (card #s #(n-3)) (cons (card #s #(n-4)) _)))) => "Straight flush" |], [mc| cons (card _ $n) (cons (card _ #n) (cons (card _ #n) (cons (card _ #n) (cons _ _)))) => "Four of a kind" |], [mc| cons (card _ $m) (cons (card _ #m) (cons (card _ #m) (cons (card _ $n) (cons (card _ #n) _)))) => "Full house" |], [mc| cons (card $s _) (cons (card #s _) (cons (card #s _) (cons (card #s _) (cons (card #s _) _)))) => "Flush" |], [mc| cons (card _ $n) (cons (card _ #(n-1)) (cons (card _ #(n-2)) (cons (card _ #(n-3)) (cons (card _ #(n-4)) _)))) => "Straight" |], [mc| cons (card _ $n) (cons (card _ #n) (cons (card _ #n) (cons _ (cons _ _)))) => "Three of a kind" |], [mc| cons (card _ $m) (cons (card _ #m) (cons (card _ $n) (cons (card _ #n) (cons _ _)))) => "Two pair" |], [mc| cons (card _ $n) (cons (card _ #n) (cons _ (cons _ (cons _ _)))) => "One pair" |], [mc| _ => "Nothing" |]]
The pattern-matching algorithms for List
and Multiset
can be defined by users.
[Skip to Readme]
Modules
[Index] [Quick Jump]
Downloads
- mini-egison-0.1.0.tar.gz [browse] (Cabal source package)
- Package description (as included in the package)
Maintainer's Corner
For package maintainers and hackage trustees
Candidates
- No Candidates
Versions [RSS] | 0.1.0, 0.1.1, 0.1.2, 0.1.3, 0.1.4, 0.1.5, 0.1.6, 1.0.0 |
---|---|
Change log | ChangeLog.md |
Dependencies | base (>=4.7 && <5), containers, haskell-src-meta, regex-compat, split, template-haskell [details] |
License | MIT |
Author | Mayuko Kori, Satoshi Egi |
Maintainer | Satoshi Egi <egi@egison.org> |
Category | Data, Pattern |
Home page | https://github.com/egison/egison-haskell#readme |
Bug tracker | https://github.com/egison/egison-haskell/issues |
Source repo | head: git clone https://github.com/egison/egison-haskell |
Uploaded | by SatoshiEgi at 2019-09-26T12:12:25Z |
Distributions | |
Reverse Dependencies | 1 direct, 2 indirect [details] |
Downloads | 3173 total (1 in the last 30 days) |
Rating | (no votes yet) [estimated by Bayesian average] |
Your Rating | |
Status | Docs available [build log] Last success reported on 2019-09-26 [all 1 reports] |