-1

why there are |b|^|a| inhabitants in the function a->b I think it should be "a" inhabitants in it as a is any type. Can't understand why it is not.

littlefish
  • 71
  • 6
  • 4
    Why do you think there are |a| inhabitants in the first place? – Willem Van Onsem Jan 05 '19 at 22:50
  • 2
    I'm voting to close this question as off-topic because it is not a question about Haskell or programming in general, but rather about mathematics. Try asking this on [Mathematics stack exchange](https://math.stackexchange.com/), if it has not already been asked. – AJF Jan 05 '19 at 22:55
  • I suspect you've misunderstood the word "inhabitants" here (which is a strange one to use). As already pointed out, this is a question of pure mathematics - nothing to do with programming, or Haskell - but from the figure of `|b|^|a|` it's clear that it means the number of different functions from `a` to `b`. I suspect you're thinking of something different. (Note that Haskell has no concept of functions being "the same" or different - function types are not members of `Eq`. This is for the simple reason that it is impossible in practice to test if 2 functions agree on every possible input.) – Robin Zigmond Jan 05 '19 at 23:59
  • 1
    @RobinZigmond "Inhabitants" is an appropriate word here. As for sameness of functions, in this context that usually means extensional equality, that is, having the same values at every argument. See also [*Abusing the algebra of algebraic data types - why does this work?*](https://stackoverflow.com/q/9190352/2751851). – duplode Jan 06 '19 at 12:51
  • @AJFarmar it is, of course, a Haskell problem as a and b is any type here – littlefish Jan 06 '19 at 14:27
  • @WillemVanOnsem As far as i can see it is because it can have |a| number of instances in the any type so a should contain |a| inhabitants – littlefish Jan 06 '19 at 14:29
  • 1
    @littlefish No, `a` and `b` here represent sets, which is why you're using the well-known set notation `|a|` to represent the cardinality of the set `a`. The fact that this may be applied to Haskell types is incidental; this may be applied to any typed programming language, and indeed mathematical functions. – AJF Jan 06 '19 at 14:32
  • @AJFarmar ok if you say so – littlefish Jan 06 '19 at 14:33
  • 2
    @AJFarmar With respect, I think the OP probably knows what their own question is about. – Rein Henrichs Jan 06 '19 at 18:00
  • @ReinHenrichs THANK YOU! – littlefish Jan 10 '19 at 15:47

1 Answers1

7

Let's talk about a specific example, namely functions on the two specific types below:

data Three = A | B | C
data Four = OneFish | TwoFish | RedFish | BlueFish

A function of type Three -> Four can be fully specified by choosing one of the four possible values of type Four for each of the inputs A, B, and C; here's an example:

f A = TwoFish
f B = BlueFish
f C = OneFish

Here's another example; it's just like f except that it makes a different choice for C:

g A = TwoFish
g B = BlueFish
g C = BlueFish

Since there are four choices of output for A, four choices of output for B, and four choices of output for C, there are 4*4*4 = 64 possible choices of functions.

Now we can generalize a little bit and talk about total functions on other finite input types. As before, a function from a to b can be fully specified by telling which b it chooses for each input a. Where before we had 4 possible outputs for each input, there are now |b|-many possibilities for each of these choices. As before, since the choices are completely independent, we must multiply |b| by itself once for each possible input -- that is, |a|-many times. That is, we have |b|^|a| possible complete specifications.

Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380