News for hackers. Stuff that matters.
Re:The algebra and calculus of algebraic data types by jpfr on Saturday September 08, 2018 @05:10PM (#17942800)
Such functions can’t exist.

The “C” void is expressed by the unit singleton.

So it is |a|^1.

Re:The algebra and calculus of algebraic data types by Sharlin on Saturday September 08, 2018 @05:21PM (#17942849)
Yes, C void is a kinda-sorta unit type. But I’m talking about the real uninhabited type, ie. Void in Haskell, Nothing in Scala, ! (Never) in Rust. Real-world languages including Haskell are not total, so all functions a->b are in reality a->(b|Void). And for a good reason, as totality means loss of Turing completeness. In a total language, a function `a->Void` does not exist, but what about `Void->a`, then? What is the single inhabitant of that type?
Re:The algebra and calculus of algebraic data types by tpush on Saturday September 08, 2018 @05:44PM (#17942940)
Since one can prove anything if you're assuming nonsense, a function of the type 'Void -> a' should be able to just return its argument.

And assuming the theory is consistent(so no infinite loops etc.) this is the only instance of that function, so the cardinality is 1.

Re:The algebra and calculus of algebraic data types by matt-noonan on Saturday September 08, 2018 @09:57PM (#17943880)
The "should be able to just return its argument" part is wrong; the types pretty clearly don't match, yeah? It is true that there is a unique function of type `Void -> a` for each `a`, but that isn't it.
Re:The algebra and calculus of algebraic data types by tpush on Sunday September 09, 2018 @06:26AM (#17945237)
When the Bottom type is included in a language it usually exhibits sub typing behavior. That can be expressed in Haskell, too:

  {-# LANGUAGE RankNTypes #-}

  type Bottom = forall a. a -- zero inhabitants

  f :: Bottom -> a
  f x = x

That passes the type checker just fine.
Re:The algebra and calculus of algebraic data types by joel_ms on Monday September 10, 2018 @01:49AM (#17949662)
> That passes the type checker just fine.

I think you are running into some weird haskellisms with your Bottom-type.

The normal way of defining that in haskell is using the "EmptyDataDecls" pragma, like this:

    {-# LANGUAGE EmptyDataDecls #-}

    data Empty 

    g :: Empty -> a
    g x = x 
Which doesn't pass the type checker.

(From a theoretic standpoint, I would have thought your Bottom was a essentially a type-level identity function..)

Re:The algebra and calculus of algebraic data types by tpush on Monday September 10, 2018 @05:58AM (#17950620)
Again, languages with a built in bottom usually have sub typing behavior that makes bottom a subtype of everything (so you can give a value of type bottom to everything).

'forall a. a' is just how you define the bottom type in System F, which Haskell is somewhat based on. As a type it describes that a term of that type can just conjure a value of any type out of thin air, which is obviously nonsense. That what makes it the bottom type.

The sub typing behavior associated with that is just the normal subsumption rule of polymorphic functions. The same reason why you can pass a function of type 'forall a. a -> a' to something expecting a function of type 'Int -> Int'. The types don't 'match' directly, but they do under the subsumption rule.

Re:The algebra and calculus of algebraic data types by joel_ms on Monday September 10, 2018 @08:42AM (#17951349)
Yeah, I just wanted to show that (GHC) haskell has a way of getting a type with "zero" inhabitants that actually gets caught by the type checker. (I say "zero" since of course my Empty-type above does contain non-terminating programs and (undefined :: a).)

With "forall a. a" I was coming from the persepctive of intuisionistic type theory where the usual parametric polymorphism just get subsumed by Π-types and universal quantification is usually represented with Π-types, so you would have:

         forall a.a       (Universal Quantification)
    <==> Π(a : Type) a    (Π-type)
    <==> (a : Type) -> a  (Agda-notation)
     ==> a -> a           (Π-type as regular function type)
where Type represents any type. That's what I meant by "type-level identity function".
Re:The algebra and calculus of algebraic data types by matt-noonan on Monday September 10, 2018 @09:38AM (#17951685)
Interesting, thanks! I hadn't considered using "forall a. a" as the bottom type, but it makes sense.
Re:The algebra and calculus of algebraic data types by Sharlin on Saturday September 08, 2018 @07:15PM (#17943342)
And correspondingly `a->Void` must be empty because you can’t prove a contradiction given a true proposition in a consistent system? Makes sense from the Curry-Howard point of view.
Re:The algebra and calculus of algebraic data types by maweki on Saturday September 08, 2018 @06:21PM (#17943107)
Where does an instance of a come from in that case? A function of that type has no way to construct one.
Re:The algebra and calculus of algebraic data types by theoh on Saturday September 08, 2018 @06:34PM (#17943179)
Yes, there are no possible return values. But there is a unique vacuous total function because no return values are ever needed. A function can be considered as a set of ordered pairs, one for each value in the domain. Since there are no values in the domain, no pairs need exist.

Another way to look at it is as a constrained subset of the Cartesian product of domain and codomain. In this case the domain is the empty set, so the Cartesian product is empty too.

Re:The algebra and calculus of algebraic data types by tpush on Saturday September 08, 2018 @06:38PM (#17943198)
The key is that you can (in theory, I don't know if Haskell has a proper bottom that can act as a type) give a value of Void/Bottom to anything, analogous to logic in that a proof of nonsense can be used as a proof of anything.

Take this Haskell code that passes the type checker:

  bottom = bottom

  f :: Int -> a
  f _ = bottom

Here 'bottom' has the conceptual type Void/Bottom and checks successfully against the 'a'.

So assuming we have this Void type in Haskell (I don't know if Haskell has it or not), this should also type check:

  f :: Void -> a
  f x = x
Just like bottom (of type Void) checked against the 'a' earlier, an argument of that type should also type check.
Re:The algebra and calculus of algebraic data types by Sharlin on Saturday September 08, 2018 @06:00PM (#17943028)
That makes sense, thanks!
Re:The algebra and calculus of algebraic data types by amboo7 on Saturday September 08, 2018 @05:31PM (#17942888)
It is the so-called empty function, which diverges, e.g. throws an exception if called.
Re:The algebra and calculus of algebraic data types by erdeszt on Saturday September 08, 2018 @05:29PM (#17942877)
This one typechecks but loops forever:

  f :: Void -> a
  f _ = let x = x in x