free-foil
Efficient Type-Safe Capture-Avoiding Substitution for Free (Scoped Monads)
https://github.com/fizruk/free-foil#readme
| LTS Haskell 24.50: | 0.2.0 |
| Stackage Nightly 2026-07-15: | 0.3.2 |
| Latest on Hackage: | 0.3.2 |
free-foil-0.3.2@sha256:cec5ff659a0214561bc7d5dc056137d1451d84e7133c658868e7f0ee1d762ee4,5491Module documentation for 0.3.2
- Control
- Control.Monad
- Data
free-foil
Efficient Type-Safe Capture-Avoiding Substitution for Free (Scoped Monads).
Changes
CHANGELOG for free-foil
0.3.2 — 2026-07-15
An additive release: the annotation layer, the ZipMatchK derivers, and a set of performance improvements. Upgrading from 0.3.1 needs no work.
New:
-
Control.Monad.Free.Foil.Annotated, a layer that annotates every node with anann termbuilt from the node’s own term (#42, #46).Bifoldableskips the annotation, soalphaEquivis annotation-blind;freeVarsOfAnnotatedsees the variables inside annotations thatfreeVarsOfskips.AnnSig‘sZipMatchKis derived rather than generic, since on an annotated signature it sits on a typechecker’s hottest path. An annotation-blind instance must returnJustlazily, and the module documents why a strict one is wrong. -
Data.ZipMatchK.TH, Template Haskell derivers forZipMatchK(#43).deriveZipMatchKzips every type parameter,deriveZipMatchK2the last two (for a signature with an annotation parameter), withderiveZipMatchK1andderiveZipMatchKWithfor the rest. The generic instance rebuilds aGenerics.Kindview of every node on every comparison, at a cost that grows with the number of constructors. The derived instance is flat, and on a 44-constructor signature runsalphaEquiv1.8 times faster for 2.3 times less allocation. It replaces the deriver 0.3.0 dropped along with the oldZipMatchclass. -
sinkContainersinks a whole container of sinkables (anIntMaporMapof terms) in O(1) by coercion, instead offmap sinkover the spine (#44). AScopeis not sinkable and aNameMapmust stay total, both noted with the function. -
zipmatchkandnormalizebenchmarks (#43, #45):alphaEquivacross signature sizes, and full β-normalisation of untyped λ-terms. CI builds them and does not run them.
Performance:
-
alphaEquivandunsafeEqASTno longer copy the node into a tuple before folding over it. They recurse inside the zipping functions, allocating nothing and short-circuiting on the first mismatch (#44). On the benchmark, 294 µs to 217 µs and 3.1 MB to 2.8 MB with a derived instance. Behaviour is unchanged. -
substitute,alphaEquiv,refreshASTand friends are nowINLINABLE, so a call site can specialise them for its own signature instead of passing dictionaries (#44).
Changed:
-
soasandLanguage.LambdaPi.Impl.FreeFoilTHderive theirZipMatchKinstances.Language.LambdaPi.Impl.FreeFoilwrites them out by hand, so the two implementations show both ways. -
The
baselower bound is now>= 4.19(GHC 9.8). The package has required GHC 9.8 all along throughtemplate-haskell >= 2.21, so this only makes the bound honest, and lets Hackage’s documentation builder pick a compatible GHC (#15).
0.3.1 — 2026-07-14
A bug fix and a set of additions, all of them prompted by the projects built on free-foil. Nothing is removed or changed, so upgrading from 0.3.0 needs no work.
Fixes:
-
mkFreeFoilandmkFreeFoilConversionsgenerated ill-typed code for a raw constructor whose shape does not line up with the free foil node it becomes (see #38):Let ::= Pattern Term ScopedTerm -- a term between the pattern and its scope LetRec ::= Pattern ScopedTerm ScopedTerm -- one pattern binding two scopes- The pattern synonym’s arguments and its type signature were computed by two traversals that disagreed, and the conversions had the mirror problem.
- A constructor binding several scopes binds the same raw name in each of them, so raw-to-foil now sends that one binder into every scoped child, and foil-to-raw reads it back from the first.
- Constructors with at most one scoped child generate exactly what they did before.
- Reported and diagnosed by @AbsoluteNikola, who had to vendor the generated code by hand in free-foil-refinement-types.
New functions, absorbed from the projects built on free-foil, several of which were importing Control.Monad.Foil.Internal to get at them (see #40):
popNameBinder— the inverse ofaddNameBinder, for leaving a binder.withFreshNameBinderList— a fresh binder for each element of a list, bound to it in aNameMap.snocNameBinderListandconcatNameBinderLists.nameBindersList,fromNameBindersListandnameMapToScopeare now exported (they existed, but were unreachable).- With thanks to @Probirochniy, @fedor-ivn and @snejugal (free-foil-hou), and to @evermake, @frog-da and @Vikono (free-foil-typecheck), whose copies of these helpers carried the comment “Should be in
Control.Monad.Foil”.
Documentation:
unifyNameBindersrenames the binder with the larger name towards the one with the smaller name. That is safe, since the renaming is pushed through a term withliftRM, which refreshes a binder whenever it would capture. The haddock now says so, with a test to match (see #39). Raised by @AbsoluteNikola.
0.3.0 — 2026-07-14
This release makes generic deriving the default way to instantiate the library:
Sinkable, CoSinkable, UnifiablePattern and ZipMatchK can now all be
derived via kind-generics,
so a user-defined pattern normally needs no hand-written instances at all.
The cost is one breaking change, described first.
Breaking changes
-
The
ZipMatchclass is removed in favour of the kind-polymorphicZipMatchK, and the modules are reorganised (see #30):Control.Monad.Free.Foil.Genericis replaced byData.ZipMatchK(withData.ZipMatchK.Bifunctor,Data.ZipMatchK.Functor,Data.ZipMatchK.GenericandData.ZipMatchK.Mappings);Control.Monad.Free.Foil.TH.ZipMatch(and itsderiveZipMatch) is removed, andControl.Monad.Free.Foil.THno longer re-exports it.
To migrate, delete the
ZipMatchinstances and anyderiveZipMatchsplices, and keep only theZipMatchKones. Where you hadimport Control.Monad.Free.Foil.Generic instance ZipMatchK a => ZipMatchK (TermSig a) instance ZipMatchK a => ZipMatch (TermSig a) where zipMatch = genericZipMatch2the second instance simply goes away:
import Data.ZipMatchK instance ZipMatchK a => ZipMatchK (TermSig a)ZipMatchKis derived generically, so the signature needs aGenericKinstance (deriveGenericK ''TermSigfromkind-generics-th). Note thatkind-generics-this not on Stackage, and is not a dependency of this library: a package usingderiveGenericKmust depend on it itself. -
α-equivalence and refreshing now ask for more of the binder:
alphaEquiv,alphaEquivRefreshed,refreshASTandrefreshScopedASTrequireSinkableK binder(andZipMatchK sigin place ofZipMatch sig). For binders and patterns generated by our Template Haskell, or derived generically, this constraint is already satisfied and no change is needed.
Generic deriving of patterns (see #31)
- New
SinkableKclass, generalising bothSinkableandCoSinkable: a typef n₁ n₂ … nₖis treated as a generalised binder with variables and terms in scopesn₁, n₂, …, nₖ. Generic (kind-polymorphic) implementations are provided forsinkabilityProofandcoSinkabilityProof, soSinkableandCoSinkableinstances can be left empty. - New
HasNameBindersclass, generalising access to the nestedNameBinders of a pattern. This is what makes a genericwithPatternpossible, so user-defined patterns no longer need a hand-written traversal. UnifiablePatternnow has a default implementation viaCoSinkable, soinstance UnifiablePattern MyPatternnormally suffices.- Malformed user-defined patterns are now rejected with a readable type error
by a separate generic check (
GValidNameBinders), instead of failing obscurely deeper in the machinery. Terms (and other types) are allowed in patterns, as long as the binders are threaded correctly.
New functions (see #32)
unsinkAST— unsink anASTfrom a larger scope into a smaller one, when possible.freeVarsOfandfreeVarsOfScopedAST— collect the free variables of anAST.nameMapToScope— recover aScopefrom aNameMap.NameMapis now aFunctor,FoldableandTraversable.
Fixes
GValidNameBindersno longer rejects a valid constructor that equates its scopes and carries more than one field (substitution now recurses through sums and products).
0.2.0 — 2024-10-27
- Generate
COMPLETEpragma inmkPatternSynonyms(see #26) - Polykind
ZipMatchKclass with default generic implementation viakind-generics(see #27) - New experimental TH generation for Free Foil with support for rich syntax in
Control.Monad.Free.Foil.TH.MkFreeFoil(see #28)
0.1.0 — 2024-08-18
-
Generalize functions for binders, support general patterns (see #16)
-
Add
withPatternmethod toCoSinkable. It can be seen as a CPS-style traversal over binders in a pattern. Our Template Haskell support covers generation ofwithPattern, so normally the user does not have to think about it. -
Generalize many functions to work with arbitrary patterns, not just
NameBinder:withFreshPattern— towithRefreshedPatternandwithRefreshedPattern'extendScopePattern— extend a given scope with all binders in a given patternnamesOfPattern— collect all names from a patternunsinkNamePattern— try to unsink names from a scope extended with binders from a given patternassertDistinctPattern— establish that extended scope is distinct (if outer scope is)assertDistinctExt— establish that extended scope is distinct and indeed an extension
-
Implement unification for patterns in
unifyPatterns. This turns out to be one of the most difficult places, especially for compound patterns. Implementing patterns properly on the user side not comfortable at all! Luckily, we provide useful helpers likeandThenUnifyPatternsandandThenUnifyNameBinders, as well as Template Haskell support to deriveUnifiablePattern. -
Generalize Free Foil to support arbitrary patterns.
-
The
FoilandFreeFoilTHimplementations now make use of the generalized pattern support.
-
0.0.3 — 2024-06-20
-
Add α-equivalence checks and α-normalization (see #12):
Control.Monad.Foilnow offers more helpers to work with binder renaming and binder unification. These helpers can be used to implement (efficient) α-equivalence.- In
Control.Monad.Free.Foilgeneral implementation of α-equivalence and α-normalization is provided.
-
Add general conversion functions for free foil (see #14):
Control.Monad.Free.Foilnow offersconvertToASTandconvertFromASTfunctions enabling easier implementation of conversions raw and scope-safe representations.
-
Add Template Haskell functions for free foil (see #14):
Control.Monad.Free.Foil.THcontains many useful functions to generate free foil from a raw representation (e.g. generated via BNFC), including generation of the signature, convenient pattern synonyms,ZipMatchinstance, and conversion helpers.
0.0.2 — 2024-06-18
- Improve TH to support parametrized data types (see #11)
- Split
lambda-piinto its own package (see #10) - Switch to
template-haskell >= 2.21.0.0(to support latest Stackage Nightly) - Fix doctests (see #9)
0.0.1 — 2024-06-08
First release, corresponding to the ICCQ 2024 paper.