summaryrefslogtreecommitdiff
path: root/testsuite/tests/simplCore/should_compile/T21689a.hs
blob: e95f4343d5b07a6c72e69081ba462ba453e6d68a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
{-# LANGUAGE ConstraintKinds       #-}
{-# LANGUAGE DataKinds             #-}
{-# LANGUAGE DeriveDataTypeable    #-}
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds             #-}
{-# LANGUAGE RankNTypes            #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE TypeFamilies          #-}
{-# LANGUAGE TypeOperators         #-}
{-# LANGUAGE UndecidableInstances  #-}
module T21689a
  ( Arity
  , Dim
  , DimM
  , IVector
  , Mutable
  , MVector
  , Vector(..)
  , constructVec
  , inspectVec
  , gfoldl'
  , gunfold'
  ) where

import Control.Monad.ST (ST, runST)
import Data.Coerce (coerce)
import Data.Data (Data)
import Data.Functor.Const (Const(..))
import Data.Kind (Type)
import GHC.TypeLits (KnownNat, Nat, type (+), type (-))

-----
-- Data.Vector.Fixed.Cont
-----

data PeanoNum = Z
              | S PeanoNum

type family Peano (n :: Nat) :: PeanoNum where
  Peano 0 = 'Z
  Peano n = 'S (Peano (n - 1))

type family Fn (n :: PeanoNum) (a :: Type) (b :: Type) where
  Fn 'Z     a b = b
  Fn ('S n) a b = a -> Fn n a b

newtype Fun n a b = Fun { unFun :: Fn n a b }

type family Dim (v :: Type -> Type) :: Nat

class Arity (Dim v) => Vector v a where
  construct :: Fun (Peano (Dim v)) a (v a)

  inspect   :: v a -> Fun (Peano (Dim v)) a b -> b

type Arity n = ( ArityPeano (Peano n)
               , KnownNat n
               , Peano (n+1) ~ 'S (Peano n)
               )

class ArityPeano n where
  accum :: (forall k. t ('S k) -> a -> t k)
        -> (t 'Z -> b)
        -> t n
        -> Fun n a b

  applyFun :: (forall k. t ('S k) -> (a, t k))
           -> t n
           -> (CVecPeano n a, t 'Z)

  gunfoldF :: (Data a)
           => (forall b x. Data b => c (b -> x) -> c x)
           -> T_gunfold c r a n -> c r

newtype T_gunfold c r a n = T_gunfold (c (Fn n a r))

gfoldl' :: forall c v a. (Vector v a, Data a)
        => (forall x y. Data x => c (x -> y) -> x -> c y)
        -> (forall x  . x -> c x)
        -> v a -> c (v a)
gfoldl' f inj v
  = inspect v
  $ gfoldlF f (inj $ unFun (construct :: Fun (Peano (Dim v)) a (v a)))

gunfold' :: forall con c v a. (Vector v a, Data a)
         => (forall b r. Data b => c (b -> r) -> c r)
         -> (forall r. r -> c r)
         -> con -> c (v a)
gunfold' f inj _
  = gunfoldF f gun
  where
    con = construct                   :: Fun (Peano (Dim v)) a (v a)
    gun = T_gunfold (inj $ unFun con) :: T_gunfold c (v a) a (Peano (Dim v))

gfoldlF :: (ArityPeano n, Data a)
        => (forall x y. Data x => c (x -> y) -> x -> c y)
        -> c (Fn n a r) -> Fun n a (c r)
gfoldlF f c0 = accum
  (\(T_gfoldl c) x -> T_gfoldl (f c x))
  (\(T_gfoldl c)   -> c)
  (T_gfoldl   c0)

newtype T_gfoldl c r a n = T_gfoldl (c (Fn n a r))

newtype ContVec n a = ContVec (forall r. Fun (Peano n) a r -> r)

type instance Dim (ContVec n) = n

instance Arity n => Vector (ContVec n) a where
  construct = accum
    (\(T_mkN f) a -> T_mkN (f . consPeano a))
    (\(T_mkN f)   -> toContVec $ f (CVecPeano unFun))
    (T_mkN id)
  inspect (ContVec c) f = c f
  {-# INLINE construct #-}
  {-# INLINE inspect   #-}

newtype T_mkN n_tot a n = T_mkN (CVecPeano n a -> CVecPeano n_tot a)

toContVec :: CVecPeano (Peano n) a -> ContVec n a
toContVec = coerce

newtype CVecPeano n a = CVecPeano (forall r. Fun n a r -> r)

consPeano :: a -> CVecPeano n a -> CVecPeano ('S n) a
consPeano a (CVecPeano cont) = CVecPeano $ \f -> cont $ curryFirst f a
{-# INLINE consPeano #-}

curryFirst :: Fun ('S n) a b -> a -> Fun n a b
curryFirst = coerce
{-# INLINE curryFirst #-}

apply :: Arity n
      => (forall k. t ('S k) -> (a, t k))
      -> t (Peano n)
      -> ContVec n a
{-# INLINE apply #-}
apply step' z = toContVec $ fst (applyFun step' z)

-----
-- Data.Vector.Fixed.Mutable
-----

type family Mutable (v :: Type -> Type) :: Type -> Type -> Type

type family DimM (v :: Type -> Type -> Type) :: Nat

class (Arity (DimM v)) => MVector v a where
  new   :: PrimMonad m => m (v (PrimState m) a)

  unsafeWrite :: PrimMonad m => v (PrimState m) a -> Int -> a -> m ()

class (Dim v ~ DimM (Mutable v), MVector (Mutable v) a) => IVector v a where
  unsafeFreeze :: PrimMonad m => Mutable v (PrimState m) a -> m (v a)

  unsafeIndex :: v a -> Int -> a

inspectVec :: forall v a b. (Arity (Dim v), IVector v a) => v a -> Fun (Peano (Dim v)) a b -> b
{-# INLINE inspectVec #-}
inspectVec v
  = inspect cv
  where
    cv :: ContVec (Dim v) a
    cv = apply (\(Const i) -> (unsafeIndex v i, Const (i+1)))
               (Const 0 :: Const Int (Peano (Dim v)))

constructVec :: forall v a. (Arity (Dim v), IVector v a) => Fun (Peano (Dim v)) a (v a)
{-# INLINE constructVec #-}
constructVec =
  accum step
        (\(T_new _ st) -> runST $ unsafeFreeze =<< st :: v a)
        (T_new 0 new :: T_new v a (Peano (Dim v)))

data T_new v a n = T_new Int (forall s. ST s (Mutable v s a))

step :: (IVector v a) => T_new v a ('S n) -> a -> T_new v a n
step (T_new i st) x = T_new (i+1) $ do
  mv <- st
  unsafeWrite mv i x
  return mv

-----
-- Control.Monad.Primitive
-----

class Monad m => PrimMonad m where
  type PrimState m

instance PrimMonad (ST s) where
  type PrimState (ST s) = s