|
Posted
almost 13 years
ago
by
Adrian Keet
Say we have a few types for things that have a “name”, which is a String that may or may not be modifiable. We want to have a type class for these things that exposes a lens-like method for accessing the name in a type-safe manner, so that it may be
... [More]
a Lens or just a Getter depending on the instance.
First let’s write some example types and some lenses for them:
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies, ConstraintKinds #-}
import Control.Lens
import GHC.Exts -- for the Constraint kind
newtype ReadWrite = ReadWrite String
newtype ReadOnly = ReadOnly String
-- Functor f => SimpleLensLike f ReadWrite String
rwName :: SimpleLens ReadWrite String
rwName f (ReadWrite s) = ReadWrite `fmap` f s
-- Gettable f => SimpleLensLike f ReadWrite String
roName :: Getter ReadOnly String
roName = to (\(ReadOnly a) -> a)
Now rwName is a full Lens, while roName can only be used as a Getter. We’d like to make a single class that lets us get and set the name of a ReadWrite, but only get the name of a ReadOnly.
Using a multi-parameter type class
Now the only difference between a Getter and a Lens is the restriction on the type of Functor it can be used with. So, the first idea is to let our class take that Functor as a parameter:
class NamedMPTC f a where
nameMPTC :: SimpleLensLike f a String
instance Functor f => NamedMPTC f ReadWrite where
nameMPTC = rwName
instance Gettable f => NamedMPTC f ReadOnly where
nameMPTC = roName
And now nameMPTC gives us a type-safe way to get what we want from both ReadOnly and ReadWrite:
ghci> ReadWrite "a" ^. nameMPTC
"a"
ghci> ReadWrite "a" & nameMPTC .~ "b"
ReadWrite "b"
ghci> ReadOnly "c" ^. nameMPTC
"c"
ghci> ReadOnly "c" & nameMPTC .~ "d"
-- No instance for (Gettable Mutator) arising from a use of `nameMPTC' ...
This appears to work pretty well. We can easily restrict to different types of Functors; for example, restricting to Applicative will make nameMPTC a Traversal, in case we had something with multiple names.
When we try to use this in practice, however, we run into problems. If NamedMPTC had another method that made no reference to f, then any time we tried to use it, the type checker would blow up with an ambiguous type error! The only way around this would be to have our lens-like method be in its own class, which sounds pretty undesirable. What we needed was a way to impose the constraint on f only on the lens-like method, not the entire class. So let’s forget this and look at another approach.
Using the Constraint kind
This means we’ll need at least GHC 7.4. Let’s see the code first:
class Named a where
type NameConstraint a f :: Constraint
name :: NameConstraint a f => SimpleLensLike f a String
instance Named ReadWrite where
type NameConstraint ReadWrite f = Functor f
name = rwName
instance Named ReadOnly where
type NameConstraint ReadOnly f = Gettable f
name = roName
Let’s see how the types work. First let’s look at the type of name:
name :: (NameConstraint a f, Named a) => SimpleLensLike f a String
When we specialize to a = ReadOnly, the Named a constraint is satisfied and NameConstraint a f just becomes Gettable f, so we end up with
name :: Gettable f => SimpleLensLike f ReadOnly String
which is exactly the type of roName we had before.
With this approach, name works exactly how nameMPTC did earlier. But notice that this time the constraint on f is only applied to name, not the entire class. This means we can easily add other methods to the class without causing any ambiguous type issues, and we can even add different lens-like methods with different constraints. Fantastic! [Less]
|
|
Posted
almost 13 years
ago
by
Edward Kmett
Greetings!I am happy to announce the release of version 3.7 of the lens package, which provides "Lenses, Folds, and Traversals" for working with arbitrary data types.
In its simplest form, a lens is a getter/setter pair, that can be composed and
... [More]
reasoned about with common sense laws that you can also derive from the Functor and Traversable laws you already know or even derive by reasoning in terms of comonad coalgebras for the even more theoretically inclined.
The lens package provides lenses along with a surprisingly powerful set of generalizations and specializations of this idea, in a manner that subsumes the notion of a "semantic editor combinator", allowing you to enrich them with the ability to read back from the targets. In the form taken by the lenses in this package, lenses are empowered to safely change the types of the fields that they edit in a manner that not only can you still reason about, but where the changes in types helps you to reason about what they can or cannot do.
One of the major design goals of lens has been that you should be able to pick up lens combinators and apply them meaningfully to a mishmash of lenses, traversals, isomorphisms, getters, and setters, etc. even without fully understanding all of the types. This encourages active exploration and users who are pleasantly surprised rather than frustrated and angry. To this end we've actively stocked the haddocks for the project with types it may be easier to think about each combinator as having, which can serve as training wheels that will help you make your way around.
Most interestingly from a package maintainer perspective, unlike previous packages that provided lenses, it is possible to provide lenses (and traversals) that are compatible with the lens library without incurring any dependency for your package at all. For instance the simplest Traversal is traverse from Data.Traversable. traverse . traverse is also a valid Traversal, and it can be composed with other lenses and traversals without any casting or coercion.
A large number of combinators are provided that automatically 'do the right thing' when presented with the various generalizations and specializations of the concept of a lens, e.g. when supplied a Traversal instead of a Lens, a combinator that returned a result based on the target of the Lens, may now return a monoidal summary of all of the results targered by the Traversal.
Major Features:* Lenses, getters, setters, isomorphisms, folds, "prisms", monadic actions, and indexed versions of these constructions that can all be composed with (.) from the Prelude in a manner that they read quite naturally to an imperative programmer who expects (.) to be field access and to compose in the 'wrong' direction, while still retaining the ability to reason about the resulting code.
* Type-safe zippers into arbitrary user data structures, where you can move down into a lens or laterally through a traversal and can come back up, following a breadcrumb trail in the type.
* Lens contains a generalized version of Neil Mitchell's uniplate in such a way that the uniplate combinators themselves that many people already know how to use can be used on an arbitrary traversal, and uniplate/biplate simply act as a Traversal, and are often ~35% or more faster than the original.
* Lens comes "batteries included" with classes and combinators for working with many common libraries that fall within the Haskell Platform. No dependencies are incurred that fall outside of the platform, unless those dependencies are needed to implement lens itself.
* We provide configurable template-haskell generators for producing lenses, isomorphisms and traversals for your own data types.New in this release:* Prisms are categorically dual to Lenses and provide a form of first class pattern. They can be used directly as a Traversal. Many operations that formerly required an isomorphism can be used directly on a Prism, and every Isomorphism can be used as a Prism.
* With this latest release we've incorporated a large amount of community feedback into the API design and have vastly expanded the documentation with hundreds of additional examples and test cases.
* We've renamed a number of operations to reduce naming conflicts with third party libraries to a minimum, and improve consistency. in particular we no longer conflict with Control.Arrow.
* We've overhauled the zipper API to permit easier use of multiple simultaneous zippers, and to make zipper movements more compositional.Resources:
* Wiki: We have a FAQ, which includes a number of links to source material, a quick getting-started guide, a discussion of the Derivation, and even a UML diagram distributed among the other content of the wiki. If you find something missing or otherwise out of whack then feel free to edit it. =)
* Larger Examples: We also have a number of larger examples included in the lens distribution, including a fully operational game of Pong that plays using Gloss and a Brainfuck interpreter.
* IRC: The #haskell-lens channel on irc.freenode.net is full of helpful people who work with or on lens itself. Lamdabot on #haskell has lens installed, and golfing solutions to user questions using lens as well as more traditional techniques has become disturbingly common.
as* I gave a talk last month at Google in San Francisco about "Lenses, Folds, and Traversals", and just under 100 people showed up. Sadly, this talk was not recorded.
* However, I plan to give an updated version of that talk at the New York Haskell User Group meeting on Wednesday, December 12th from 7:00-9:00PM. At the time of this writing we have 94 slated as coming and room for 26 more, so if you'll be in the New York area on the 12th and are interested, register soon. =) That said, this time the talk will be recorded and the video will be made available online, so even if you can't make it out, you can still enjoy the talk.
Support: * We currently support the last 3 versions of the Haskell Platform, including versions of GHC back to 7.0.4 and are actively maintaining the project as part of stackage. We intend to maintain support for a rolling wave of at least 2 previous Haskell Platform releases going forward.
* If you have any questions or comments, please feel free to contact me by email, github, or IRC.-Edward [Less]
|