Add 'hadrian/' from commit '45f3bff7016a2a0cd9a5455a882ced984655e90b'
[ghc.git] / testsuite / tests / typecheck / should_compile / T13943.hs
1 {-# LANGUAGE MultiParamTypeClasses #-}
2 {-# LANGUAGE TypeOperators #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE FlexibleContexts #-}
5 {-# LANGUAGE ScopedTypeVariables #-}
6 {-# LANGUAGE UndecidableInstances #-}
7 {-# LANGUAGE TypeApplications #-}
8 {-# LANGUAGE PolyKinds, DataKinds #-}
9 {-# LANGUAGE AllowAmbiguousTypes #-}
10 {-# LANGUAGE NoImplicitPrelude #-}
11 {-# LANGUAGE GADTs #-}
12
13 module Data.List.Unrolled where
14
15 import GHC.TypeLits
16
17 -- | Drop @n@ elements from a list
18 class Drop (n :: Nat) where
19 drop :: [a] -> [a]
20
21 instance {-# OVERLAPPING #-} Drop 0 where
22 drop xs = xs
23 {-# INLINE drop #-}
24
25 instance {-# OVERLAPPABLE #-} (Drop (n - 1)) => Drop n where
26 drop [] = []
27 drop (_ : xs) = drop @(n - 1) xs
28 {-# INLINE drop #-}
29
30 -- | Take @n@ elements from a list
31 class Take (n :: Nat) where
32 take :: [a] -> [a]
33
34 instance {-# OVERLAPPING #-} Take 0 where
35 take _ = []
36 {-# INLINE take #-}
37
38 instance {-# OVERLAPPABLE #-} (Take (n - 1)) => Take n where
39 take [] = []
40 take (x : xs) = x : take @(n - 1) xs
41 {-# INLINE take #-}
42
43 -- | Split list at @n@-th element.
44 splitAt :: forall (n :: Nat) a. (Take n, Drop n) => [a] -> ([a], [a])
45 splitAt xs = (take @n xs, drop @n xs)
46
47 -- | Split list into chunks of the given length @c@. @n@ is length of the list.
48 class ChunksOf (n :: Nat) (c :: Nat) where
49 chunksOf :: [a] -> [[a]]
50
51 instance {-# OVERLAPPING #-} ChunksOf 0 0 where
52 chunksOf _ = []
53 {-# INLINE chunksOf #-}
54
55 instance {-# OVERLAPPABLE #-} ChunksOf 0 c where
56 chunksOf _ = []
57 {-# INLINE chunksOf #-}
58
59 instance {-# OVERLAPPABLE #-} ChunksOf n 0 where
60 chunksOf _ = []
61 {-# INLINE chunksOf #-}
62
63
64 instance {-# OVERLAPPABLE #-} (Take c, Drop c, ChunksOf (n - 1) c) => ChunksOf n c where
65 chunksOf xs =
66 let (l, r) = splitAt @c xs
67 in l : chunksOf @(n - 1) @c r
68 {-# INLINE chunksOf #-}