Lists of fixed length and type literals

前端 未结 2 881
栀梦
栀梦 2021-01-02 08:22

I\'m trying to define a type for lists of fixed length in Haskell. When I use the standard way to encode natural numbers as types in unary, everything works fine. However, w

2条回答
  •  温柔的废话
    2021-01-02 08:34

    This is not really an answer.

    Using https://hackage.haskell.org/package/ghc-typelits-natnormalise-0.2 , this

    {-# LANGUAGE GADTs #-}
    {-# LANGUAGE TypeOperators #-}
    {-# LANGUAGE DataKinds #-}
    {-# LANGUAGE KindSignatures #-}
    {-# OPTIONS_GHC -fplugin GHC.TypeLits.Normalise #-}
    
    import GHC.TypeLits
    
    data List (n :: Nat) a where
      Nil :: List 0 a
      (:>) :: a -> List n a -> List (n+1) a
    
    append :: List n1 a -> List n2 a -> List (n1 + n2) a
    append Nil       ys = ys
    append (x :> xs) ys = x :> (append xs ys)
    

    ... compiles, so obviously it's correct.

    ???

提交回复
热议问题