6 ms·
> You need a PhD in type theory to get anywhere. That is extremely false. Haskell isn't even a good playground for academic type theory -- you'd want Agda etc.
by mrkgnao 9y ago
> You need a PhD in type theory to get anywhere.
That is extremely false. Haskell isn't even a good playground for academic type theory -- you'd want Agda etc. for that. The development of the language over the last few years has been characterized by pragmatism and a focus on backwards-compatibility, which is why you can take code from something like ten years ago and have it run without issues on modern versions of the Haskell compiler with little to no modifications. (Let's not talk about how long code written in "modern" JS lasts.)
And I'd really like to see type class constraint resolution with functional dependencies, or Hindley-Milner type checking, or something of that sort implemented in "5-10 lines" of JS.
"There he goes again with his mumbo-jumbo," you say. That's right, you don't need to care about those things to write Haskell. What you meant is implementations of typeclasses ("interfaces") like Monad, Functor, and so on: they don't take much more code in Haskell.
Array.prototype.chain = function (f) {
return this.reduce((acc, it) => acc.concat(f(it)), [])
}
instance Monad [] where
xs >>= f = concat (map f xs)
return = pure
And we didn't even have to go the "this" route! Notice that your 5 - 10 lines of JS don't let you write code that works in any monad, whereas I can easily write
whenM :: Monad m => m Bool -> m () -> m ()
whenM cond action = do
condition <- cond
if cond then action else pure ()
In Elm, you'd have List.whenM, Array.whenM, Maybe.whenM, ... or a straight-up false type signature like their Eq ones, and in JS, a bunch of prototype methods with no unifying threads.
--
As for an example of why I think Haskell has the right ideas (few of us will say it's the "best language evar"):
I'd really like to see a JS version of the Servant library, which takes an API spec for a server and actually generates a fully functional server from that. Here's a description:
https://news.ycombinator.com/item?id=14149200 https://news.ycombinator.com/item?id=14149200
Does this strike you as idle theoretical self-enjoyment?
- dmitriid 9y ago> This is extremely false [referring to "You need a PhD in type theory to get anywhere."] almost immediately followed by > And I'd really like to see type class constraint resolution, or Hindley-Milner type checking You don't even see the irony in that, do you? > they don't take much more code in Haskell: riiight. I won't even go into the number of things that need to be explained there before you even start explaining what the code does.
- dmitriid 9y ago> Does this strike you as idle theoretical self-enjoyment? It does. How many PhDs does one require to understand/correct/debug all the :> and :<|> etc.?
- mrkgnao 9y agoSpeaking from experience, zero. > debug But the whole point of a good compiler is that it tells you when you're wrong! (Instead of having to write hundreds of tests (and thousands of Node test runners).) > :> and :<|> You can just treat them as syntax, like the largest proportion of every other language, but with the opportunity of actually being able to write things like that yourself later. Observe: type API = "polls" :> Get '[JSON] [Poll] :<|> "polls" :> Capture "question_id" Int :> Get '[JSON] Poll :<|> "polls" :> Capture "question_id" Int :> "results" :> Get '[JSON] PollResults The :> operator separates parts of a path, and the :<|> separates different URL patterns. This is the equivalent of this API from the Django documentation: urlpatterns = [ # ex: /polls/ url(r'^$', views.index, name='index'), # ex: /polls/5/ url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'), # ex: /polls/5/results/ url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results') ] The only difference is it has less regexes in it, is capable of being checked for nonsense by a compiler much smarter than me, and gives you the aforementioned "server for free". I have had URL pattern-match errors with Django in the past, and having your compiler check that there aren't any is excellent. Easier to maintain? Check. Easier to read? Check. (If nothing, because of the lack of regexes.) Defines the response type too? Check. Easy to refactor? Check! Tired of typing "polls" at the beginning? Just lift it out: turn type API = "polls" :> Get '[JSON] [Poll] :<|> "polls" :> Capture "question_id" Int :> Get '[JSON] Poll :<|> "polls" :> Capture "question_id" Int :> "results" :> Get '[JSON] PollResults into type API = "polls" :> ( Get '[JSON] [Poll] :<|> Capture "question_id" Int :> Get '[JSON] Poll :<|> Capture "question_id" Int :> "results" :> Get '[JSON] PollResults ) Types are first-class :) > How many PhDs does one require to understand/correct/debug all the :> and :<|> etc.? Definitely less than it takes to become comfortable with the quirks of literally everything in JS: perhaps you should give something an honest shot before telling people who have derived real-world benefits from using it in production that it's useless?
- dmitriid 9y agoCommenting on the edited comment :) > Notice that your 5 - 10 lines of JS don't let you write code that works in any monad, whereas I can easily write Maybe, maybe not. Depends on your requirements, really. The core language might never get this, but these 5-10 lines of code do some very important things: - they explain monads faster and clearer than any of the countless monad tutorials that exist for Haskell - they demystify monads and show that: hey, you've probably been writing monads all along (and re-implemented them yourself countless of times, no doubt) - they (by necessity) dumb down the jargon-heavy lingo for easy consumption by average Joes like me :) Edit: that page in particular has also shown me that I have used easily half of Haskell's things (functors of all flavors, monads, comonads, etc. etc. etc.) countless times over the years in Javascript and Erlang. I didn't even know I did, because no one scared me off with the theory, and strange explanations and names :)
- mrkgnao 9y agoSure! I don't disagree: Haskell learning materials are a far cry from adequate, and we definitely need to learn from, e.g. the Rust/Elixir/Elm communities here. For now, this is worth trying: http://haskellbook.com/ http://haskellbook.com/ Also, #haskell on IRC has been, without a doubt, one of the friendliest learning environments I've ever seen. Drop by sometime if the mood strikes you. :)
- willtim 9y agoThis is not really a definition of a Monad. For example, there's no mention of the Monad laws. Monads are a very general and powerful abstraction that are not adequately described by your example. My advice to anyone is to read Phil Wadler's seminal paper, it is very easy to read.
- tel 9y agoIs it fair to say that your argument here is that "this resource was extremely valuable to me for understanding certain concepts in ways that Haskell-oriented resources in the past have not been"? I think that's a totally fair criticism. I also believe that the Haskell resources can provide further value to you (and others in your position) over time if you choose to study them. Similarly, studying category theory or type theory or logic could. Are these practical things to do? It depends upon your goals.