Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omit module declarations in OCaml snippets #237

Open
hmemcpy opened this issue Feb 14, 2020 · 19 comments
Open

Omit module declarations in OCaml snippets #237

hmemcpy opened this issue Feb 14, 2020 · 19 comments

Comments

@hmemcpy
Copy link
Owner

hmemcpy commented Feb 14, 2020

Perhaps I should have asked this earlier, but better late than never :)
Dear @ArulselvanMadhavan @mseri, do you think we can omit the module declarations? Some things feel a bit noisy to me (but I don't know OCaml, so... I don't know what I'm talking about :))

image

What if we moved some of the common definitions to the first chapter (notes from the editor), and keeping the snippets smaller?

WDYT?

@mseri
Copy link
Contributor

mseri commented Feb 14, 2020

This is the way OCaml allows to abstract over common signatures, there are no typeclasses.
One could use first class modules instead of module functors but it would not work for all cases.

For examples like compose, one could just write

let compose g f : ‘a -> ‘c = fun x -> g (f x)

But for other examples is unavoidable.

@mseri
Copy link
Contributor

mseri commented Feb 14, 2020

I’ll have a read as soon as I can, likely in a couple of weeks, to check all that could be simplified. But in most cases I think we have to leave with the verbosity

@yawaramin
Copy link

yawaramin commented Feb 15, 2020

Definitely looks too verbose to me :-) I've just started reading the OCaml code examples and they declare a lot more than the Haskell versions do. E.g.:

-- Haskell
f :: A -> B
(* OCaml *)
module type Polymorphic_Function_F = sig
  type a
  type b

  val f : a -> b
end

The equivalent OCaml should be:

val f : a -> b

As for the composition example, it would be:

let (<<) g f x = g (f x)
g << f

Note that I reversed the direction of the angle brackets. OCaml doesn't ship with these operators, but in F#, the direction of the brackets indicates the 'data flow', so a flow of 'g after f' would be g << f.

[EDIT: my key point with the OCaml code examples is that they declare too much. Way more than the Haskell equivalent code declares.]

@hmemcpy
Copy link
Owner Author

hmemcpy commented Feb 15, 2020

Perhaps we could add a Prelude chapter, defining all the helper operators and modules?

@yawaramin
Copy link

Maybe. It would essentially turn the Prelude chapter into an OCaml re-implementation of certain parts of the Haskell Prelude :-)

@XVilka
Copy link

XVilka commented Feb 15, 2020 via email

@mseri
Copy link
Contributor

mseri commented Feb 15, 2020

The Prelude is a good idea, indeed. I think it would be worth even if it will not help once you deal with the chapters on Monoid/Monad/Comonad/Fix/... where you really need the functorial implementation (or a first class module) with the signature available somewhere close by, as it is part of the chapter content itself

@mseri
Copy link
Contributor

mseri commented Feb 17, 2020

Concerning the composition, I think you may also want to have a look here: #201 (comment)

@mseri
Copy link
Contributor

mseri commented Feb 17, 2020

There is also one more question here.

The fist chapter contains lots of module types to make te code fully compilable ocaml, where type signature go separate from the implementation on interface files or module signatures.

We could decide to make a compromise to the sake of readability and allow to just show the signature even though it is not compilable ocaml and not the correct thing to do. We would need to explain this in the prelude and, again, this change would not “fix“ many other snippets

@yawaramin
Copy link

@mseri it's not just the first chapter. My question is–why does the OCaml code need to be fully compilable? The Haskell code is not, as I pointed out above. I assumed that was originally done for pedagogical reasons–i.e. the goal was to sketch out how the concepts would look in code form, not to provide full working implementations. Why doesn't the OCaml version follow that pedagogical principle?

We would need to explain this in the prelude

The explanation would need to be provided for the Haskell code too then, I guess.

@yawaramin
Copy link

Hey folks, I am on chapter 4 now, I am noticing also that the Haskell and OCaml code samples are 'out of sync'. I've cloned the repo and made some changes locally from chapters 1 to 4 to what I think is a closer match of the Haskell code. Would a PR be welcome?

@hmemcpy
Copy link
Owner Author

hmemcpy commented Feb 20, 2020 via email

@yawaramin
Copy link

Thanks, sent #242 . I believe this will re-sync the code samples. Since I'm reading through the book as well, syncing them up as I go.

@ArulselvanMadhavan
Copy link
Contributor

ArulselvanMadhavan commented Feb 20, 2020

@mseri it's not just the first chapter. My question is–why does the OCaml code need to be fully compilable? The Haskell code is not, as I pointed out above. I assumed that was originally done for pedagogical reasons–i.e. the goal was to sketch out how the concepts would look in code form, not to provide full working implementations. Why doesn't the OCaml version follow that pedagogical principle?

We would need to explain this in the prelude

The explanation would need to be provided for the Haskell code too then, I guess.

@yawaramin I haven't been following the discussion completely but I can answer the question on writing compilable OCaml code. I chose to do it that way because it was easier for me to make changes and still be confident that I haven't introduced any errors, as I discovered OCaml syntax subtleties along the way. Part of my reason for translating the book was to force myself to learn OCaml

@XVilka
Copy link

XVilka commented Feb 20, 2020

In my opinion, it is better to go the opposite way - make Haskell/Scala code compilable too, instead of reducing the quality level of OCaml flavor. It will be beneficial for these languages as well, and easier to spot the mistakes.

@yawaramin
Copy link

Hi @ArulselvanMadhavan thank you for your efforts. It's a massive achievement for sure. The reason I've been recommending that the code stay close to the Haskell samples, is for teaching reasons. When people are learning, we shouldn't force them to immediately deal with the software engineering concerns of compilation–and the more complex code that comes with it. We want them to get the basic ideas first. Once they have those, they can figure out how to turn the samples into actual production code.

This makes it more difficult for the authors and translators but I think it's important to make it easier for learners, than for ourselves.

Hi @XVilka , I had assumed that the Haskell samples were designed by Dr Milewski to get his ideas across as simply as possible, and their point was not really to be production-level code.

@hmemcpy
Copy link
Owner Author

hmemcpy commented Feb 20, 2020

I don't have good input on this, but one thing to consider is the book length. The maximum number of pages for a print book is 480 (on Blurb, at least), and each page increases the overall cost of printing.

@XVilka
Copy link

XVilka commented Feb 20, 2020

Compilable code will make updating the book more straightforward, in case of OCaml or Haskell language syntax improvements. And judging by the last few years, both languages are quite active at changing/improving the syntax. Anyway, just my two cents.

@hmemcpy
Copy link
Owner Author

hmemcpy commented Feb 20, 2020

I'm sure there'll be a separate Scala 3 version of the book at some point, once all syntax has been finalized. My goal was always keeping the original text and snippets intact, changing it as little as possible. That said, this is a "live" document, it can be updated with the new syntax as languages evolve.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants