Is there any practical difference between these two definitions of summing Peano Natural's natural numbers?

I am new to level-level programming, and I am unable to reason about very simple level programs.

I have this code:

data Nat = Zero | Succ Nat

type family n + m where
    Zero + m = m  -- This is the base case of my type level recursive function.

Now I have two options for switching to a recursive case, and only one of them is typechecks without UndecidableInstances:

    Succ n + m = n + Succ m  -- Typechecks with UndecidableInstances.

    Succ n + m = Succ (n + m)  -- Typechecks without UndecidableInstances.

I understand that it UndecidableInstancesallows me to use general recursion at the type level, and not just structural recursion, which would guarantee the end of typechecker. But in fact, both definitions seem to compile equally well and give reasonable types when checking with :kind!repl for small numbers.

, , ? + ?

+4
2

,

foo :: T (Succ n) -> Bool
bar :: T n -> T m -> T (n + m)

baz :: T (Succ a) -> String
baz x | foo (bar x x) = "A"
      | otherwise     = "B"

, +

Succ n + m = Succ (n + m)

Succ n + m = n + Succ m

baz , bar x x :: a + Succ (Succ a), foo.

: , ground" Nat. (.. ), Succ a + Succ a, . , .

+2

, . , , , .

@Benjamin Hodgson, , , , , , .

. , :

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}

data Nat = Zero | Succ Nat

-- A vector type
data Vec n a where
  V0 :: Vec Zero a
  (:>) :: a -> Vec n a -> Vec (Succ n) a
infixr 5 :>

"" (+), . , m n, :

type family m + n where
  Zero + m = m
  Succ m + n = Succ (m + n)

(++) , :

vappend :: Vec m a -> Vec n a -> Vec (m + n) a
vappend V0 ys = ys
vappend (x :> xs) ys = x :> vappend xs ys

, :

rev :: [a] -> [a] -> [a]
rev ys [] = ys
rev ys (x:xs) = rev (x:ys) xs

reverse:

reverse' = rev []

vrev, :

vrev :: Vec m a -> Vec n a -> Vec (n + m) a
vrev ys V0 = ys
vrev ys (x :> xs) = vrev (x :> ys) xs

, , GHC :

(n1 + 'Succ m) ~ 'Succ (n1 + m)

"" .

n + m m + n - .

, + :

Succ m + n = m + Succ n

, vrev , vappend !

, ? , , , "" . :

Succ m + n = Succ (m + n)

. , , , singletons-nats.

+2

Source: https://habr.com/ru/post/1693647/


All Articles