News for hackers. Stuff that matters.
Re:Haskell: The Bad Parts, part 2 (2020) by samthecoy on Tuesday February 09, 2021 @10:25AM (#26078503)
Partial functions are not the same thing as "partially applied functions". Partial functions means that not every element of the domain is mapped to an element of the range, for example:

    divTenBy :: Double -> Double
    divTenBy n = 10 / n
If you actually call the above function you get a runtime exception. We really don't like functions that do this; they are called partial.
Re:Haskell: The Bad Parts, part 2 (2020) by Athas on Tuesday February 09, 2021 @11:14AM (#26079215)
Am I missing something subtle? Why would you get a runtime exception if you call this function?
Re:Haskell: The Bad Parts, part 2 (2020) by jsmith45 on Tuesday February 09, 2021 @11:21AM (#26079301)
The parent missed a part. If you call it with 0 you get an exception, because division by zero obviously.
Re:Haskell: The Bad Parts, part 2 (2020) by yakubin on Tuesday February 09, 2021 @06:42PM (#26084061)
No, you don't. You get Infinity. That's how floating point works.
Re:Haskell: The Bad Parts, part 2 (2020) by smabie on Tuesday February 09, 2021 @10:11PM (#26085357)
In most languages you get an exception
Re:Haskell: The Bad Parts, part 2 (2020) by yakubin on Tuesday February 09, 2021 @11:24PM (#26085738)
Which languages would those be?
Re:Haskell: The Bad Parts, part 2 (2020) by warkdarrior on Wednesday February 10, 2021 @12:26PM (#26092089)

  $ python3
  Python 3.9.1 (default, Feb  3 2021, 07:04:15)
  [Clang 12.0.0 (clang-1200.0.32.29)] on darwin
  Type "help", "copyright", "credits" or "license" for more information.
  >>> 10 / 0
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  ZeroDivisionError: division by zero
  >>>
Re:Haskell: The Bad Parts, part 2 (2020) by yakubin on Wednesday February 10, 2021 @09:45PM (#26097660)
Although Python seems to be the exception to the rule here, your example isn't exactly right. In your example, you're performing integer division, not floating point division. In most languages, integer division by zero is indeed some kind of exception (or UB, which will most likely result in a hardware exception, causing a signal sent to the process). However, floating point division by zero is well-defined, as per definition of floating point arithmetic (in some RFCs), and most languages don't throw exceptions in this case. Python indeed seems to be the exception to the rule.
Re:Haskell: The Bad Parts, part 2 (2020) by dthul on Thursday February 11, 2021 @09:28AM (#26101791)
In Python 3, 1/0 is floating point division. 1//0 would be integer division.
Re:Haskell: The Bad Parts, part 2 (2020) by yakubin on Thursday February 11, 2021 @11:31AM (#26103539)
Right, Python2 is still the default on Debian, so I didn't notice.
Re:Haskell: The Bad Parts, part 2 (2020) by smabie on Tuesday February 09, 2021 @11:31PM (#26085767)
I guess I'm thinking of Python mostly. I thought OCaml did as well but apparently not.
Re:Haskell: The Bad Parts, part 2 (2020) by the_af on Tuesday February 09, 2021 @11:23AM (#26079326)
If you call it with argument 0 you get a runtime exception, because it results in a division by zero.

It's "partial" because it's not defined for 0.

Re:Haskell: The Bad Parts, part 2 (2020) by magicalhippo on Tuesday February 09, 2021 @05:19PM (#26083170)
If Double is IEEE 754 compatible then it should be perfectly defined for 0, you'd get +/- infinity. There are algorithms which rely on this.
Re:Haskell: The Bad Parts, part 2 (2020) by cies on Tuesday February 09, 2021 @12:43PM (#26080376)
So it should have been: Double -> Maybe Double

That would be the "right way" to fix this.

(or create a type NotNilDouble, lol)

Re:Haskell: The Bad Parts, part 2 (2020) by bidirectional on Tuesday February 09, 2021 @02:00PM (#26081258)
Or, controversially, define 1/0 = 0.[1]

[1]: https://www.hillelwayne.com/post/divide-by-zero/

Re:Haskell: The Bad Parts, part 2 (2020) by cies on Tuesday February 09, 2021 @02:01PM (#26081267)
Enough controversy in defining std libs as it is :)
Re:Haskell: The Bad Parts, part 2 (2020) by the_af on Tuesday February 09, 2021 @03:11PM (#26081897)
The "right" way for division by zero is controversial, though maybe that would be a solution.

It's clearer for partial function "head :: [a] -> a", which takes the first of a list if it exists, and explodes without dignity if the list is empty (this is what makes head partial).

A proposal is "head :: [a] -> Maybe a", so head returns Nothing when the list is empty.

Re:Haskell: The Bad Parts, part 2 (2020) by cies on Wednesday February 10, 2021 @06:04AM (#26087721)
Re:Haskell: The Bad Parts, part 2 (2020) by cies on Wednesday February 10, 2021 @06:06AM (#26087733)
Re:Haskell: The Bad Parts, part 2 (2020) by a_wild_dandan on Tuesday February 09, 2021 @10:46AM (#26078790)
Ah, so partial functions aren't onto (i.e. surjective).
Re:Haskell: The Bad Parts, part 2 (2020) by wetmore on Tuesday February 09, 2021 @11:05AM (#26079082)
Not exactly, partial functions can be surjective, eg

f :: Int -> Bool

f 0 = True

f 1 = False

f n = f (n + 1)

is surjective onto Bool but also partial (doesn't return for n > 1). In Haskell we say that when a function doesn't return, the output is bottom, written as ⊥.

You could say that a function f : A -> B is partial if f^{-1}(B \ ⊥) is surjective.

Re:Haskell: The Bad Parts, part 2 (2020) by curtisf on Tuesday February 09, 2021 @10:59AM (#26078998)
No, they are not functions in the mathematical sense. It's not that they don't cover the output space, they don't cover the input space.