103 points · by Tehnix · 7y ago · 140 comments
...
hoistStateIntoStateT
:: Sem (State s ': r) a
-> S.StateT s (Sem r) a
hoistStateIntoStateT (Sem m) = m $ \u ->
case decomp u of
Left x -> S.StateT $ \s ->
liftSem . fmap swap
. weave (s, ())
(\(s', m') -> fmap swap
$ S.runStateT m' s')
(Just . snd)
$ hoist hoistStateIntoStateT x
Right (Yo Get z _ y _) -> fmap (y . (<$ z)) $ S.get
Right (Yo (Put s) z _ y _) -> fmap (y . (<$ z)) $ S.put s
...I definitely need to use my brain to read that code though.
Haskell feels a lot like that. That code snippet gave me the same feeling of having to climb a conceptual mountain to appreciate the metaphorical view at the top. I have no doubt that there is something beautiful there, but of all of the conceptual, programming related mountains I need to climb, the language abstraction one is pretty low priority, because the language I am using really is not my limiting factor. I appreciate that others who care spend a lot of time thinking about this problem, but Haskell's got a long way to go before I'm convinced that it's worth using in everyday contexts.
I also find that code quite abstract, it looks to be solving some kind of problem that usually the language designer (if say you are in C#) would be solving. Haskell seems to be something that leaks it's mechanics into your code, where as C# keeps it for the most part nicely separated from your code, albeit with the effect of giving the programmer less power. No typeclasses etc.
"Gee, that’s complicated! I must be really smart to have
written such a function, right?
Wrong! I just have a trick!"
Seems like the author deliberately picked an example that looks hard but isn't once you understand what he wants to say.Nothing new under the sun. How about APL?
https://medium.com/@gordonguthrie/the-beam-needs-an-apl-y-la...
A professor of mine used to write commercial software in APL. (Including business apps!)
I take an old familiar tool and I instantly remember how to use it.
What gives?
When I was a boy, wise practitioners told me that being a programmer is a path of constant learning. They were right. And since then, I take learning as a natural part of the profession. I suspect it also has something to do with a high salary it commands.
Somewhere down closer to #15 is "learn Haskell," which comes after learning Rust/Go. Learning a "real" functional language would be much higher, but I already did that early in my career.
I only meant to notice that there's no impenetrable barrier to mastery of that, when desired. I don't think that to learn Haskell at a high level is harder than to do the same with C++. The latter just had more adoption for last 25 years, for a number of reasons.
In any case, some (cursory) knowledge of Haskell improved my Java, Python, and JavaScript quite a bit, and is directly applicable to Scala in many aspects.
- Harold Abelson
Looking at that code, it looks like it's working on a tree of some sort, but what kind? Something to do with Sem and State and Yo, I guess. At a wild guess, it's something to do with ASTs and DSLs or similar. But code that is dramatically inscrutable without metric shit tonnes of context, is bad code.
Really? I must be misunderstanding what you mean by "operation", or maybe this is the part of functional programming that hasn't clicked for me yet... my understanding is that the type signature tells you what kinds of things the function works with, but it doesn't tell you how the function maps the input to the output. When I hear "operation", I think of the latter. What are you referring to?
For example, if a function's type is `a -> b -> (a, b)`, there's really only one function fitting that shape: cons. What else could a (pure) function possibly do, with that typing, other than "exactly what cons does"?
hoistStateIntoStateT
:: Sem (State s ': r) a
-> S.StateT s (Sem r) a
?Sorry even that'll get my brain CPU on 100% for a few minutes.
I have never user Polysemy, any effects library, or type-level lists before, but I'm reasonably sure my guess is right, despite not understanding any of the code.
The reason is simple: as a developer looking at the code written a week ago by myself, using "normal" language, I'm able to somewhat recognize what's going on, after a month or two it's something produced by aliens. With code depicted above this time will shorten to what? hours? days? Additionally it will require additional effort for developer himself or anyone else to decrypt it after a while.
Now we currently use Java here. I worked with other programming languages in companies too, for instance C and C++ which were a nightmare with no equal. Don't get me wrong, when I was in college I just loved creating crazy shit with template metaprogramming and preprocessor metaprogramming. Once you mature out of it, this is just the most craziest thing you could ever do.
The reason is simple. Even plain, stupid Java is already incredibly hard to verify during a CodeReview. Spend 2 hours in a row trying to find problems with other's people well written Java code and your brain feels like it explodes. Now with C++ that same task is almost impossible and will likely require 8 hours of your day for the same code volume. With Haskell I would go out on a limb here and say it would require 24 hours of your daily time to handle the same code volume...
Not to mention that most programmers even at Google and Amazon would not even be able to either read or write such code and people like me, who could and did in the past would just shake their heads and move on. Nobody wants to deal with such code outside of university...
Hello, I am at Amazon. I spend a decent portion of my time working with an in-house Clojure library (not entirely dissimilar to Haskell) which handles some mission critical business logic my team owns.
It terrifies me. I hate it.
For every hour I spend working with it, I spend another hour trying to convince everyone we need to rewrite it in Java, immediately, or replace it with a different library. None of us have a clue how it works. We can't debug it. The guy who wrote it quit three years ago.
I mean, it works. It works very well. But the day it doesn't work, we're not going to have a clue why that is. And then we're in real trouble.
Bottom line: Pick the language most teams at your company are familiar with. At Amazon that simply is Java for anything that is not frontend, period. Even getting into Kotlin presents a major obstacle. Clojure? Yeah right.
Edit: Just to clarify. Working in a big company is not meant as a restriction. In fact, having worked in several startups before, these would do well to adopt the same principles for programming and prohibit this "university graduate" way of developing software. In the end, if your startup is going to survive, it's going to become a big company itself someday.
But see, someday, I won't be on this team anymore. And then all the time I spent all learning how this beast works will be wasted and some new person will be scratching their head at it.
That is far from accurate for more left-field languages. Should every new hire spend three months meditating and becoming one with the functional gods? Seems a bit silly, almost as silly as trying to hire a programmer that is a fit into the company, and knows the relevant domain AND programming language/ paradigm as dictated by someone long gone.
Clojure is actually very different from Haskell. The pervasive use of "dynamic" features, including the lack of a satisfactory type system, does mean that Clojure code can be expected to be inherently less reviewable, surveyable and auditable, which is exactly what you found.
The article was about "virtues of Haskell's strong type system" but you seem to have read it as "Why you should use Haskell and Type driven development in place of your existing C++ or Java Stack."
The author has published a computer science book. He probably doesn't work in a big company, but lots of people work in big companies and still appreciate FP / type theory / computational theory and forays into those realms.
Not to mention... A little bit of stuff like this and a lot of Rust makes more sense.
Yeah, look! I started with a function signature and a little _. Lo and behold, fifty seven holes later, I have a graph-coloring optimal register allocator for MIPS for my compiler back end. Don't ask me how it works, but the type system assures it.
Dear everyone, code like this is not where Haskell starts to get useful, it's useful much earlier. I won't reproduce it here but I just wrote a post with some practical code (and how haskell helped) taken from a project of mine: https://news.ycombinator.com/item?id=20260095.
If you are using Coyoneda in your code, please do not use it to extol the virtues of Haskell, except if you're sure your audience is expert haskellers/mathematicians/researchers/others who can actually gain something from the post, or if the writing is so sublime that it has reduced the complexity to near zero.
Please OP, you're actively hurting the adoption of Haskell by posting stuff like this in the wrong places/context.
The author of the article (Sandy Maguire) created a library called polysemy that improved on the efficiency of using a concept called Free Monads in Haskell -- their main benefit is that they let you write functions that deal only in your business domains. A rough corrolary would be a dependency-injected function that could not do anything outside of what the dependency injected pieces do, but also allows for different dependency implementations to be mocked out entirely within the languag (i.e. without the XML/annotation hell that is Spring DI magic).
There are plenty of submissions on HN that are aimed at specialists of a field. They get to the front page because people are curious about specialized fields. So, even though I disagree from the pits of my soul with the content of the submission (as a professional Haskell developer, I should add), I think you're being very unfair to say that stuff like this shouldn't be shared with a broader audience.
- You're absolutely right about specialist-aimed submissions on HN, it's a large part of the reason I keep coming back to HN, I should have thought about that a bit more. I think this post was a little too far into the very deep end of what haskell can do.
- Stoking curiosity about Haskell is a wanted/welcome result, and I think part of my annoyance was that this article was very unlikely to do that, in this context.
- I agree with the fact that deeply technical posts like this should be shared with a broader audience -- I didn't mean to imply that all posts shouldn't be shared, but the audience needs to be taken into account a little more. r/haskell is very different audience from HN.
While we're here, I'd love to get your opinion on polysemy -- it looks like the best thing since sliced bread for free(r) monads, the combination of the usability of freer-simple and the efficiency gains of fused-effects.
I've long secretly held the believe that mtl-style and free-monads were roughly equivalent in power and efficiency equality was only a matter of time, but I am really looking to the free approach because effect algebras seem to be a fantastic tool -- they compose more cleanly than mtl does.
> While we're here, I'd love to get your opinion on polysemy -- it looks like the best thing since sliced bread for free(r) monads, the combination of the usability of freer-simple and the efficiency gains of fused-effects. > I've long secretly held the believe that mtl-style and free-monads were roughly equivalent in power and efficiency equality was only a matter of time, but I am really looking to the free approach because effect algebras seem to be a fantastic tool -- they compose more cleanly than mtl does.
I've not tried polysemy and I don't think I will unless it comes up on the job. I'm inured to effect systems after the fifth or sixth flavor. I've heard it's nice from people who've wanted to use it though. I've written my share of production Haskell and I haven't found mtl to be as bad about composition as people tell me it is.
Now, as someone who evaluates resumes and interviews people, the only Haskell specific thing I care about is making sure the candidate knows how to use the core of the language, which is basically Haskell2010 + some indispensable extensions. I expect fancier stuff to require training, and to be honest it's sometimes better to properly contextualize that stuff for someone instead of having them come in already thinking they know how to use the footguns :^). My company asks candidates to attach or link code samples if they have them. If they don't, we ask them to write a small Reflex [0] app, because we're primarily a Reflex shop.
Now, every shop is going to be different and expect different things from their candidates. That's one reason why getting involved in the community is so useful. If you hear directly from people who employ Haskellers in your area what they want, then you can build your credentials for that. Besides that, you just gotta apply. If you're a strong candidate in another tech stack, you're a strong candidate for a Haskell stack too if you can demonstrate you can use the language. There's currently a remote job posting on r/haskell if you want to give it a shot. I wish you the best of luck :)
- https://wiki.haskell.org/Jobs
- https://angel.co/haskell/jobs (if you're into startups)
- r/haskell on reddit
I do not work in haskell at my day job but I've found writing side projects with it has done wonders for my understanding -- I've been able to work through the trends in how people are being productive with haskell (mtl, readert, free monads, vinyl, datakinds shenanigans), just by trying to make stuff with haskell.
If someone's put off learning Haskell because they are (terrifyingly!) exposed to a single obviously non-beginner's article (on HN not the Daily Mail!) -- well, they must be too incurious to be worth recruiting to the language anyway.
I know nothing about Haskell (and am unlikely ever to learn it for unrelated reasons), but I read the article, understood little of it, and survived with my interest if anything somewhat piqued.
Great things happen when barriers to entry are low, why would you want to make the barriers seem higher (despite the fact they didn't actually change)? You're going to scare off someone who could have contributed a simple driver/client/whatever (the kind of libraries that make thriving ecosystems) that doesn't require any of this type tomfoolery (I use that term jokingly not pejoratively), because they wrote off haskell early.
I don't know about 'using'. I think it just is one - learning is possible but terribly inefficient in its absence.
There's also nothing to suggest that someone who isn't curious today can't become more curious as time goes on
Very true & thanks for reminding me. That we can & do change (and must leave space for each other to) is one of the drums I like to bang now and then.
Is Haskell a language or a cult?
> Sorry but this is just crazy. I always wonder if people writing blog posts like that have ever worked in a big company. You need to spend at least 2-3 hours a day doing code reviews and most of the rest you are writing code, which subsequently involves reading the code you wrote yourself and others have written.
> Now we currently use Java here. I worked with other programming languages in companies too, for instance C and C++ which were a nightmare with no equal. Don't get me wrong, when I was in college I just loved creating crazy shit with template metaprogramming and preprocessor metaprogramming. Once you mature out of it, this is just the most craziest thing you could ever do.
These impressions of Haskell are wrong Haskell is not crazy, it's actually insanely reasonable -- so reasonable in fact that it will not let you compile code that is crazy.
This is the message getting lost in crazy type signatures (which admittedly often do cool/useful things if you undertsand them) and overly verbose/complex examples.
> Hello, I am at Amazon. I spend a decent portion of my time working with an in-house Clojure library (not entirely dissimilar to Haskell) which handles some mission critical business logic my team owns.
> It terrifies me. I hate it.
I do not use/encourage use of Clojure any more, and this comparison scares me -- Haskell is not clojure, but it's getting lumped together and this really worries me because it should not be.
Haskell helps you write clear code -- type signatures, pure functions, immutable values, typeclasses, algebraic data types are all tools to make your code clearer and more obviously correct. If this isn't getting across haskell is in trouble/has a big marketing problem.
I also remember reading your comment the other day, and I thought it was great too.
Personally, I clicked through expecting to find out about some new way haskell can help me use my types to verify how implementations work (the title is fantastic in this sense, and I saw the domain), but I was instantly hit with that bit of code that's somewhat inscrutable and doesn't do enough to set up the context of what's happening and why. Then the general point of the article basically was to use polymorphism and type holes to work your way to what you need. I felt like this wasn't what I signed up for, and wondered what others thought, only to go back and see upvoted comments bashing haskell based on this one example.
This is what lead me to make my comment -- I think this article is too advanced to post here without some really good prose/writing. Not blaming sandy, as of course he doesn't control how his content spreads but I thought it just wasn't right for this context and sharing something that makes haskell seem harder than need be is damaging IMO.
The case that "haskell will tell you what types it expects" is so much easier to make without the monstrous signatures in that post, or without type level computation (the ':).
I think what's really hurting the adoption of Haskell is Haskell. This blog post represents Haskell quite well. Haskell will never be "popular", just like category theory will never be "popular".
It happens with small "misunderstood" groups. People in such groups start doing things to purposely set themselves apart and distinguish themselves from the outside majority. It seems partly subconscious or instinctual, though it's also expressed as conscious actions and can involve quite a bit of thought and rationalization.
In particular, it happens with programming communities.
> This post is not for beginners/non-haskellers, I don't understand why it's being posted here.
Plenty of interesting topics related to specific languages get posted here all the time, I am under no assumption that 100% of HN is haskellers, but there is still enough that people find it worthwhile to share and upvote on here.
> Is this intentional or are people in the haskell community this tone deaf?
Why would you blame the community on this? If anything, the need to create rifts with comments like yours, is something the community deeply needs to move on from.
The reason for the posting is that it’s an interesting exploration of type holes and their usefulness. As you may have noticed several places in this thread, the concept that the more polymorphic the type, the more restricted you actually are, seems to have peaked interest and opened some eyes to new concepts.
> It's like we're determined to prevent new people from joining.
Not at all. What people find interesting is very subjective, and one of the things I sorely miss from daily working in other languages, is being able to work with the compiler as a pal that helps you out. This post demonstrates one way to make Haskell work for you, and if you’ve honestly read the post, Sandy does a good job of explaining the concept on simpler functions.
> If you are using Coyoneda in your code, please do not use it to extol the virtues of Haskell, except if you're sure your audience is expert haskellers/mathematicians/researchers/others who can actually gain something from the post, or if the writing is so sublime that it has reduced the complexity to near zero.
Sorry, but I’m starting to feel like you didn’t read the article, but maybe just the comments here. At no point did the post care about Coyoneda. It started out with demonstrating how a complex piece of code could be reached with the method (type holes), and then went on to demonstrate the method on simpler code, removing the need to know anything else. If you focused on understanding the first piece of code, then you’ve completely missed the authors intent.
> Please OP, you're actively hurting the adoption of Haskell by posting stuff like this in the wrong places/context.
That’s a fair opinion to have, but not one I can agree with at all. With a wealth of languages to pick from, why should anyone pick Haskell? Some of what drives people’s interest are these demonstrations of what is possible, when you have a more expressive type system. That are tons of other reasons too, but as a “selling point”, it’s not “look how simple Haskell code can look”, because that is sure as sh*t like lying people straight up in their face—that’s not where Haskell differentiates itself.
I’ll close out with adding that Polysemy is very cool, and I’m personally looking forward to seeing it ready for usage :)
https://www.reddit.com/r/haskell/comments/c5dfuw/implement_w...
Surely this is the difference of (more) appropriate context & audience.
Maybe this is just a case of the loudest people on HN being the most disapproving of haskell somehow, but just about all the top level comments except for mine and the bottom one are critical of haskell because of the perceived complexity of the code that was posted.
Blah blah ...15 meters per second... blah blah ...after 10 minutes... blah blah ...how far... blah blah => aha! I probably want to multiply 15 by (10 × 60).
Does this actually work out for Haskellers? I mean, I guess I'll believe you if you tell me it does, Haskell is different enough from the kind of programming I do that I don't think I know anything at all about it.
Okay, this may be (probably is) a stupid question, because I know no Haskell whatsoever, but... if there's really only one thing it can do given the types, why does the code have to be there at all? Why can't you just write "Do the only thing you can do given these types"? If it's code humans aren't meant to read anyway, and it really is the only thing that could be done... why can't the compiler just do it without you having to put the non-human-intelligible code in your source files?
Technically there is more than one possible implementation, but you would also have to go out of your way to get it wrong. Automatically determining what constitutes "going out of your way" so that the code can be generated automatically is an active area of research (program synthesis), but as this post already shows, for "simple" cases, we're getting pretty close.
As the post mentions, there are certain dead giveaways of an incorrect implementation, such as unused variables. Conversely one may still have some rough idea of the desired code to guide the implementation. The point is not about entirely removing one's mind from the process, but to allow oneself to only think about the choices that matter, which are few.
Alternatively, another way to look at the problem is that these types, while already being quite precise, are still not as precise as they could be, because the type system is not sufficiently expressive. And even with the ability to describe the desired properties to uniquely determine the implementation:
- it may not be obvious that the implementation is in fact uniquely determined;
- a solution, unique or not, may not be easy to find automatically. Program synthesis is essentially the same problem as proof search (c.f. Curry-Howard correspondence); knowing that there is a proof of Fermat's last theorem is not sufficient to construct an actual proof of it from scratch.
For these reasons, it may still be desirable to nail down the implementation explicitly even if it is hard to read.
It's also worth considering the fact that polysemy (the package that the controversial snippet at the beginning of the post comes from) is very much an implementation of a state-of-the-art effect system using state-of-the-art features of Haskell's type system, so it can be expected that the abstractions to make this code more digestible (for the right audience) are still missing, because no one has ever thought of how to express them yet.
What if the code you wrote gave you guarantees about the things that it did, as long as you trust the compiler/proof-checker/other tools that used to verify the correctness of the code?
This is basically what testing does-it gives you proof. As long as the things you test are "pure" or functional in the sense that they will do the same thing every time, given that they are given the same inputs, you have proof that the code is correct.
The problem with testing is that you can't test everything because of combinatorial explosion. However, there's a trick to this where you can collapse states together and prove something using that collapsed state. For example, you collapse all 32-bit positive integers {1,2,3,...} into the state {N} and now as long as you prove something that holds for {N} you also proved something that holds for all 2^31-1 positive integers. You just have to be very careful and very precise about what you're doing, and that's where your compiler comes in to help.
(So this next part is kinda butchering the math behind it and all sorts of programming language theory, apologies in advance.) You can kind of think of the state {N} as a type [2], and {1,2,3,...} as instantiations of that type. You can do something like "prove" that if you're given {f(a,b) => a+b} and {a=1, b=2} then you can conclude {f(1,2) == 3}, and throw that under some test. The test instantiation where you run 'assertEquals(f(1,2), 3)' is basically a concrete instance of your proof that 'f' does what it should. You just have to trust that your runtime environment is behaving correctly (no bugs).
You can also consider functions that can be defined in your language as types (e.g. f: int -> int, a function that takes an int and gives you an int). That's the 'function signature' or 'function declaration'--it's type. And you can consider instantiations of this type to be the same thing as a function definition --where you define what the function does. As long as your function definition doesn't throw any compiler errors (that is, it passes your type-checker and there are no bugs in your toolchain), then you have provided an instance of a proof that the function definition is of the type of the function declaration. This might not sound like much ("great, the function definition has a certain type...didn't we already know that?"), but as long as your function definitions are pure functions (no external state change), you've just guaranteed type safety in your program [3]. Your program will never crash because you input a string when you should've input an int (hello JavaScript...). The type-checker won't allow you to write such a program.
You can get a lot more guarantees than what I just mentioned, and there is a ton of active research in this area [4].
So to answer your first question, yes, they are suggesting writing code that you don't necessarily understand because you "offload" parts of your understanding to the compiler (type-checker). It's like when you do an automated refactor--if your code and the auto-refactoring tool are written correctly you can just trust that it did the right thing and doesn't cause any bugs.
As for your second question, I can't answer it because I haven't used Haskell.
[1] https://en.wikipedia.org/wiki/Operational_semantics
[2] https://en.wikipedia.org/wiki/Type_theory#Basic_concepts
And then it starts with "Let's go through an example together. Consider the random type signature that I just made up:"
Hmm...I never have a type signature as a starting point, random or not. I have some sort of requirement, some sort of functionality I want out of the code. Soft fuzzy requirements.
And the types rarely if ever tell me what the function does. Heck, a function that's int x int -> int could be just about anything. How about string -> int? Does it convert the string to an integer or count the vowels?
But mostly I like Haskell's type system for the high degree of confidence it gives me while refactoring. Type Tetris is a side benefit on that, IMO.
That's because using types like "int" and "string" is not a real, valid use of a good type system. Instead, you'd typically have a function type of `newton -> sqMeter -> pascal`, or `username -> accessLevel`. All these types would be implemented through basic int and string types, but declaring them explicitly with very limited conversions inbetween would actually use type system to verify correctness of your code.
Arithmetic of adding up kilograms and kilograms together is automatically inherited when you declare a derived type.
Types are great imo for checking against primitives because the semantics of int string array etc are strong but most languages are encouraging wrapping up primitives in some user defined wrapper then the semantics of the wrapper are implicit, brittle and known only to the author
The semantics of your Bob class is more likely to change as it's used over many different contexts than the int type is, if the int type is being swapped out it's usually because the wrong data is being used, which is what we are using types to protect against, it's not usually because there's a problem with what int is
Would refinement types help in that respect? For example, the F* language. https://www.fstar-lang.org/
I don't have any experience with "business" code, but my naive understanding is that application-level code is typically very monomorphic which is exactly where refinement types are useful, whereas the OP leverages polymorphism to a large extent (not necessarily), and that works well for general-purpose libraries which don't and must not care about their users' data.
I find it slightly silly because one still has to read such functions so it is still hard to know that they do the right thing. For example, consider the function in the article which has type:
Sem (State s ': r) a
-> S.StateT s (Sem r) a
It seems pretty obvious what this morally should do.In some cases it is possible for polymorphism to force a value of a certain type to always behave a certain way. E.g. a value of type [a] must be the empty list because that is the only value with that type. On the other hand there are multiple values of type [[a]] (I.e. an empty list or an infinite list of empty lists or anything in between). A more complicated example is that the only thing a function with the signature of compose ((b -> c) -> (a -> b) -> (a -> c)) can do is compose functions whereas a function of type (Int-> Int) -> (Int-> Int) -> (Int -> Int) can do just about anything. Outside of abstract Haskell libraries, code tends to look more like the second case than the first.
In this case it is hard to be sure that the function must behave in the correct way because of its type. I think it hinges on whether it is possible for Put to change the type of the state. If it can then types force you to always take the newly put value and return it with Get, otherwise it would be possible for Put to eg only sometimes work. So if one has to check that the code is correct, one must either read very carefully and check the code, or one must very carefully read the type signature and deduce that the code is correct.
In the first case the fact that the compiler made the code easy to write doesn’t really help much. Perl5 taught us that just because a program was easy to write it does not mean it will be easy to read/check.
In the second case, why do we have a program at all? If there is only one correct program we could have written then it seems to me that the program is in fact the type and the compiler ought to have been more clever and written it itself. (There are of course plenty of issues with that statement).
It seems to me that claiming “Haskell is great because it helps automatically write difficult functions which are impossible to check” or “Haskell is great because it makes me do the manual steps in writing a function that can only ever do one thing and mixes that pointless code in with the thing I care about” is a bit silly.
This all being said, I do think holes are a useful feature but I don’t think they are useful for entirely writing a function for you. They are particularly useful in proof assistants like Agda where one must manually exhibit a million trivial propositions and holes help fill in all the boring gaps. Agda doesn’t really suffer from the “multiple things a type could mean” issue.
I would argue that in this case it is quite easy to ensure that it does the right thing, because even if the implementation is not unique, the number of possibilities is extremely limited. In the case of `Put` you can return either the new state or the old one. It takes a single unit test to ensure it's putting the new one, with parametricity to generalize from one case to all cases, still without looking at the implementation.
> In the second case, why do we have a program at all? If there is only one correct program we could have written then it seems to me that the program is in fact the type and the compiler ought to have been more clever and written it itself. (There are of course plenty of issues with that statement).
Program synthesis being basically proof search, even after the right automation is developed it may still be more practical to write the program by hand.
> They are particularly useful in proof assistants like Agda where one must manually exhibit a million trivial propositions and holes help fill in all the boring gaps.
I think the same mechanisms and benefits are at play here in a non-dependently-typed setting, even considering "entirely writing a function for you" as an unwarranted exaggeration.
Does such a thing exist?
For example, for this post's "jonk" example I just had to copy the type into emacs, reformat it a bit into Agda syntax, then press C-c C-a and it automatically figured out the solution that the author worked through manually: λ z z₁ z₂ → z₁ (λ z₃ → z₂ (z z₃))
See the full list of commands at https://agda.readthedocs.io/en/v2.5.2/tools/emacs-mode.html
Snake Oil.
Apply this claim to any set of real world functions, however well designed they are.
This is classic Haskellism. Obsessed with the lattice of types in the room. Door closed. Real world outside.
Otherwise you hope that someone has written in a doc somewhere that bar is a string. And judging by npm docs I've seen - that is very unlikely. So you debug at runtime, head over to the github repo, or do an console.log(typeof(... hmm!
My experience with TS has been great until I need to deal with data that comes from an external system (API, client, database). Then I either have to cast the type (which can lead to bugs where compile and run time types don't match and IMO isn't an option server-side) or I have to effectively duplicate my types by writing type guard functions.
>Change a front-end React prop, then follow static type errors through the statically typed API, into the backend, all the way down into the database where they force a change in the Postgres schema. The system won't even compile unless React can talk all the way down to Postgres.
https://twitter.com/garybernhardt/status/1140695491685933060
That said, even if I have to write type guards at the edges (which I'd do in JS anyway, just because you aren't using types doesn't mean you don't need to enforce schemas), I think being able to rely on compiler typechecking inside the codebase boundary is really nice for making module interactions more predictable.
There are some really great things about types as documentation, though:
1) It's machine-checked, so it's probably not out of date or mistaken, which can be worse than no documentation.
2) The interaction with my code is checked, so if I misunderstood the compiler often has my back.
3) With inference, I can often ask for docs for code I just wrote.
None of this is to say that there aren't usually other forms of documentation that should be produced.
(a -> b -> c) -> b -> a -> c