how to resolve use of operators in type declaratio

2019-09-10 19:35发布

问题:

I am playing around with Repa, and the code below can compile and run.

import qualified Data.Array.Repa as R
--t:: R.Array R.U (R.Z R.:. Int) Float 
--t =  R.fromListUnboxed (R.Z R.:. (10::Int)) ([1.0..10]::[Float])

main = do 
   let x = R.fromListUnboxed (R.Z R.:. (10::Int)) ([1.0..10]::[Float])
   print x

I believe (from check in ghci) that x has the type signature that I have declared t to have, but I get this error if I uncomment everything associated with t:

Illegal operator ‘R.:.’ in type ‘R.Z R.:. Int’
  Use TypeOperators to allow operators in types

what is the correct way to resolve the use of an type operator/constructor in type declarations? (I will google some more, but would like to ask anyway too learn more)

回答1:

You can use the command line option -XTypeOperators to either ghc or ghci, e.g.:

ghci -XTypeOperators ...

Or you can use the OPTIONS_GHC pragma in your source file:

{-# OPTIONS_GHC -XTypeOperators #-}

I'm not sure why LANGUAGE TypeOperators is not recognized.

or the LANGUAGE pragma:

{-# LANGUAGE TypeOperators #-}

(make sure to spell it correctly.)

In this case the type of t is inferable, so you can use :t in ghci:

$ ghci
Prelude> import qualified Data.Array.Repa as R
Prelude R> let t =  R.fromListUnboxed (R.Z R.:. (10::Int)) ([1.0..10]::[Float])
Prelude R> :t t
t :: R.Array R.U (R.Z R.:. Int) Float

Note: I'm using GHC 7.10.2.



标签: haskell repa