2

Sorry about the title, I don't know if there's a better way to describe it in one short sentence.

The idea is I want a function type to express that the function accepts one argument but will never evaluate it.

There are few alternatives I can think of, and I want to know what would be the best one:

(for all following examples, Int is just an arbitrary choice from any concrete types)

  1. f :: forall a . a -> Int, feeding f with anything will do
  2. f :: () -> Int, there's nothing but a trivial value to be evaluated
  3. f :: Void -> Int, this could be a bad idea, as it's possible that f = absurd and f is applied with undefined or let x = x in x, but if you know f terminates, then you also know it will never evaluate its argument.
  4. I don't know if there are other ways, please give your suggestions if any.

About why I need a function like this in the first place: suppose I want to design a stream-processing library and let type of stream processors be like SP <input-type> <output-type> and it's possible that a processor doesn't need any input. and at some point I'll have to deal with a function like <input-type> -> <output-type> if I want to make this an Arrow.

Javran
  • 3,394
  • 2
  • 23
  • 40
  • 1
    This is probably relevant: [*Why does Haskell Pipes “use () to close unused inputs and X (the uninhabited type) to close unused outputs”?*](http://stackoverflow.com/q/35161616/2751851) – duplode Nov 15 '16 at 19:41
  • @duplode thanks for the link, that's super relevant as I was trying to figure out the reason behind choice of `()` vs. `Void`. – Javran Nov 15 '16 at 21:38

1 Answers1

6

The first one is perfect. It will accept an argument of any type, so you have maximum flexibility in what you provide it (even undefined). #2 limits you to only a single valid argument, (). #3 isn't an option; you can't even call the function, because there is no valid argument to give it.

This is exactly what const :: a -> b -> a returns. foo = const 3 is a function that takes a value of any type and returns 3.

chepner
  • 497,756
  • 71
  • 530
  • 681