mutable-lens: Interoperate mutable references with regular lens

[ apache, data, generics, lenses, library ] [ Propose Tags ]

Use mutable references as lens, compose them with other lens, and run them.

You may have heard of lens being described as "functional" or "first-class" references, with some nice properties such as composition. However the equivalence is more than just abstract - mutable references can be readily converted into lens form, interoperable with other pure lens. This makes use of the fact that "mutability" in a high-level language focusing on purity, is really just an alias for a pure state monad that is treated specially by the runtime system. A mutable reference over a value of type a with world state of type s, *is* therefore an actual Lens s s a a. There is no need for handwaving or connotative language, the isomorphism is direct and concrete.

This package demonstrates this isomorphism, converting between the two forms. You then have access to the whole of the lens ecosystem and its compositional properties, and use them with mutable references without other special-cased shims or mutable-specific abstractions.

Uniformity of API is another major benefit - if you want to switch to a different reference type, you only have to change your type definitions and imports, you don't have to change all readSomeVar to readOtherVar, writeSomeVar to writeOtherVar, or was it putOtherVar, and does the atomicModifyVar function take a s -> (a, s) or a s -> (s, a) again?

On the other hand, this package has not been optimised for performance, and currently only supports GHC. We would certainly welcome contributions in these areas, if you are interested in running this code in production. Until that happens, please be cautious and treat this code as experimental.

For what it's worth, the API is likely to be very stable: an isomorphism to another well-established library, is not a complex API.

See unit tests for example usage, e.g. MutableTest

Versions [faq] 0.1.0.0, 0.2.0.0, 0.3.0.0, 0.4.0.0
Change log CHANGELOG.md
Dependencies base (>=4.12.0.0 && <5), lens, primitive, stm [details]
License Apache-2.0
Copyright 2020 Ximin Luo
Author Ximin Luo
Maintainer infinity0@pwned.gg
Category Data, Lenses, Generics
Home page https://github.com/infinity0/hs-mutable-lens
Bug tracker https://github.com/infinity0/hs-mutable-lens/issues
Source repo head: git clone https://github.com/infinity0/hs-mutable-lens
Uploaded by infinity0 at 2020-05-29T10:36:05Z
Distributions NixOS:0.4.0.0
Downloads 210 total (21 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs available [build log]
Last success reported on 2020-05-29 [all 1 reports]

Modules

[Index] [Quick Jump]

Flags

NameDescriptionDefaultType
dev

Set compile flags for development

DisabledManual

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

For package maintainers and hackage trustees