1
+ {-# LANGUAGE Trustworthy #-}
2
+ {-# LANGUAGE RankNTypes #-}
3
+ {-# LANGUAGE ScopedTypeVariables #-}
4
+
1
5
module Foreign.Matlab.Internal (
2
6
CBool , boolC , cBool ,
3
7
MIO ,
@@ -22,12 +26,17 @@ module Foreign.Matlab.Internal (
22
26
MAny , MAnyArray ,
23
27
MNull , mNullArray , isMNull ,
24
28
MCell (.. ),
25
- MStruct (.. ),
29
+ MStruct (.. ), mStruct ,
26
30
MXFun , MFun ,
27
31
MWSize , MWIndex , MWSignedIndex
28
32
) where
29
33
34
+ import Data.Coerce (Coercible , coerce )
30
35
import qualified Data.Map.Strict as DM
36
+ import Data.Profunctor
37
+ import Data.Profunctor.Unsafe
38
+
39
+
31
40
import Foreign
32
41
import Foreign.C.Types
33
42
import qualified Data.Char
@@ -37,7 +46,23 @@ import Foreign.Matlab.Util
37
46
38
47
type MIO a = IO a
39
48
40
- -- type CBool = #type bool
49
+
50
+ -- Lens types copied in --
51
+
52
+ type Iso s t a b = forall p f . (Profunctor p , Functor f ) = > p a (f b ) -> p s (f t )
53
+ type Iso' s a = Iso s s a a
54
+
55
+ coerce' :: forall a b . Coercible a b => b -> a
56
+ coerce' = coerce (id :: a -> a )
57
+ {-# INLINE coerce' #-}
58
+
59
+ coerced :: forall s t a b . (Coercible s a , Coercible t b ) => Iso s t a b
60
+ # if __GLASGOW_HASKELL__ >= 710
61
+ coerced l = rmap (fmap coerce') l .# coerce
62
+ # else
63
+ coerced l = case sym Coercion :: Coercion a s of
64
+ Coercion -> rmap (fmap coerce') l .# coerce
65
+ # endif
41
66
42
67
boolC :: CBool -> Bool
43
68
boolC = (0 /= )
@@ -194,12 +219,15 @@ instance MType MCell MCell where
194
219
mxClassOf _ = MXClassCell
195
220
196
221
-- |A single struct in an array, represented by an (ordered) list of key-value pairs
197
- newtype MStruct = MStruct { mStruct :: DM.Map String MAnyArray }
222
+ newtype MStruct = MStruct { _mStruct :: DM.Map String MAnyArray }
198
223
instance MType MStruct MStruct where
199
224
hs2mx = id
200
225
mx2hs = id
201
226
mxClassOf _ = MXClassStruct
202
227
228
+ mStruct :: Iso' MStruct (DM.Map String MAnyArray)
229
+ mStruct = coerced
230
+
203
231
type MXFun = CInt -> Ptr MXArrayPtr -> CInt -> Ptr MXArrayPtr -> IO ()
204
232
-- |A Matlab function
205
233
type MFun =
0 commit comments