Haskell type types and dummy arguments

I made a function similar to numpy array. It converts lists to arrays, lists of lists to 2d arrays, etc.

It works as follows:

ghci> arrFromNestedLists ["hello", "world"] :: Array (Int, (Int, ())) Char
array ((0,(0,())),(1,(4,()))) [((0,(0,())),'h'),((0,(1,())),'e'),((0,(2,())),'l'),((0,(3,())),'l'),((0,(4,())),'o'),((1,(0,())),'w'),((1,(1,())),'o'),((1,(2,())),'r'),((1,(3,())),'l'),((1,(4,())),'d')]

(Int, (Int, ())), but not (Int, Int)because I don’t know about a programmatic way to increase the length of a tuple. (side question: is there such a way?)

Encoding this was inconvenient, and I had to make a "workaround" (going around dummy arguments to functions) for it to work. I wonder if there is a better way.

So here is the code interrupted by the details of ugly workarounds:

{-# LANGUAGE FlexibleInstances, ScopedTypeVariables, TypeFamilies #-}

type family ListOfIndex i a
type instance ListOfIndex () a = a
type instance ListOfIndex (Int, i) a = [ListOfIndex i a]

class Ix i => ArrConv i where
  acBounds :: a -> ListOfIndex i a -> (i, i)
  acFlatten :: i -> ListOfIndex i a -> [a]

acBounds"must be :: ListOfIndex i a -> (i, i). " And similarly for acFlatten. Each is given a dummy variable ( undefinedalways a given value), because otherwise I could not compile it: (

arrFromNestedLists :: forall i a. ArrConv i => ListOfIndex i a -> Array i a
arrFromNestedLists lst =
  listArray
  (acBounds (undefined :: a) lst)
  (acFlatten (undefined :: i) lst)

dummy undefined. GHC, ListOfIndex .

instance ArrConv () where
  acBounds _ = const ((), ())
  acFlatten _ = (: [])

acBounds ArrConv ​​ , ScopedTypeVariables, , ..

acSucBounds
  :: forall a i. ArrConv i
  => a -> [ListOfIndex i a] -> ((Int, i), (Int, i))
acSucBounds _ lst =
  ((0, inStart), (length lst - 1, inEnd))
  where
    (inStart, inEnd) = acBounds (undefined :: a) (head lst)

instance ArrConv i => ArrConv (Int, i) where
  acBounds = acSucBounds
  acFlatten _ = concatMap (acFlatten (undefined :: i))
+5
2

, acBounds acFlatten, , a i ListOfIndex i a -> (i, i) ListOfIndex i a -> [a] . acArgs ListOfIndex i a -> ((i, i), a). , (Int, i) , typechecker , , (, fst . acArgs).

{-# LANGUAGE TypeFamilies, FlexibleInstances #-}

import Data.Array

type family ListOfIndex i a
type instance ListOfIndex () a = a
type instance ListOfIndex (Int, i) a = [ListOfIndex i a]

class Ix i => ArrConv i where
  acArgs :: ListOfIndex i a -> ((i, i), [a])

instance ArrConv () where
  acArgs x = (((), ()), [x])

instance ArrConv i => ArrConv (Int, i) where
  acArgs lst =
    (((0, inStart), (length lst - 1, inEnd)), args >>= snd)
    where
      args = map acArgs lst
      (inStart, inEnd) = fst (head args)

arrFromNestedLists :: ArrConv i => ListOfIndex i a -> Array i a
arrFromNestedLists = uncurry listArray . acArgs
+4

acBounds acFlatten, , acBounds acBounds :: Proxy a -> ListOfIndex i a -> (i, i). undefined, (Proxy :: SomeConcreteType) ; acBounds - , ( ) .

0

All Articles