• cosmic_quanta 21 hours ago

    Interesting, thanks for posting.

    I share the author's frustration with the lack of non-compiler-related examples of GADT uses. It seems like such a powerful idea, but I haven't been able to get a feel for when to reach for GADTs in Haskell

    • undefined 18 hours ago
      [deleted]
      • wyager 18 hours ago

        I often find them handy for locking down admissible states at compile time. Maybe ~10 years ago in a processor design class, I wrote some CPUs in Haskell/Clash for FPGA usage. A nice thing I could do was write a single top-level instruction set, but then lock down the instructions based on what stages of the processor they could exist at.

        For example, something like (not an actual example from my code, just conceptually - may be misremembering details):

          data Instruction stages where
           MovLit :: Word64 -> Register -> Instruction '[Fetch, Decode, Execute, Writeback]
           -- MovReg instruction gets rewritten to MovLit in Execute stage
           MovReg :: Register -> Register -> Instruction '[Fetch, Decode, Execute]
           ...
        
        And then my CPU's writeback handler block could be something like:

          writeback :: (Writeback `member` stages) => Instruction stages -> WritebackState -> WritebackState
          writeback (MovLit v reg) = ...
          -- Compiler knows (MovReg _ _) is not required here
        
        So you can use the type parameters to impose constraints on the allowed values, and the compiler is smart enough to use this data during exhaustiveness checks (cf "GADTs Meet Their Match")
        • anyfoo 16 hours ago

          Wow, someone else who (used to be) using Clash. I still use it for everything I can in my (hobby) FPGA projects. I'm not sure I've used GADTs, but I've certainly made use of other more "advanced" parts of the type system, like type families.

          What you're doing here is pretty cool, I think I will start doing so, too. I have a number of places where I use "undefined" instead. (The "undefined" from the Clash Prelude, which translates into a "don't care" signal state.)

      • rbjorklin a day ago

        Does anyone have some hard numbers on the expected performance uplift when using GADTs? Couldn't see any mentioned in the article.

        • ackfoobar 21 hours ago

          The example here is basically an 8-fold memory saving going from `long[]` from `byte[]` - while still retaining polymorphism (whereas in Java the two are unrelated types).

          Hard to say exactly how much performance one would get, as that depends on access patterns.

          • misja111 20 hours ago

            The reason that a byte array is in reality layed out as a (mostly empty) long array in Java, is actually for performance. Computers tend to have their memory aligned at 8 byte intervals and accessing such an address is faster than accessing an address that's at an offset of an 8 byte interval.

            Of course it depends on your use case, in some cases a compact byte array performs better anyway, for instance because now you're able to fit it in your CPU cache.

            • ackfoobar 19 hours ago

              > a byte array is in reality layed out as a (mostly empty) long array in Java

              Are you saying each byte takes up a word? That is the case in the `char array` in OCaml, but not Java's `byte[]`. AFAIK The size of a byte array is rounded up to words. Byte arrays of length 1-8 all have the same size in a 64-bit machine, then length 7-16 take up one more word.

              https://shipilev.net/jvm/objects-inside-out/

              • john-h-k 20 hours ago

                But you can load any byte by loading 8 bytes and shift (v cheap)

          • hyperbrainer 20 hours ago
            • goldchainposse 20 hours ago

              I know Jane Street love OCaml, but you have to wonder how much it's cost them in velocity and maintenance. This is a quant firm blogging about a programming language they're the most famous user of.

              • pjmlp 19 hours ago

                It is thanks to the companies like Jane Street that believe there is something else beyond C, that we can have nice toys.

                Remember if OCaml wasn't a mature programming language, maybe Rust would not have happened in first place.

                • fjwufjfa 15 hours ago

                  It's easier to reason in FP plus the python paradox [1] [2].

                  [1]: https://www.paulgraham.com/pypar.html

                  [2]: https://blog.janestreet.com/why-ocaml/

                  • AdieuToLogic 12 hours ago

                    I agree with your point about reasoning when employing Functional Programming (FP).

                    However, I very much disagree with Graham's 2004 assertion[0]:

                      It's a lot of work to learn a new programming language. And 
                      people don't learn Python because it will get them a job; 
                      they learn it because they genuinely like to program and 
                      aren't satisfied with the languages they already know.
                    
                    It does not require "a lot of work to learn a new programming language" once a person has fluency with at least one. Actually, the difficulty of learning a new programming language is inversely proportional to how many programming languages the person has already learned. Especially if a new programming language is in the same paradigm category as those already known (Procedural, OOP, FP, etc.).

                    I was a professional software engineer in 2004, when the Graham post was written. To say, "people don't learn Python because it will get them a job ..." was bullshit then just as it is now. The remainder of the quoted sentence is unfounded extrapolation and has the value of same.

                    0 - https://www.paulgraham.com/pypar.html

                    • codr7 13 hours ago

                      For certain classes of programs, yes. I have a hunch finance is a pretty good fit.

                    • kryptiskt 19 hours ago

                      Why do you assume it's a drag for them and not a competitive advantage? I don't know if it's such a terrible thing to use a slightly out of mainstream language, when the standard in the business is to accumulate tens of millions of lines of C++.

                      • ackfoobar 19 hours ago

                        Agreed, indeed I believe they have mentioned that OCaml gets them to ship quicker because they are more confident with the correctness of changes.

                        But being outside of the mainstream may mean you need to occasionally debug more esoteric stuff: https://gallium.inria.fr/blog/intel-skylake-bug/ I'm sure Jane Street can afford doing that, but I'm not so sure if a small team can.

                        • gjadi 17 hours ago

                          That was an interesting read, thanks. However I fail to see how it's an issue specific to ocaml. It was a bug in the Skylake processor triggered by a special pattern of instructions produced by gcc. Ocaml built with clang was ok because it doesn't used the same pattern. Did I miss something?

                          • ackfoobar 17 hours ago

                            If the JVM encountered the same bug other people would have discovered it before me. Most probably I won't even know the bug exists.

                        • goldchainposse 17 hours ago

                          > Why do you assume it's a drag for them and not a competitive advantage?

                          Because despite them being very open about it, no one else does it, and every distinguished engineer who pushes a weird tech choice will justify and defend it.

                          • cdaringe 8 hours ago

                            People that haven’t used ocaml think it’s weird. I picked it up casually in 2020. It might not be popular, but it’s certainly not weird. It’s actually quite fantastic. These days I rarely ever use it, but I wish I did!

                        • anyfoo 16 hours ago

                          There are many things to say about this, but one of those things is that I think you are making the assumption that an (e.g.) C programmer who does not want (or even cannot) get into OCaml would somehow be better for this highly specialized, high-performance, and high-correctness-affine use case, than someone who does. And I'd question that assumption.

                          • lmm 13 hours ago

                            Jane Street has been one of the most successful financial firms of the last 10 years or so, going from a niche hedge fund to a big player. Sounds like OCaml has been working out for them. Certainly I know it's helped them hire a lot of excellent programmers.

                            • keybored 17 hours ago

                              Concretely how do you think it’s holding them back? Just by being niche?