Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
b91642c
Grammar classes: RegGrammar, Grammar & CtxGrammar; *Grammatical const…
echatav Nov 14, 2026
784aafa
Ruled, BackusNaurForm & Tried; remove Control.Monad.Fail.Try
echatav Nov 14, 2026
afcbced
Varifocal stepping over any Cons stream
echatav Nov 14, 2026
b03fe7d
steppingTree & arityWith: stepping through cofree trees
echatav Nov 14, 2026
1de747d
Varifocus: many & some by manyP & someP
echatav Nov 14, 2026
34b3f34
Remove RegString & RegBnf: RegEx & Bnf, without orphans
echatav Nov 14, 2026
68f0bf0
Control.Lens.Grammar.Analysis: FIRST, FOLLOW & LL(1), by folds
echatav Nov 14, 2026
d94485b
Fold Lexical into RegGrammatical
echatav Nov 14, 2026
2099c53
DisjunctiveMonoid for every Alternative, & for Set & Map by <>
echatav Nov 14, 2026
4524d22
DisjunctiveMonoid for every Alternative of the dependencies
echatav Nov 14, 2026
e48aa8d
Bits as BooleanAlgebras; Predicate & Ap Kleene; ReadPrec & Predicate …
echatav Nov 14, 2026
bf19bfc
Grammars of base types
echatav Nov 14, 2026
aa01b2b
RegEx records try & renders class ranges
echatav Nov 14, 2026
ead7a70
Rho calculus grammars, in the paper's & an ASCII notation
echatav Nov 14, 2026
9f5eade
Json example's character class by complement
echatav Nov 14, 2026
710874d
RegEx quantifiers, by timesK & timesP
echatav Nov 14, 2026
b1690c1
Counts are those n >= 0 within bounds, alike across instances
echatav Nov 14, 2026
4d09da1
TokenSets: Char's classes by ranges, of range-set-list
echatav Nov 14, 2026
33de5c3
tokenRange, a token between two, by its ends
echatav Nov 14, 2026
43c1a3d
Token classes intersect & sample without listing large sets
echatav Nov 14, 2026
3e72c8b
Grammar classes in Control.Lens.Grammar.Class; RegEx grammars as inst…
echatav Nov 14, 2026
e145f94
Lexical: per-token Boolean algebras of tests; Tokenized absorbs Token…
echatav Nov 14, 2026
febcf9b
Lexical defaults to TokenRanges, so enumerable tokens' instances are …
echatav Nov 14, 2026
e3f47a0
Tagged tokens' Lexical methods by an iso to their kinds & tags
echatav Nov 14, 2026
10e960b
Cut ranges from token class storage, & the Lexical algebras they forced
echatav Nov 14, 2026
d87d581
Hexadecimal control escapes; digit & numeral rather than show & read
echatav Nov 14, 2026
a80f0c7
Bench workloads' types, explicit for GHC 9.10
echatav Nov 14, 2026
fea5814
ruleTyped, which was jsonTyped, in distributors-grammar, naming base …
echatav Nov 14, 2026
f6d8ab8
Base types' grammars at parity with Read & Show lexemes; no String in…
echatav Nov 14, 2026
20a9264
wrappedG is integralG
echatav Nov 14, 2026
70ea53e
Base types' grammars equivalent to Read & Show, property tested
echatav Nov 14, 2026
7037fce
Base types' grammars, lexemes again: parentheses & whitespace are pre…
echatav Nov 14, 2026
f67103e
Control.Lens.Grammar.Prec: PrecGrammar, equivalent to stock Read & Show
echatav Nov 14, 2026
4e17d28
PrecGrammar lists & strings: precListGrammar, listG, precG; parensG
echatav Nov 14, 2026
6d47225
Prec names: precList, precStep, precReset, precWith, precParens
echatav Nov 14, 2026
bac6f9a
PrecGrammar by Generics, as derived Read & Show; Lookahead; tuples & ()
echatav Nov 14, 2026
9d6fef5
notFollowedByToken, a Lookahead method; used for lexemes' maximal munch
echatav Nov 14, 2026
096cc82
Lookahead: a single notFollowedBy, of a string of token classes
echatav Nov 14, 2026
a0fc424
Lookahead: notFollowedBy a RegEx; eof
echatav Nov 14, 2026
fa37ad2
Prec: precMunch, precMunch1, precSkipSpaces, precEof; prec* names thr…
echatav Nov 14, 2026
b095111
Lookahead names after ReadP: notFollowedBy, eof, munch, munch1, skipS…
echatav Nov 14, 2026
e8a761e
Lookahead: prefix matching local to Parsor's notFollowedBy
echatav Nov 14, 2026
2d10345
Update Prec.hs
echatav Nov 14, 2026
ce32767
asdf
echatav Nov 14, 2026
eacd12e
,jhg
echatav Nov 19, 2026
83e1623
Prec: read binary literals only on base >= 4.21, as read does
echatav Nov 19, 2026
9f27c9e
Merge remote-tracking branch 'origin/json-algebra' into json-algebra-…
echatav Nov 20, 2026
3fd4f53
Merge json-algebra: quantK, quantP, ctxList & ctxTree
echatav Nov 20, 2026
9e41526
Control.Lens.Grammar.Pretty: Wadler-Leijen layout for grammars
echatav Nov 20, 2026
bc8c60d
Update README.md
echatav Nov 20, 2026
c837804
Update Pretty.hs
echatav Nov 20, 2026
8e9b36e
SExpr's pretty grammar spells out its constraints, PrettyGrammatical …
echatav Nov 20, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .hlint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,5 @@
# Printor matches its pairs strictly, for speed.
- ignore: {name: "Use first", within: Data.Profunctor.Grammar}
- ignore: {name: "Use snd", within: Data.Profunctor.Grammar}
- ignore: {name: "Use first", within: Data.Profunctor.Grammar.Printor}
- ignore: {name: "Use snd", within: Data.Profunctor.Grammar.Printor}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ With Stack:

```sh
stack build --test --bench
DISTRIBUTORS_RUN_DOCTESTS=1 stack test
stack test
```

With Cabal:
Expand Down
109 changes: 109 additions & 0 deletions distributors-grammar/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,115 @@

## Unreleased

- `Control.Lens.Grammar.Pretty`: grammars laid out by Wadler-Leijen
layout combinators, `Lined`'s `hardlineP` & `LaidOut`'s `flatAltP`,
`nestP`, `groupP` & `alignP`, with `lineP`, `lineP'` & `softlineP`.
`PrettyGrammar` types, run by `Grammatical` interpreters which are
`Lined` & `LaidOut`, print, by
`Prettor`, a printer into a `Monoid`, a @prettyprinter@ `Doc`, by
`prettyG`, laid out by `layoutG` in a `LayoutStyle`; & parse, by
`Parsor`, every layout they print, with any indentation, which `RegEx`,
`Bnf` & `Grammor` describe. `Grammor` is `IsString`.
- `ruleTyped`, in `Control.Lens.Grammar.BackusNaur`, a `rule` named by its
`Typeable` type, module-qualified, which was `distributors-json`'s
`jsonTyped`. Base types' grammars name their rules by it.
- Regular expressions escape control characters by hexadecimal codes,
@\\x{e}@, rather than by names, which were ambiguous, @\\SO@ & @H@
reading as @\\SOH@; codes parse in either case.
- `digit` & `numeral`, digits & natural numbers in a base, by their values,
rather than by `show` & `read`, which `Natural` & `Integer` grammars,
quantifiers' counts, & the examples use.
- `regexGrammar` & `regbnfGrammar` are removed: they're the `Grammar Char`
instances of `RegEx Char` & `Bnf (RegEx Char)`, by `grammar`. The classes
`RegGrammar`, `Grammar` & `CtxGrammar`, & the base types' instances, are
in `Control.Lens.Grammar.Class`, which `Control.Lens.Grammar` re-exports,
so that `Control.Lens.Grammar.RegEx` defines its own instances; their
tutorials are `Control.Lens.Grammar`'s module documentation.
- `RegEx` has counts, a `KleeneTimes` constructor, normalized by `quantK`,
rendered @x{n}@, @x{n,}@ & @x{n,m}@, & parsed so, normalizing; `Grammor`s
keep counts, so `exactly`, `atLeast`, `atMost` & `inRange` render by them.
Machines & analyses unroll them.
- `RegEx` records `try`, by a `Backtracking` constructor, whose language
is its own, rendered @\\t{…}@; `ll1Conflicts` skips conflicts resolved
by `try`. `Tried` is part of `RegGrammatical`.
- Character classes render runs of three or more plain characters as
ranges, @[a-z]@, & parse ranges; a dash in a class is escaped, @\\-@.
- `Control.Lens.Grammar.Prec`: Haskell types' grammars, by precedence,
equivalent to their stock `Read` & `Show` instances. `PrecGrammar` types,
run by `PrecGrammatical` interpreters, with `precP`, `precStep`, `precReset` &
`precWith`, as `ReadPrec`'s `prec`, `step` & `reset`, `precParens`, as
`GHC.Read.parens` & `showParen`, & `precList`, as `GHC.Read.list` &
`showList`, & `precListGrammar`, as `readListPrec` & `showList`; `Precedence`, an interpreter over any
`Grammatical` one, run by `precG`, `readsPrecG` & `showsPrecG`. Instances for
lists, & `String`s, as string literals, with `showLitString`'s & `read`'s
escapes, empty escapes & gaps, maximally munched, as `read`'s lexer does;
`Char`, with `showLitChar`'s escapes & `read`'s named, control & numeric
escapes, up to `maxBound`; `Natural`, `Integer`, & sized `Int`s & `Word`s,
which wrap out of range as `read` does, in decimal, & binary, octal &
hexadecimal; & `Double`s & `Float`s, by `floatToDigits` & `fromRational`;
negative numbers parenthesized above precedence 6, as `showsPrec` does, &
read bare at any, as `read` does. Property tested against `showsPrec` &
`readsPrec`, at each precedence. Haskell types have no `Grammar` instances.
- `PrecGrammar` defaults, by `GHC.Generics`, to derived `Read` & `Show`
instances' syntax: nullary & prefix constructors, at precedence 10,
records, read bare at 11 & shown within parentheses there, with operator
fields, infix constructors, symbolic & backquoted, at their fixities, &
symbolic constructors in prefix. Instances for `Bool`, `Ordering`, `Maybe`
& `Either`, by it, & for `()` & tuples. `NotFollowedBy`, negative lookahead
by `notFollowedBy` a `RegEx`, which `PrecGrammatical` requires, &
`eof`, `munch`, `munch1` & `skipSpaces`, as `ReadP`'s,
so that identifiers, symbols & numbers are munched maximally, &
whitespace skipped greedily, as `read`'s lexer does.
- `Predicate`s are `Tokenized` & `TokenAlgebra`s, as predicates are; &
`ReadPrec` runs grammars as `ReadP` does, `Tokenized`, a `TokenAlgebra`,
`TerminalSymbol`, `Ruled`, `BackusNaurForm` & `Tried`.
- `Control.Lens.Grammar.Analysis`, static analyses of `RegEx`tended
t'Bnf's over any token: `foldRegEx`, the homomorphism out of `RegEx`, the
free `KleeneStarAlgebra` of token classes & nonterminals, & `fixRules`,
least fixpoints over a t'Bnf''s `bnfRules`; `Leading`, nullability &
leading atoms, by which `firsts` & `leftRecursive`; `Guard`, a
`BooleanAlgebra` of one-token lookahead, by which `follows`; &
`ll1Conflicts`, built of them, testing each example's @LL(1)@ flag.
The lambda example's abstraction rule is renamed, as it shared its name
with the grammar's, merging into a left-recursive rule.
- `RegString` & `RegBnf` are removed: `RegEx Char` & `Bnf (RegEx Char)`
are them, `IsString` & `IsList` by the string syntax of regular
expressions, of `regexGrammar` & `regbnfGrammar`, & their `Grammar Char`
instances, while their `Show` & `Read` are stock; render them by
`toList` or `putStringLn`. `regstringG` is renamed `regexG`, & `regexG`
& `regbnfG` generate over any token, `RegEx token` & `Bnf (RegEx token)`.
- Modules, without orphans: `Control.Lens.Grammar.BackusNaur` has `Ruled`,
`BackusNaurForm` & `Tried`, & the `RegGrammatical`, `Grammatical` &
`CtxGrammatical` constraints, `Lexical` being folded into
`RegGrammatical`, of which it was always a part; `Control.Lens.Grammar.RegEx`
has `RegEx` & `Bnf`, with `liftBnf0`, `liftBnf1`, `liftBnf2` & `diffB`,
& `regexGrammar` & `regbnfGrammar`; & `Printor` has its own module,
`Data.Profunctor.Grammar.Printor`, re-exported by
`Data.Profunctor.Grammar`. `Control.Lens.Grammar` re-exports them all.
- `Control.Monad.Fail.Try`, with `MonadTry` & `MonadicTry`, is removed.
`Control.Lens.Grammar.BackusNaur` has, instead:
- `Ruled`, with `rule`, split from `BackusNaurForm`, now its subclass,
whose `ruleRec` defaults to @ruleRec label = rule label . fix@, so
that its invariant holds for any `rule`;
- `failP`, renamed from `failBnf`, for any `Ruled` `DisjunctiveMonoid`;
- `Tried`, with `try`, for any grammar, not only monads, defaulting to
`id`, as `try` doesn't change a language. `Parsector` backtracks by
it, a t'Bnf' tries its start, & a `RegEx` doesn't record it, for now.

`Grammatical` requires `Tried`, so context-free grammars can `try`, &
`CtxGrammatical` requires `MonadPlus` & `MonadFail` of `Filtrator`s.
`Control.Lens.Grammar` still re-exports `Alternative`, `MonadPlus`,
`MonadFail` & `Filterable`.
- `RegGrammar`, `Grammar` & `CtxGrammar` are classes, of a type's
grammar over a token, by `regGrammar`, `grammar` & `ctxGrammar`, each
defaulting to the more restrictive, & `RegGrammatical`, `Grammatical`
& `CtxGrammatical` are the constraints on profunctors which run them,
typing grammar values, as @Grammatical Char p => p a a@. Runners take
the grammar from the class: parsers & printers by inference, as
@parseG \@Arith@, & generators by a required type argument, as
@regbnfG Arith@, or @regbnfG (type SemVer)@ where a constructor shares
the type's name. Run a grammar value by the backends' own runners,
`parseP`, `printP`, `parsecP` & co.
- Token classes test their sets of tokens logarithmically, by `Set`, in
`tokenClass`'s default & in `oneOf` & `notOneOf` of predicates, rather
than linearly by `Foldable`: parsing the JSON example, whose characters
Expand Down
43 changes: 23 additions & 20 deletions distributors-grammar/bench/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Control.Lens.Comonocle (_BoundedEnum, indexComonocle)
import Control.Monad (unless)
import Data.List (intercalate)
import Test.Tasty.Bench
import qualified GHC.Exts as Exts

import Examples.Arithmetic
import Examples.Expression
Expand All @@ -36,7 +37,8 @@ main = do
defaultMain groups

-- | A named workload: a grammar, an input string & its syntax value.
data Workload = forall a. Eq a => Workload String (CtxGrammar Char a) String a
data Workload = forall a. Eq a =>
Workload String (forall p. CtxGrammatical Char p => p a a) String a

-- | Benchmark each workload at each backend which round-trips it.
backends :: String -> [Workload] -> IO Benchmark
Expand All @@ -50,7 +52,7 @@ backends name workloads = bgroup name . concat <$> traverse workload workloads

cases
:: forall a. Eq a
=> String -> CtxGrammar Char a -> String -> a
=> String -> (forall p. CtxGrammatical Char p => p a a) -> String -> a
-> [(String, Bool, Benchmark)]
cases wname g str a =
[ ( wname <> "/parse"
Expand All @@ -72,18 +74,18 @@ cases wname g str a =
]
where
parser :: String -> Maybe (a, String)
parser = parseG g
parser = parseP (g :: Parsor String Maybe a a)
printer :: a -> Maybe (String -> String)
printer = printG g
printer = printP (g :: Printor String Maybe a a)
parsector :: String -> ParsecState String a
parsector = parsecG g
parsector = parsecP (g :: Parsector String a a)
unparsector :: a -> String -> ParsecState String a
unparsector = unparsecG g
unparsector = unparsecP (g :: Parsector String a a)

digitG :: RegGrammar Char Char
digitG :: RegGrammatical Char p => p Char Char
digitG = asIn @Char DecimalNumber

commaG :: RegGrammar Char ()
commaG :: RegGrammatical Char p => p () ()
commaG = terminal ","

size :: Int
Expand Down Expand Up @@ -137,17 +139,17 @@ varifocals =
-- | The test examples, scaled up.
examples :: [Workload]
examples =
[ Workload "arith" arithGrammar arithStr (parsed arithGrammar arithStr)
, Workload "expr" exprGrammar exprStr (parsed exprGrammar exprStr)
, Workload "expr left-assoc" leftGrammar leftStr (parsed leftGrammar leftStr)
, Workload "json" jsonGrammar jsonStr (parsed jsonGrammar jsonStr)
, Workload "sexpr" sexprGrammar sexprStr (parsed sexprGrammar sexprStr)
, Workload "lambda" lambdaGrammar lambdaStr (parsed lambdaGrammar lambdaStr)
, Workload "semver" semverCtxGrammar semverStr (parsed semverCtxGrammar semverStr)
[ Workload @Arith "arith" arithGrammar arithStr (parsed arithGrammar arithStr)
, Workload @Expr "expr" exprGrammar exprStr (parsed exprGrammar exprStr)
, Workload @Expr "expr left-assoc" leftGrammar leftStr (parsed leftGrammar leftStr)
, Workload @Json "json" jsonGrammar jsonStr (parsed jsonGrammar jsonStr)
, Workload @SExpr "sexpr" sexprGrammar sexprStr (parsed sexprGrammar sexprStr)
, Workload @Lambda "lambda" lambdaGrammar lambdaStr (parsed lambdaGrammar lambdaStr)
, Workload @SemVer "semver" semverCtxGrammar semverStr (parsed semverCtxGrammar semverStr)
]
where
parsed :: CtxGrammar Char a -> String -> a
parsed g s = case parseG g s of
parsed :: forall a. (forall p. CtxGrammatical Char p => p a a) -> String -> a
parsed g s = case parseP (g :: Parsor String Maybe a a) s of
Just (a, "") -> a
_ -> error ("bench input doesn't parse: " <> take 40 s)
terms n ops = concat
Expand All @@ -165,9 +167,10 @@ examples =
-- | Building static grammars, as regular expressions & Backus-Naur forms.
statics :: Benchmark
statics = bgroup "static"
[ bench "regstring sepBy" $ whnf (length . show . regstringG) (sepBy commaG several digitG)
, bench "regbnf json" $ whnf (length . show . regbnfG) jsonGrammar
, bench "regbnf expr" $ whnf (length . show . regbnfG) exprGrammar
[ bench "regstring sepBy" $ whnf (\g -> length (Exts.toList (runGrammor g :: RegEx Char)))
(sepBy commaG several digitG)
, bench "regbnf json" $ whnf (\() -> length (Exts.toList (regbnfG @Char Json))) ()
, bench "regbnf expr" $ whnf (\() -> length (Exts.toList (regbnfG @Char Expr))) ()
]

-- | Optics as lenses, at functions.
Expand Down
14 changes: 13 additions & 1 deletion distributors-grammar/distributors-grammar.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ source-repository head
library
exposed-modules:
Control.Lens.Grammar
Control.Lens.Grammar.Analysis
Control.Lens.Grammar.BackusNaur
Control.Lens.Grammar.Class
Control.Lens.Grammar.Machine
Control.Lens.Grammar.Prec
Control.Lens.Grammar.Pretty
Control.Lens.Grammar.RegEx
Control.Lens.Grammar.Symbol
Control.Lens.Grammar.Token
Control.Monad.Fail.Try
Data.Profunctor.Grammar
Data.Profunctor.Grammar.Parsector
Data.Profunctor.Grammar.Printor
other-modules:
Paths_distributors_grammar
autogen-modules:
Expand Down Expand Up @@ -92,6 +96,7 @@ library
, lens >=5.0 && <6
, monad-loops >=0.4.3 && <1
, mtl >=2.2 && <3
, prettyprinter >=1.7 && <2
, profunctors >=5.6 && <6
, random >=1.2 && <2
, witherable >=0.4 && <1
Expand Down Expand Up @@ -157,6 +162,7 @@ test-suite doctests
, lens >=5.0 && <6
, monad-loops >=0.4.3 && <1
, mtl >=2.2 && <3
, prettyprinter >=1.7 && <2
, profunctors >=5.6 && <6
, random >=1.2 && <2
, witherable >=0.4 && <1
Expand All @@ -174,9 +180,12 @@ test-suite test
Examples.Lambda
Examples.LenVec
Examples.RegString
Examples.Rho
Examples.SemVer
Examples.SExpr
Properties.Base
Properties.Kleene
Properties.Pretty
Properties.Varifocal
Paths_distributors_grammar
autogen-modules:
Expand Down Expand Up @@ -230,11 +239,13 @@ test-suite test
, containers >=0.6 && <1
, distributors
, distributors-grammar
, free >=5.1 && <6
, hspec >=2.7 && <3
, lens >=5.0 && <6
, megaparsec >=9.0 && <10
, monad-loops >=0.4.3 && <1
, mtl >=2.2 && <3
, prettyprinter >=1.7 && <2
, profunctors >=5.6 && <6
, random >=1.2 && <2
, witherable >=0.4 && <1
Expand Down Expand Up @@ -304,6 +315,7 @@ benchmark core
, lens >=5.0 && <6
, monad-loops >=0.4.3 && <1
, mtl >=2.2 && <3
, prettyprinter >=1.7 && <2
, profunctors >=5.6 && <6
, random >=1.2 && <2
, tasty-bench >=0.3 && <1
Expand Down
2 changes: 2 additions & 0 deletions distributors-grammar/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies:
- MemoTrie >= 0.6 && < 1
- monad-loops >= 0.4.3 && < 1
- mtl >= 2.2 && < 3
- prettyprinter >= 1.7 && < 2
- profunctors >= 5.6 && < 6
- QuickCheck >= 2.14 && < 3
- random >= 1.2 && < 2
Expand Down Expand Up @@ -99,6 +100,7 @@ tests:
dependencies:
- distributors
- distributors-grammar
- free >= 5.1 && < 6
- hspec >= 2.7 && < 3
- megaparsec >= 9.0 && < 10
- QuickCheck >= 2.14 && < 3
Expand Down
Loading
Loading