Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/laserpants/dyckword
:surfer: A Haskell library for working with binary Dyck words.
https://github.com/laserpants/dyckword
formal-languages haskell math parentheses
Last synced: about 1 month ago
JSON representation
:surfer: A Haskell library for working with binary Dyck words.
- Host: GitHub
- URL: https://github.com/laserpants/dyckword
- Owner: laserpants
- License: bsd-3-clause
- Created: 2017-04-30T22:19:30.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-01-15T16:11:24.000Z (almost 7 years ago)
- Last Synced: 2024-04-23T22:55:02.454Z (8 months ago)
- Topics: formal-languages, haskell, math, parentheses
- Language: Haskell
- Homepage: https://hackage.haskell.org/package/dyckword
- Size: 45.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# dyckword [![Build Status](https://img.shields.io/travis/laserpants/dyckword/master.svg?style=flat)](https://travis-ci.org/laserpants/dyckword) [![License](https://img.shields.io/badge/license-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause) [![Language](https://img.shields.io/badge/language-Haskell-yellow.svg)](https://www.haskell.org/) [![Hackage](https://img.shields.io/hackage/v/dyckword.svg)](http://hackage.haskell.org/package/dyckword)
## Table of Contents
* [Background](#background)
* [Documentation](#documentation)
* [Install](#install)
* [Examples](#examples)## Background
In formal language theory, the *Dyck language* consists of all strings of evenly balanced left and right parentheses, brackets, or some other symbols, together with the *empty* word. Words in this language (named after German mathematician Walther von Dyck) are known as *Dyck words*, some examples of which are `()()()`, `(())((()))`, and `((()()))()`.
The type of Dyck language considered here is defined over a binary alphabet. If we take this alphabet to be the set Σ = {(, )}, then the binary Dyck language is the subset of Σ* (the Kleene closure of Σ) of all words that satisfy two conditions:
1. The number of left brackets must be the same as the number of right brackets.
2. Going from left to right, for each character read, the total number of right brackets visited must be less than or equal to the number of left brackets up to the current position.E.g., `(()(()` and `())(())()` are **not** Dyck words.
When regarded as a combinatorial class – with the size of a word defined as the number of bracket pairs it contains – the counting sequence associated with the Dyck language is the *Catalan numbers*.
```
λ> take 15 $ (length . wordsOfSize) <$> [0..]
[1,1,2,5,14,42,132,429,1430,4862,16796,58786,208012,742900,2674440]
```## Documentation
See [Hackage](http://hackage.haskell.org/package/dyckword/docs/Math-DyckWord-Binary.html).
## Install
```
cabal install dyckword
```## Examples
```
λ> :set -XOverloadedStringsλ> toText $ unrank (10^889)
(()((()((())(()()((()()()((()((())())(()(()()()(())()()()))((()()))())(()()(())((((()())((((()()(()()(())((()(())
((((())))))(((((((()((())()()((())))()(()((())))(((())))))()())()()()()))(()))()(())(((())))))))()(())))()))()(()
(((()())((()())()(()())())))())))))))))(((((()((())()))((()())()(((()()()(()(()))))((())((()))))((()())())())((()
(()()))))))())()((())((((()(()(()()()((())((((((((()(((((((((()(())()(()()()((()))))()((())(((())(()())())((()())
((()(()))())()())(()))))))()((()((())(())())(()))))()())()()(())(()()))()))((((()())(()()()))(())))((())))))())((
()))))))))()(())((()((((((())(())))()(()(((((()()()))())))())(((())(()((()(((((((())())((())(()))((()(()()(()(()(
())()(()))()()(((()(((()))(())))(()))((()))()(()))((())(()()()((()()())))(()))())(()))))()()))((())()())())(()())
)))((()()))(((()))(()))((()))))())())()(()(()))())((())))()()()()(()())()(())())(((((()))())()(()())()()(((((())(
)))(()(()))(((()()()())()()())())())(((((()())))))()))())())))(()()))()))()()))))())()(((((((())()(()(()))))(((((
((((((()()((()())())(())))())))()(()))((((()))()()((()()()(()(((()))((())(((((((()()((())))())(()()(((()(()))))))
))))()()((()(()()))())(())()(())(((()((((())))((()(((())(((()())))))(((((()))()(()((()()((()())())(((())())(())()
)(()))())((((()(()())()(())(()(((()())()(()))()(()((()(((())))(())(()())))())(())()))((((()()(()))()()((((())()((
((((())())())(()()((()(()())))))())())())))()))()))))((())((()))()()))((()()))(((()())()()()))()())))(((())))))))
(((((())())()(())))((()()())()((((()())((((((((()(()()))))(()(())))))(())()(()((()()))(((((()(((())(((((((()))(((
))(()(()(()()()(((())())))()((((((((())((((()((()(((((()))(())((()))))())(()(()())))((((())(()((()()))(((((()((((
(((())()())())(()))()))()(()(()(((((()))(()))))))()())()))))()()())())((()(())))((())(((((((()()))((()()))))(((((
(((()())()()))()())()(())()(()()(()))))))()))))()))())((())))))((()())(()(()))()()))()(())())((()())()))()())(())
())(()()))())(()()))(()()()())()()())((())))))()(()())(()))()()())))(((())()(()()((()()))))))())((((((()()())))()
())())((()())(((((()()(((((())()()()((()()(((((()((()(()())))(()(())(()()(()()(()(())))))()()(()())(()()))((())))
))(())()))((()(()())((()))((()()()()()())))))())))()))()(((((((())))))))((())))))(((()()()())()()()()(()(()))(()(
()())()(()()(()()(((())(())))))()((()))((()))(((((()(()()(()(((()((())))(()()(()()()(()((())()((()))()))(()()))))
)()())((((())())(())()()()()((((()))))))((()()((())((()((((()())()))()((()((())())()()(((())()(((()(((((()((()))(
)))))(())((()())()(()(((())))((((()(((()()())((()()()())())((((()(((()))()((((()(()))(()(((()()(()))())))())()(((
()((()((())))(())))))))(()())()))()())())()))())()))())))))(((())))))))(()))((()())((())()())((()))))))()(()))())
)(())))))))()())())))))))(()()(((())))(())((()(()()))(()()()()))(()()((()()()())))(()(()(()()))(())))(()())((())(
)((()((()))(()()(()(()))(())))))))()()))())))))))))(()()))))(()))))())()(()()))()))((())))))()()()(()))))()(()(()
((()))()(()(()(()))))()))())))()λ> size $ unrank (10^989)
1651λ> rank $ fromText' "(())()(((())))"
480λ> rank $ fromText' "ooxxoxooooxxxx"
480λ> fromText "aaaa"
Left "bad input"λ> fromText "()()" > fromText "(())"
Trueλ> mapM_ print (toText <$> wordsOfSize 5)
"((((()))))"
"(((()())))"
"(((())()))"
"(((()))())"
"(((())))()"
"((()(())))"
"((()()()))"
"((()())())"
"((()()))()"
"((())(()))"
"((())()())"
"((())())()"
"((()))(())"
"((()))()()"
"(()((())))"
"(()(()()))"
"(()(())())"
"(()(()))()"
"(()()(()))"
"(()()()())"
"(()()())()"
"(()())(())"
"(()())()()"
"(())((()))"
"(())(()())"
"(())(())()"
"(())()(())"
"(())()()()"
"()(((())))"
"()((()()))"
"()((())())"
"()((()))()"
"()(()(()))"
"()(()()())"
"()(()())()"
"()(())(())"
"()(())()()"
"()()((()))"
"()()(()())"
"()()(())()"
"()()()(())"
"()()()()()"
```