Use the type lits Nat solver plugin by Christian B, or a userland peano encoding. Sadly ghc does have any builtin equational theory so you either need to construct proofs yourself or use a plugin.
Hi List,When I try to build below program, the compiler complainsCouldn't match type ‘n’ with ‘1 + (n - 1)’ …Why GHC (7.10.3) cannot do type level natural number arithmetic? `` n /= 1 + (n-1)`` at type level?-- | main.hs{-# LANGUAGE ScopedTypeVariables #-}{-# LANGUAGE FlexibleInstances #-}{-# LANGUAGE GADTs #-}{-# LANGUAGE PolyKinds #-}{-# LANGUAGE KindSignatures #-}{-# LANGUAGE TypeOperators #-}{-# LANGUAGE DataKinds #-}{-# LANGUAGE OverloadedStrings #-}{-# LANGUAGE DeriveGeneric #-}module Main whereimport qualified Data.ByteString as Bimport Data.ByteString(ByteString)import Data.Serialize -- require cerealimport GHC.TypeLitsdata Scalar :: Nat -> * -> * whereNil :: Scalar 0 aCons :: a -> Scalar m a -> Scalar (1+m) ainstance (Serialize a) => Serialize (Scalar 0 a) whereget = return Nilput _ = return $ memptyinstance (Serialize a) => Serialize (Scalar n a) whereget = dox <- get :: Get axs <- get :: Get (Scalar (n-1) a)return $! Cons x xsput (Cons x xs) = doput (x :: a)put xs--/tmp/vect1/app/Main.hs:31:15: Couldn't match type ‘n’ with ‘1 + (n - 1)’ …‘n’ is a rigid type variable bound bythe instance declaration at /tmp/vect1/app/Main.hs:27:10Expected type: Scalar n aActual type: Scalar (1 + (n - 1)) aRelevant bindings includexs :: Scalar (n - 1) a (bound at /tmp/vect1/app/Main.hs:30:5)get :: Get (Scalar n a) (bound at /tmp/vect1/app/Main.hs:28:3)In the second argument of ‘($!)’, namely ‘Cons x xs’In a stmt of a 'do' block: return $! Cons x xsCompilation failed.- baojun