Are Lists Inductive or Coinductive in Haskell?

Due to laziness, Haskell types are both inductive and coinductive, or, there is no formal distinguishment between data and codata. All recursive types can contain an infinite nesting of constructors. In languages such as Idris, Coq, Agda, etc. a definition like ones = 1 : ones is rejected by the termination checker. Laziness means that … Read more

Should I use typeclasses or not?

Why I shouldn’t do something simple and only defining functions without defining new data types and typeclasses (with their instances). Why indeed? You could just define: checkState :: (a -> Bool) -> (a -> b) -> (a -> b) -> a -> b checkState is_repairable repairs destroy a = if (is_repairable a) then repairs a … Read more

What is Applicative Functor definition from the category theory POV?

The key to understanding applicative functors is to figure out what structure they preserve. Regular functors preserve the basic categorical structure: they map objects and morphisms between categories, and they preserve the laws of the category (associativity and identity). But a category may have more structure. For instance, it may allow the definition of mappings … Read more

Let Haskell functors sink in.

Here’s the functor class: class Functor f where fmap :: (a -> b) -> f a -> f b Note that “f” by itself is a type constructor because it’s applied to a type variable in the fmap line. Here are some examples to make this clear: Type constructors: IO Maybe Either String Types: IO … Read more

Is it possible to program and check invariants in Haskell?

The following is a stunt, but it’s quite a safe stunt so do try it at home. It uses some of the entertaining new toys to bake order invariants into mergeSort. {-# LANGUAGE GADTs, PolyKinds, KindSignatures, MultiParamTypeClasses, FlexibleInstances, RankNTypes, FlexibleContexts #-} I’ll have natural numbers, just to keep things simple. data Nat = Z | … Read more