179 points · by harporoeder · 6y ago · 103 comments
https://github.com/ghc-proposals/ghc-proposals/blob/master/p...
data Grade = A | B | C | D | E | F
data Quarter = Fall | Winter | Spring
data Status = Passed | Failed | Incomplete | Withdrawn
data Taken =
Taken { year :: Int
, term :: Quarter
}
data Class =
Class { hours :: Int
, units :: Int
, grade :: Grade
, result :: Status
, taken :: Taken
}
getResult :: Class -> Status
getResult c = c.result -- get
setResult :: Class -> Status -> Class
setResult c r = c{result = r} -- update
setYearTaken :: Class -> Int -> Class
setYearTaken c y = c{taken.year = y} -- nested update
getResults :: [Class] -> [Status]
getResults = map (.result) -- selector
getTerms :: [Class] -> [Quarter]
getTerms = map (.taken.term) -- nested selectorI am so elated to see this change, even if it is 15 years overdo. Nested selectors in particular are beautiful.
Maybe it's time again to do me some haskell for great good.
When/ how can I use this? Do I have to wait for a new GHC release?
I agree that it's very nice, and a big improvement!
E.g. if both data types were to contain an "id" field then "taken.id" and "class.id" wouldn't result in a compilation error as it does now.
That code is copy-pasted by the way, from the proposal.
So what will this mean now? Will function composition require a space between the dot, or how will a “record dot” be disambiguated?
Both pages link to each other, but the discussion is unlikely to be of very much note unless one has already read the proposal being discussed.
1. https://github.com/ghc-proposals/ghc-proposals/blob/master/p...
Records were my number one gripe in the language that otherwise does so many advanced things so uniquely well.
The whitespace requirements, though, are going to lead to some confusing error messages for newbies who want to type myRecord . someField
(Hopefully britanny will get an update to handle this correctly!)
They could, In Theory, rename the composition operator selectively when this language extension is enabled. That'd mean Prelude has to change what operators it exports depending on what extensions are enabled, which would be horribly magical.
data Student = Student { name :: Text, school :: School }
we end up with two functions 'name' and 'school' in scope that extract the values of their respective fields: name :: Student -> Text
school :: Student -> School
This means it is common for field names to be composed with functions. I write code like this all the time: schools = map (schoolId . school) students
This is compounded by the fact that a lot of newtypes use record syntax to define functions in this style that are even named like functions: newtype Reader r a = Reader { runReader :: r -> a }
Importing runReader from a module, you might not even know that it's implemented as a record field! This is pretty handy since you can change the implementation of runReader from being a record field to a normal function without breaking most of your callers. (In fact, I think this might have happened with runReader being implemented in terms of runReaderT.)This kind of pattern means that people write lots of code that uses record fields as functions, sometimes without even realizing that they are using a record field!
The practical upshot is that myRecord . someField is used with . as function composition all the time, so any proposal that cares about backwards compatibility can't change the meaning of . in that context.
I even use it sometimes, like this:
local field = require "some/module" . field
When I want exactly one field from a module which returns a table. I think the spaces look nicer next to the string.
EDIT: I mean, lenses and RecordDotSyntax both provide a way to access nested fields in records, so the new syntax would take away some of the reasons to use lenses. But are there any other relations among the two, or do lenses and RecordDotSyntax just provide two different solutions to the same problem?
- Mutation of nested records,
- Iteration of collections,
- Folding of collections,
- Viewing discriminated cases that may or may not exist,
- Building a structure around a value, and so on and so forth.
[1] https://github.com/ghc-proposals/ghc-proposals/pull/282#issu...
I would say in my 5 years of production Haskell, I've seen that sort of thing deployed in production _maybe_ once, and I've run into it during development once or twice.
One time it was in integration testing, and a simple heap profile made it clear it was in a library dependency. And a quick look at the library source was enough to diagnose it. It was like an hour or two tops from "we have a space leak somewhere" to "here's a PR to a third-party library fixing the leak."
A bit of a problem when you first start using Haskell seriously is that the IO mechanisms built into the prelude are very slow once you get to even only a few MiB.
That's easily remedied by switching to eg ByteStrings, but it's still a bit annoying that what the language presents as the 'default' is such a toy implementation.
My only worry is . being function composition operator so it might not be crazy amazing for readability only you get
a . b . c . d.e
So maybe it should have been something like "->".
The problem is that "->" has an even more special meaning than "." in Haskell, since "->" separates the arguments of an unnamed function from its definition.
So, for example
addOne = \x -> x + 1
and addOne x = x + 1
are equivalent.This means that "->" is about as special as "=". Which leads me to think it would require some very substantial changes to the parser, and perhaps even defeat the purpose of increased readability. For example, how readable is the following?
getY = \x -> x -> y
(which reads as "getY = \x -> x.y" in the current proposal)(.a.b.c) . (.d.e.f) = (.d.e.f.a.b.c)
If you use -> both for the member function and for reverse function composition you'd get:
(->a->b->c) -> (->d->e->f) = (->a->b->c->d->e->f)
which is somehow nicer, especially because accessing a member and composing functions become the same thing when you view a value as the unique function sending the initial object to that value.
But unfortunately we've spent centuries composing functions the wrong way round, so now we're forced to deal with the consequences.
Given functions f, g , h,
h >>> g >>> f is equivalent to f . g . h
Its from Control.Category, so it generalizes to other things, but for functions specifically its left to right composition.
( theres also (<<<), which in this case is identical to (.) )
( Also present in Control.Arrow . Why? I dont know this topic well enough to explain. My understanding is limited to what these operators mean in the specific of functions. Functions(->) are a type as well, so they can be instances of typeclasses. for example
instance Functor ((->) r) where
fmap = (.)
) (<.) :: (b -> c) -> (a -> b) -> a -> c
and (.>) :: (a -> b) -> (b -> c) -> a -> c e{lbl = val}
Though this has been a part of haskell for a while it seems:> Note: e{lbl = val} is the syntax of a standard H98 record update
FSharp, OCaml and Elm have not got great solution here either:
{ e with lbl = value } // ocaml/fsharp
{ e | lbl = value }
Do any functional languages have anything as nice as: e.lbl = value new_e = e.lbl = value
at which point "nice" is not how I would describe it. e = e.lbl <- value
(I didn't reuse the '=' because two '=' like that looks weird.The main thing I want to address is that a.b.c.d is so much work to write to.
%{existing_map_or_struct | some_key_1: :some_val, some_key_2: :some_val}
Then there is also `put_in/2`, `put_in/3`, `update_in/2`, `update_in/3`, these are nice because they work in pipelines some_val
|> put_in([:foo, :bar, :baz], 3)
some_val
|> update_in([:foo, :bar, :baz], fn x -> x + 1 end)
The macro forms are cool too put_in(foo.bar.baz, 2)
update_in(foo[:some]["nested"][:path], & &1 + 1)
The cool thing about the `/2` forms of `put_in` and `update_in` is that they are macros they rewrite the ast to return the entire object, and not just the changed value. iex(1)> foo = %{bar: %{baz: 2}}
%{bar: %{baz: 2}}
iex(2)> update_in(foo.bar.baz, & &1 + 1)
%{bar: %{baz: 3}}
There's even more powerful macros like `get_and_update_in` which can use "selectors" to extend their functionality (https://hexdocs.pm/elixir/master/Kernel.html#get_and_update_...)https://hexdocs.pm/elixir/master/Kernel.html#put_in/2
https://hexdocs.pm/elixir/master/Kernel.html#put_in/3
let a = {a with b = { a.b with c = { a.b.c with d = 5 } } } } e & lbl .~ value
It's not an in-place update like `e.lbl = value`. a.b.c.d
but a write is: let a = {a with b = { a.b with c = { a.b.c with d = 5 } } } }
which is much much more verbose than the OO/imperative: a.b.c.d = 5syntax, and Ramda has
e2 = assoc(lbl, value, e)
which is curried and data-last, so you can keep that setter around for future instances of e. Usually not too bad to translate Ramda into your language of choice, and a valuable exercise. I did it with Python and it helped a TON
Once JS gets immutables, web devs will have more tools for FP, and we can look forward to that expanding the community of functional programmers a lot: (https://www.infoworld.com/article/3569118/ecma-proposal-woul...)
Python’s lack of an immutable map is annoying but there are nice pip packages for it
e{lbl = val, lbl2 = val2}I can't find a subscribe button in gutlab but I will set a reminder
The old classic languages just create so-called selectors in the global namespace, like defstruct macro does. This is not the best possible solution.
However, the . is used for function composition, so the better alternative could be the -> syntax, reflecting that in Haskell everything is a pointer.