1. 13
  1.  

  2. 1

    Very nice. Is the (b ~ Double) trick needed because () gets picked by defaulting rules; and if so, is this only a problem in GHCi (which does more defaulting) or would it also affect .hs files given to GHC?

    Nitpick: I would define KB as 1000, MB as 1000000, KiB as 1024 and MiB as 1024*1024. I would definitely avoid the names KBi and MBi, which look like typos for the latter ;)

    1. 2

      Very nice. Is the (b ~ Double) trick needed because () gets picked by defaulting rules

      No. I described in the blog post the transition steps:

      • Bytes -> Double
      • Bytes -> b
      • (b ~ Double) => Bytes -> b

      It’s needed because otherwise type inference doesn’t work. The defaulting is a different error/issue that is an artifact of GHCi trying to help the inferencer, but it doesn’t work if you use it outside of that context.

      You need this trick to make the syntactic pattern work with type inference at all, it’s not particular to my demonstration using GHCi. I wouldn’t go to the effort of writing a blog post about it otherwise. This demonstration/trick links up with a related issue in other languages where you can’t really have return type polymorphism. You’re absolutely permitted this in Haskell but it’s easy to break inference if you don’t use type families, functional dependencies, associated types, or this trick. What all of these techniques have in common is that they let you create a relationship or equality between types that helps type inference. Haskell has no reason to assume that just because you used a numeric literal as a function that you mean the particular instance Bytes -> Double. The argument type of a function does not imply the return type, it could be anything.

      This article about functional dependencies describes the problem and motivates a particular solution: https://wiki.haskell.org/Functional_dependencies

      Nitpick: I would define KB as 1000, MB as 1000000, KiB as 1024 and MiB as 1024*1024. I would definitely avoid the names KBi and MBi, which look like typos for the latter ;)

      Seems reasonable. I’m not trying to publish a library here so I’m not going to change it, but anybody that feels inspired to do so should probably follow your suggestion here.