Functional Programming in OCaml
cs.cornell.edu
cs.cornell.edu
It says, in the 'Future Work' section:
> This could be extended to allowing multiple names to be initialized (such as through destructuring) with a "normal" conditional to be written after the assignment with a ; (similar to a for).
if (let x = 1, y = 2; x || y) {
/* ... */
}
if (let {x, y} = data; x && y) {
/* ... */
}
Meanwhile, OCaml from day one: if let x = true in let y = true in x || y then
...
if let { x; y } = data in x && y then
...I think this assertion was true 20-25 years ago. Less so now.
Not to forget that when languages pile on features over the course of years, they effectively split their users into 'old-style' and 'new-style' codebases, where people need to worry about what features are usable with what versions of the language. It's a huge mess.
For some people Word 1.0 for MS-DOS is all they need, whereas others won't do without the latest set of Office 365 features, same applies to programming languages, as per application architecture, software design tooling and deployment scenarios.
* GADTs
* Row polymorphism with inference
* First-class modules
* Let binding operators, e.g., let* for monadic binding
On the coming soon to OCaml (hopefully) and way ahead front:
* Algebraic effects
* Modular implicits
* Row polymorphism: statically type-checked duck types, for lack of a better description. In dynamic languages like python or javascript, it's common to pass dict or dict-like objects to functions and have them extract the key/value pairs that they need. Row polymorphism lets you have your cake and eat it too by enforcing enough structure to ensure that functions only receive records containing all relevant keys — without requiring that functions take a specific concrete type.
* First-class modules: another tool for type-level programming and modularity. I won't say much since it's hard to understand why you want and at times need them until you've organically come across a use-case for them. Once you do, you'll wish that other languages had them. Here's a resource, though: [4].
* Let binding operators: these let you define your semantics for binding values to variables. A common use-case is defining let* as the monadic bind operator for some type: let (let*) x f = bind x f, after which writing monadic functions is way more pleasant syntax-wise. In contrast to the other items on this list, this is pure syntactic sugar but incredibly impactful [5].
* Algebraic effects: the conceptual root of concepts such as generators (yield), async/await, and checked assumptions. Informally, they're (among other things) a way to encapsulate computation and jump around code like a goto statement, but in a more structured and type/memory-safe way [6].
* Modular implicits: these allow (among other things) ad-hoc polymorphism, meaning that the same operator, e.g. (+), can operate on both integers and floats. That's the oft-cited use-case, and it sounds pedestrian, seeing as most mainstream languages with weaker type systems have it. There's more to it than that [7]. Haskell and Scala have type classes and implicits as mechanisms for ad-hoc polymorphism, but modular implicits (if implemented) seem to strike a nice balance between readability, flexibility, and transparency.
[1]: https://blog.mads-hartmann.com/ocaml/2015/01/05/gadt-ocaml.h...
[2]: https://sketch.sh/s/yH0MJiujNSiofDWOU85loX/
[3]: https://stackoverflow.com/questions/48092739/what-are-row-ty...
[4]: https://dev.realworldocaml.org/first-class-modules.html
[5]: https://jobjo.github.io/2019/04/24/ocaml-has-some-new-shiny-...
[6]: https://overreacted.io/algebraic-effects-for-the-rest-of-us/
Also fork() doesn't work consistenly across POSIX implementations regarding threads and signals.
Functional Programming in OCaml - https://news.ycombinator.com/item?id=22408664 - Feb 2020 (58 comments)
Functional Programming in OCaml - https://news.ycombinator.com/item?id=22400233 - Feb 2020 (1 comment)
Functional Programming in OCaml - https://news.ycombinator.com/item?id=19292067 - March 2019 (144 comments)
He is a pretty good communicator!
https://www.youtube.com/playlist?list=PLre5AT9JnKShBOPeuiD9b...
Perhaps give a reference to "libraries" on the first page (or all?) where you use Core so people know how to use Core (opam, dune).
A great help here is a "start-project-template" like with svelte (for example), they have RIGHT on the home-page a big-ass block that shows you how to "get and start" the svelte-new-project-template.
Coming to OCAML the "tooling" can be confusing and ambiguous. For example you can use ocamlc, dune,ocamlbuilder,esy opam etc.
And of course the "few options" for a std lib Core, Batteries etc.
I like the "syntax" from the Batteries more to be honest.
Here's an implementation for those interested: https://github.com/senhorsolar/hack-assembler
However, I thought I should mention that I got _exactly_ as far as [0] (found while trying to google a solution) before giving up. The REPL is great, but if I can't compile easily, it doesn't inspire the confidence needed to go any further.
The community actually standardized its way of compiling around Dune which is very good.
[1] https://ocaml.org/learn/tutorials/a_first_hour_with_ocaml.ht...
[1] https://ocaml.org/learn/tutorials/a_first_hour_with_ocaml.ht...
In my defense, [1] makes no reference to dune, which I am excited to try out.
[1] https://ocaml.org/learn/tutorials/a_first_hour_with_ocaml.ht...
> You may follow along with this tutorial with just a basic OCaml installation, as described in Up and Running.
So, you can't really skip 'Up and Running'. And, the Graphics module usage is in the section 'A module from OPAM', which explicitly talks about installing the package and then loading it in the REPL ('top level').
It is co authored by Yaron Minsky, I think Yaron plays a big role in making OCaml relevant
I think OCaml would have been a far less popular language, had Yaron chosen another language for jane street
once you grok functors and algebraic type systems, a lot of other system design patterns seem like abstractions on a few key concepts.
I would like to see a course or textbook that explains functional programming in the abstract. Instead of going into the details of any one functional language, gives examples of how various functional languages implement the various core ideas of functional programming.
So, explain functional programming concepts such as pure functions, referential transparency, functors, monoids, monads, effects, lazy evaluation and so on, not in the context of any specific language, but giving examples from multiple functional languages.
In other words, focus on functional programming in general, instead of one specific implementation of it. I have been looking for such a textbook or course, but it doesn't exist.
These three things are mostly Haskell-specific though, at least in relation to functional programming.
https://docs.rs/functional/0.0.5/functional/trait.Functor.ht...
https://docs.rs/functional/0.0.5/functional/trait.Monoid.htm...
https://docs.rs/functional/0.0.5/functional/trait.Monad.html
The only real requirement of functional programming is the support of first class functions, the possibility to pass and return functions like any other values.
If you want to discuss your own interpretation of what makes a language more or less functional, which could be interesting, please provide a definition first.
IMHO, a functional programming language requires:
- First-class functions and function literal (lambda) syntax support
- Expression-oriented syntax
- Emphasis on immutable data structures
- Tail call optimization
Other features are 'take-it-or-leave-it'.
There is no emphasis when mutability is one mut and ref cell away.
'Emphasis' just means the 'paved path', i.e. default data structures like records and variants are immutable, default list type is immutable. Of course if you want mutable stuff it's there, but it's not the default.
By that definition C language supports functional programming. I think a lot of people would disagree with your definition.
https://www.semanticscholar.org/paper/Functional-C-Hartel-Mu...
It uses SML, had fun learning this stuff on the train with termux and vim.
C++ had support for partial applications with templates (std::bind iirc), and it was possible to implement closures as objects with the () operator implemented. Now it has support for closure with a dedicated syntax.
It would be possible to do something similar in C - a closure is just a function pointer and a bunch of parameters bundled together, but I don't think there's enough flexibility in the language to make it look like regular function calls.
Having to use monads for sequencing is something that happens in Haskell and not in Rust.
* it's a bit more verbose.
* you can accidentally implement a weaker interface than the one you'd want, e.g. by only implementing Functor and not Traversable for your data structure.
* the flip side of the benefits is that adding I/O to a previously pure function changes its interface.
I don't know how it'd compare to OCaml's algebraic effects proposal, though.
[0]: https://reactjs.org/docs/hooks-effect.html
[1]: http://joeduffyblog.com/2010/01/03/a-brief-retrospective-on-...
Lots of developers are curious about functional programming. Even if you don't program in a functional programming language, many principles of functional programming, such as pure functions, can be applied in any language. Almost all languages in use today, including JavaScript, C#, Java, Python and Rust have varying degrees of FP features. Understanding what FP is all about is helpful in learning those features.
I'd argue that the market for learning what FP is all about, and how you can apply FP features in traditional languages, is bigger than the market for learning specific FP languages such as OCaml, Scala, F# and so on.
When teaching, it's much better to teach as little as possible at a time, so that learners can absorb concepts easily through their working memory and then build on their knowledge over time.
Hence why I really favour CS books that rather pick a pseudo-code language, than trying to sell language X as part of exercises.
https://www.lihaoyi.com/post/WhatsFunctionalProgrammingAllAb...
(edit) a post that uses JS, PHP, Python etc. to explain many FP concepts: http://chriswarbo.net/blog/2015-01-18-learning_functional_pr...
It is a very bold statement. There is nothing in course about how to fight complexity. Knowledge how to traverse an ADT tree helps a little unfortunately. But it is a great classic CS course though.
You should read it more carefully. E.g. https://www.cs.cornell.edu/courses/cs3110/2020sp/textbook/mo...
> One key solution to managing complexity of large software is modular programming: the code is composed of many different code modules that are developed separately. This allows different developers to take on discrete pieces of the system and design and implement them without having to understand all the rest. But to build large programs out of modules effectively, we need to be able to write modules that we can convince ourselves are correct in isolation from the rest of the program. Rather than have to think about every other part of the program when developing a code module, we need to be able to use local reasoning: that is, reasoning about just the module and the contract it needs to satisfy with respect to the rest of the program. If everyone has done their job, separately developed code modules can be plugged together to form a working program without every developer needing to understand everything done by every other developer in the team. This is the key idea of modular programming.
Imagine you are a UFC fighter and going to the gym and lift weights to be a "better fighter" even though, you didn't throw one-kick or punch one bag etc :P
In terms of performance, there is this paper https://kcsrk.info/papers/system_effects_feb_18.pdf where on a single core async OCaml and effect OCaml are close to Go's net/http, and there is also this project https://github.com/ocaml-multicore/retro-httpaf-bench but I haven't see any results from it.
- Bootstrap teaches a toned-down version of Racket (i.e. Scheme): https://bootstrapworld.org/materials/spring2021/en-us/course... . It's taught in some schools as well as a comp sci curriculum.
- https://code.world/ teaches using a toned-down version of Haskell. To my knowledge it's not used in schools.
[1]: https://www.coursera.org/learn/programming-languages
[2]: https://www.coursera.org/learn/programming-languages-part-b
Note the following:
> This work is based on over 20 years worth of course notes and intellectual contributions by the authors named above; teasing out who contributed what is, by now, not an easy task. The primary compiler and author of this work in its form as a unified textbook is Michael R. Clarkson.
You do not need to be enrolled in CS 3110 to be able to utilize this content (said "unified textbook").
They could have certainly done something more radical than OCaml then such as, say, Idris2 which is truly a challenge for those not used to it.