haskell

Does GHC use dynamic dispatch with existential types?

让人想犯罪 __ 提交于 2021-01-27 09:54:58
问题 Does the following bit of code use dynamic dispatch as it's understood in C++ or Java? As I understand, at the last line, compiler cannot possibly know at compile time which implementation of (==) to call, yet the code compiles and produces correct results. Can someone please explain, what kind of implementation (vptr, for example) lies behind this? {-# LANGUAGE ExistentialQuantification #-} data Value = A Int data ForallFunc = forall a. Eq a => Forall (Value -> a) unpackA (A int) = int

GHC type error which I do not understand

不羁的心 提交于 2021-01-27 07:55:18
问题 I am teaching myself Haskell. I want to write a function that recursively finds the first number that has an integer square root and is smaller than a starting number. It looks like this: findFirstSquare :: Int -> Int findFirstSquare x | x <= 0 = error "This function only works for 1 or above" | fromInteger(floor(sqrt(x))) == (sqrt x) = x | otherwise = intSqrt(x - 1) But GHC complains: No instance for (RealFrac Int) arising from a use of `floor' at ... However, if I type the following into

GHC type error which I do not understand

孤人 提交于 2021-01-27 07:53:02
问题 I am teaching myself Haskell. I want to write a function that recursively finds the first number that has an integer square root and is smaller than a starting number. It looks like this: findFirstSquare :: Int -> Int findFirstSquare x | x <= 0 = error "This function only works for 1 or above" | fromInteger(floor(sqrt(x))) == (sqrt x) = x | otherwise = intSqrt(x - 1) But GHC complains: No instance for (RealFrac Int) arising from a use of `floor' at ... However, if I type the following into

Type-safe `read` in Haskell

故事扮演 提交于 2021-01-27 07:43:33
问题 Learn You a Haskell discusses the following data type: data Day = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday deriving (Eq, Ord, Show, Read, Bounded, Enum) The book demonstrates how to use read to parse a String into a Day type. $ read "Saturday" :: Day Saturday However, I can pass in a non-Day value, resulting in an exception. $ read "foo" :: Day *** Exception: Prelude.read: no parse What's a type-safe way to use read in the above example? 回答1: In addition to the old

Type-safe `read` in Haskell

◇◆丶佛笑我妖孽 提交于 2021-01-27 07:42:25
问题 Learn You a Haskell discusses the following data type: data Day = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday deriving (Eq, Ord, Show, Read, Bounded, Enum) The book demonstrates how to use read to parse a String into a Day type. $ read "Saturday" :: Day Saturday However, I can pass in a non-Day value, resulting in an exception. $ read "foo" :: Day *** Exception: Prelude.read: no parse What's a type-safe way to use read in the above example? 回答1: In addition to the old

Build dependency in library or executable section of cabal file?

你。 提交于 2021-01-27 07:42:11
问题 First off, I'm new to using cabal and external packages with Haskell. I'm trying to use the Graphics.Gloss package inside of MyLib. I can make it work if I include gloss in both the build-depends of library and executable . Here is the relevant portion of the cabal file: library exposed-modules: MyLib build-depends: base ^>=4.13.0.0, gloss ^>=1.13.1.1 default-language: Haskell2010 executable ray-tracer main-is: Main.hs other-modules: MyLib build-depends: base ^>=4.13.0.0, ray-tracer, haskell

Build dependency in library or executable section of cabal file?

百般思念 提交于 2021-01-27 07:35:22
问题 First off, I'm new to using cabal and external packages with Haskell. I'm trying to use the Graphics.Gloss package inside of MyLib. I can make it work if I include gloss in both the build-depends of library and executable . Here is the relevant portion of the cabal file: library exposed-modules: MyLib build-depends: base ^>=4.13.0.0, gloss ^>=1.13.1.1 default-language: Haskell2010 executable ray-tracer main-is: Main.hs other-modules: MyLib build-depends: base ^>=4.13.0.0, ray-tracer, haskell

Haskell/GHC: Matching multiple unary constructors with the same pattern

主宰稳场 提交于 2021-01-27 06:53:33
问题 So I was playing around with defining a TrieSet datatype (even though I know I don't need to): module Temp where import Data.Map data TrieSet a = Nonterminal (Data.Map a (TrieSet a)) | Terminal (Data.Map a (TrieSet a)) insert :: Ord a => [a] -> TrieSet a -> TrieSet a insert [] (_ m) = Terminal m insert (a:as) (c m) = c $ insertWith (insert as . flip const) a (insert as $ Nonterminal empty) m When I got an error I've never seen before: % ghc -c Temp.hs Temp.hs:8:11: Parse error in pattern So

ScopedTypeVariables fail to work with nested where-clauses?

烂漫一生 提交于 2021-01-27 06:51:29
问题 It's a horribly contrived example, but anyway... this typechecks: newtype Foo c = Foo { runFoo :: c -> Bool } newtype Bar c = Bar { runBar :: Int -> c } foo :: Eq c => Bar c -> (c -> [c]) -> Bar (Foo c) foo bar f = Bar res where res n = Foo judge where judge c = (c`elem`) . f $ runBar bar n and works GHCi> let foo0 = foo (Bar id) (\n -> [n, n*2]) GHCi> map (runFoo $ runBar foo0 4) [1..10] [False,False,False,True,False,False,False,True,False,False] but if I add the obvious type signature to

How to install an older version of base in Haskell

爷,独闯天下 提交于 2021-01-27 06:41:31
问题 I have installed Haskell platform and I have the 7.10.3 version of ghci, which has the 4.8.2.0 version of base. I need to install gloss-1.8.* which needs base-4.7.* version of base. My question is how to install this older version now, when I already have the newer version. Is it possible? Or do I have to uninstall the Haskell platform and install an older version? 来源: https://stackoverflow.com/questions/37195188/how-to-install-an-older-version-of-base-in-haskell