Like “iterating” over a function whose type changes between iterations, but the formal definition is the same

I was just starting to learn Haskell, and I ran into the following problem. I am trying to iterate over a function \x->[x]. I expect to get a result [[8]]on

foldr1 (.) (replicate 2 (\x->[x])) $ (8 :: Int)

This does not work and gives the following error message:

A check occurs: it is impossible to build an infinite type: a ~ [a]

Expected Type: [a -> a]

Actual Type: [a -> [a]]

I understand why this is not working. This is because it foldr1has a type signature foldr1 :: Foldable t => (a -> a -> a) -> a -> t a -> aand takes a -> a -> aas the signature of the type of its first parameter, and nota -> a -> b

This is also for the same reason:

((!! 2) $ iterate (\x->[x]) .) id) (8 :: Int)

However, this works:

(\x->[x]) $ (\x->[x]) $ (8 :: Int)

and I understand that the first (\x->[x])and second are of different types (ie, [Int]->[[Int]]and Int->[Int]), though formally they look the same.

, 2 , 100.

, ? , Template Haskell? , ?

node, read it. , , read . ,

read "[[[[[8]]]]]" :: ??

, ??, . , , - .

, "" . :

natSucc x = [Left x,Right [x]]

succ, . , foldr1-replicate !!-iterate .

. .

Edit: 3 , ( ), , , . (, Just Just Just 8, ?).

+4
3

.

@chi , . fin , .

:

{-# language DataKinds, PolyKinds, KindSignatures, ScopedTypeVariables, TypeFamilies #-}

, :

import Data.Type.Nat

type family Nested (n::Nat) a where
    Nested Z a = [a]
    Nested (S n) a = [Nested n a]

, ghci,

*Main> :kind! Nested Nat3 Int
Nested Nat3 Int :: *
= [[[[Int]]]]

(Nat3 - , Data.Type.Nat.)

, , . ,

newtype Iterate (n::Nat) a = Iterate { runIterate :: (a -> [a]) -> a -> Nested n a }

fin induction1, Nat. Iterate, Nat. Nat , :

iterate' :: forall n a. SNatI n => Iterate (n::Nat) a
iterate' =
    let step :: forall m. SNatI m => Iterate m a -> Iterate (S m) a
        step (Iterate recN) = Iterate (\f a -> [recN f a])
    in  induction1 (Iterate id) step

ghci ( -XTypeApplications Nat):

*Main> runIterate (iterate' @Nat3) pure True
[[[[True]]]]
+4

, 2 :: Int 4 :: Int . Haskell > & dagger; , foldr1 (.) (replicate 2 (\x->[x])) (8 :: Int) foldr1 (.) (replicate 4 (\x->[x])) (8 :: Int) , , [[8]] :: [[Int]], [[[[8]]]] :: [[[[Int]]]]. , ( Haskell ). .

, Haskell: . , , - – , , . - :

data Tree a = Leaf a | Rose [Tree a]

Prelude> foldr1 (.) (replicate 2 (\x->Rose [x])) $ Leaf (8 :: Int)
Rose [Rose [Leaf 8]]
Prelude> foldr1 (.) (replicate 4 (\x->Rose [x])) $ Leaf (8 :: Int)
Rose [Rose [Rose [Rose [Leaf 8]]]]

& dagger; , GHC Haskell (. DaniDiaz), . >

+5

, - : .

, , , :

data NestList a = Zero a | Succ (NestList [a]) deriving Show
instance Functor NestList where
    fmap f (Zero a) = Zero (f a)
    fmap f (Succ as) = Succ (fmap (map f) as)

, , , ; ,

Succ (Succ (Zero [['a']])) :: NestList Char

\x -> [x]; , Succ.

> iterate (\x -> Succ (fmap (:[]) x)) (Zero 8) !! 5
Succ (Succ (Succ (Succ (Succ (Zero [[[[[8]]]]])))))

Your suggestion on how to implement natural numbers can be changed similarly to using a simple recursive type. But the standard way is even cleaner: just take it higher NestListand discard all arguments.

data Nat = Zero | Succ Nat
+5
source

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


All Articles