Skip to content

Work on json-grammar - #36

Open
echatav wants to merge 52 commits into
json-algebrafrom
json-algebra-grammar
Open

echatav wants to merge 52 commits into
json-algebrafrom
json-algebra-grammar

Conversation

@echatav

@echatav echatav commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

and more

echatav and others added 20 commits November 13, 2026 17:11
…raints

RegGrammar, Grammar & CtxGrammar become classes of a type's grammar
over a token, regGrammar, grammar & ctxGrammar, each defaulting to the
more restrictive (RegGrammar => Grammar => CtxGrammar). RegGrammatical,
Grammatical & CtxGrammatical are the constraints on the profunctors
which run them, typing grammar values (p a a).

Runners take the grammar from the class: printG, parseG, unparseG,
parsecG, unparsecG, applicativeG, monadG & readG infer the type;
regstringG, regbnfG & transducerG take it as a required type argument,
(type T) where a constructor shares its name. Grammar values run by
the backends' own runners (parseP, printP, parsecP & co.).

Instances: RegString & RegBnf (their Read & IsList use them), the
TypeScript token & declaration grammars, & the test examples' canonical
grammars; alternatives (powGrammar, leftGrammar, semverCtxGrammar) stay
values, run by the test harness on each backend.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Implements SCRATCHBackusNaur: Ruled (rule) is split from
BackusNaurForm, now its subclass, with ruleRec defaulting to
rule label . fix, so its invariant holds for any rule; failBnf becomes
failP; Tried generalizes MonadTry's try to any grammar, defaulting to
id. Parsector backtracks by Tried, Grammor & Joker delegate, a Bnf
tries its start, & a RegEx doesn't record try, for now (try = id).

Control.Monad.Fail.Try, MonadTry & MonadicTry are removed: Grammatical
requires Tried, & CtxGrammatical, MonadPlus & MonadFail of Filtrators.
Control.Lens.Grammar still re-exports Alternative, MonadPlus, MonadFail
& Filterable. The Json package's profunctors keep their own ruleRec,
by separate Ruled & BackusNaurForm instances; they aren't Grammatical,
so they needn't be Tried. Instances ported from wadler-leijen eaa473d.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
stepping, terminateBy, delimitBy, balanceBy & countWith are generic in
their stream, (Cons s s a a, AsEmpty s), as their bodies only use
_Cons & asEmpty, so they step through a Seq or Text as through a list
(tested at Seq). Varifocal requires a Filterable monad, for
Bifocal ⊂ Varifocal, which these needn't use; the module disables the
redundant-constraint warning that now reports it, as explained there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
steppingTree generalizes stepping from streams to the cofree comonad of
any Traversable g: from a state, each node's focus decides its
children's states, a g-shape of them, or rejects. Foci go in preorder;
printing checks each node's children have the decided shape (Eq (g ())),
so a tree prints only as it parses. stepping is its case at Maybe, of
non-empty streams, but stays direct: through Cofree it was 1.6-1.8x
slower. arityWith, count-prefixed trees, is the tree version of
countWith, tested at Parsor & Printor. Adds free, already a dependency
of lens, for Cofree.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Varifocus's Alternative defaulted many & some, building recursive
AltsP & BindP trees, which broke the Alternator law
many = lmap repeat . manyP & lost the target's own manyP & someP when
run. They're now ManyP & SomeP, as in the other Alternators.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
RegEx Char & Bnf (RegEx Char) replace the newtypes: IsString & IsList by
the string syntax of regexGrammar & regbnfGrammar, Grammar Char
instances, & stock Show & Read (render by toList or putStringLn).
regstringG becomes regexG; regexG & regbnfG generate over any token.

To keep every instance with its type, the modules are restructured:
- BackusNaur: Ruled, BackusNaurForm, Tried, failP, & the Lexical &
  *Grammatical constraints (no longer importing RegEx);
- RegEx: RegEx & Bnf, liftBnf*, diffB, the string grammars & their
  IsString & IsList instances;
- Data.Profunctor.Grammar.Printor: Printor, re-exported by
  Data.Profunctor.Grammar, so RegEx can print without a cycle.
Control.Lens.Grammar re-exports all of it. The HLint baseline follows
Printor's moved code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Static analyses of RegEx-tended Bnfs, over any token, in parts:
- foldRegEx: the homomorphism out of RegEx, the free KleeneStarAlgebra
  of token classes & nonterminals, into any other;
- bnfRules & fixRules: rules by name, & least fixpoints over them;
- Leading: nullability & leading atoms, a KleeneStarAlgebra: at token
  classes, firsts; at rule names, left corners, for leftRecursive;
- Guard: a BooleanAlgebra of one-token lookahead & end of input, for
  follows, inherited from callers to a fixpoint;
- ll1Conflicts, rebuilt of them from textual's, overlaps by one >&<.
Every example's LL(1) flag is checked statically; the lambda example's
abstraction rule shared the grammar's name, & is renamed. Sets are a
DisjunctiveMonoid by union, for left corners.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Lexical was only ever used beside Alternator, which together are
RegGrammatical, so its constraints are RegGrammatical's own, & the
TypeScript helpers are RegGrammatical TsToken.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every Alternative of base, containers & vector is a DisjunctiveMonoid,
by deriving anyclass, adding lists, Seq & Vector; & Set, IntSet, Map &
IntMap disjoin by their monoids, of union.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Exhaustive by GHCi's :info: all 48 Alternative instances visible through
core's dependencies are DisjunctiveMonoids, adding transformers' monad
transformers (each variant), free's Free, F, FreeT, FT & Alt, lens's
ReifiedFold & witherable's WrappedFoldable, by deriving anyclass with
their Alternative instances' contexts, in the class's module.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tokens

- Bits types are BooleanAlgebras of bit vectors (.|., .&., complement),
  by a private Bitwise newtype: every Int & Word, Integer, IntPtr, WordPtr
  & the Foreign.C.Types integers; Natural, lacking complement, only
  disjoins.
- KleeneStarAlgebras: Predicate (star true, as a Boolean algebra is) &
  Ap of an Alternative of Monoids (languages, star by many, a
  near-semiring, documented).
- Predicate is Tokenized & a TokenAlgebra, via token -> Bool; ReadPrec
  runs grammars as ReadP does (Tokenized, TokenAlgebra, TerminalSymbol,
  Ruled, BackusNaurForm, Tried; Filterable in core's Orphanage).
Laws tested at Int, Int8, Word8, Integer & Natural; Ap's star at ReadP;
a grammar at ReadPrec as at ReadP; a token class as a Predicate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
RegGrammar Char instances, each Grammar a rule named by its type:
Char (any token), String (any tokens), Natural (ASCII digits), Integer
(optional minus, digits), Double & Float (NaN, or signed Infinity or
decimals with optional fraction & exponent), printing canonically as
show does & parsing more as read does, by improper isos of hidden
helpers. Sized Ints & Words are CtxGrammars: an out-of-range literal
fails to build, which needs a Filtrator (?<), so they have no regex.
Tested: round trips (incl. NaN, infinities, -0.0), improper parses
(007, -0, 1E+5, 1.50), range failures, & static LL(1) by ll1Conflicts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.2

QR code for preview link

🚀 View preview at
https://morphismtech.github.io/distributors/pr-preview/pr-36/

Built to branch gh-pages at 2026-11-21 00:05 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

echatav and others added 9 commits November 13, 2026 23:32
…ances; Varifocus runs alternatives by anyP

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…Algebra

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…empty

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Reverts e3f47a0, febcf9b, e145f94, 43c1a3d, 33de5c3 & 4d09da1: classes
are stored as written again, OneOf & NotOneOf sets with category tests;
[a-z] stays as regular expression syntax only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…types' rules

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…stance

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
echatav and others added 23 commits November 14, 2026 01:25
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Whitespace & parentheses, as read accepts them, & character codes bounded
by maxBound; readShowEquivalence tests each instance against show &
readMaybe, on variants, decorations & mutations of its values' strings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…cedence's

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Haskell types have PrecGrammar instances, not Grammar instances, run by
PrecGrammatical interpreters, with ReadPrec's prec, step & reset as precP,
stepP & resetP, precedenceP & parensP; Precedence interprets them over any
Grammatical one, for readsPrecG & showsPrecG. Base types' instances are
property tested against showsPrec & readsPrec at each precedence.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Strings, as Read & Show's string literals, with empty escapes & gaps, &
numeric escapes & \SOH maximally munched; whitespace skipped at one place
before each lexeme, as read's greedy skipSpaces does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Constructors nullary, prefix, record & infix, by fixity; lexemes munched
maximally & whitespace skipped greedily by notAhead, as read's lexer does.
Property tested against showsPrec & readsPrec at each precedence, for
Bool, Ordering, (), Maybe, Either, tuples & a type of each shape.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…oughout

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…paces

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
base-4.20 (GHC 9.10) Read doesn't lex 0b/0B literals, so the grammar
disagreed with readsPrec there, failing CI's cabal 9.10.3 and nix jobs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Takes json-algebra's distributors-core, which is this branch's plus the
renames timesK ~> quantK, timesP ~> quantP, stepping ~> ctxList &
steppingTree ~> ctxTree, & renames their uses in distributors-grammar.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Lined (hardlineP) & LaidOut (flatAltP, nestP, groupP, alignP), with
lineP, lineP' & softlineP; PrettyGrammar & PrettyGrammatical; Prettor,
a printer into a Monoid, printing a prettyprinter Doc by prettyG &
layoutG; Parsor parses every layout, any indentation, & RegEx, Bnf &
Grammor describe them. Grammor is IsString. Tested at SExpr.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant