typeclass

Haskell Typeclass shorthand

So, I have a pair of typeclasses that I'll be using a lot together, and I want to avoid specifying both each time. Basically, instead of putting :: (Ord a, Fractional a, Ord b, Fractional b, ... Ord z, Fractional z) => at the beginning of all my type specifications, I'd rather put :: (OrdFractional a, OrdFractional b, ... OrdFractio...

F# functions with generic parameter types

Hello, I am trying to figure out how to define a function that works on multiple types of parameters (e.g. int and int64). As I understand it, function overloading is not possible in F# (certainly the compiler complains). Take for example the following function. let sqrt_int = function | n:int -> int (sqrt (float n)) | n:int6...

How does Haskell know which typeclass instance you mean?

This question arose while reading the new chapter in the excellent Learn You a Haskell about applicative functors. The Applicative typeclass has, as part of the definition for the Maybe instance: pure = Just If I just go to GHCi and import Control.Applicative, and do: pure (3+) I don't get Just anything (makes sense). But if I use...

Haskell: Creating Type Classes for Zippers

So I've been reading a bit about the Zipper pattern in Haskell (and other functional languages, I suppose) to traverse and modify a data structure, and I thought that this would be a good chance for me to hone my skills at creating type classes in Haskell, since the class could present a common traversal interface for me to write code to...

specialization in type classes using ghc

How can I make the genOut/String fire? module IOStream where import System.IO import System.IO.Unsafe class Out a where out :: a → String instance Show a ⇒ Out a where out = show outString :: String → String outString = id {-# RULES "genOut/String" out = outString #-} infixl 9 <<, ≪ (≪), (<<) :: Out a ⇒ IO Handle → a → IO Handl...

Haskell: type classes question

I wish to define the following typeclass Mapping: {-# LANGUAGE MultiParamTypeClasses #-} class Mapping k v m where empty :: m v insert :: k -> v -> m v -> m v search :: k -> m v -> Maybe v delete :: k -> m v -> m v One instance of Mapping is Data.Map.Map {-# LANGUAGE ..., FlexibleInstances #-} instance Ord k => Mapping k v ...

Haskell: Overlapping instances

Consider the following example program: next :: Int -> Int next i | 0 == m2 = d2 | otherwise = 3 * i + 1 where (d2, m2) = i `divMod` 2 loopIteration :: MaybeT (StateT Int IO) () loopIteration = do i <- get guard $ i > 1 liftIO $ print i modify next main :: IO () main = do (`runStateT` 31) . runMaybeT . forever $ lo...

Type classes in Haskell data types

In Haskell, one can define a data type like so: data Point1 = Point1 { x :: Integer , y :: Integer } Can one use type classes for variables inside a data type? If so how? I realize it is possible to do this as an algebraic data type, with a different definition for each kind of point, but I'm wondering if there...

Haskell: Algebric data types whose type variables need to be an instance of a typeclass

I am trying to define an algebric type: data MyType t = MyType t And make it an instance of Show: instance Show (MyType t) where show (MyType x) = "MyType: " ++ (show x) GHC complains becasue it cannot deduce that type 't' in 'Show (MyType t)' is actually an instance of Show, which is needed for (show x). I have no idea where an...

Is there an encyclopedia/list of common type classes in Haskell?

Is there any website that lists and describes common type classes in Haskell? ...

Operate on values within structurally similar types in Haskell

Excuse me for my extremely limited Haskell-fu. I have a series of data types, defined in different modules, that are structured the same way: -- in module Foo data Foo = Foo [Param] -- in module Bar data Bar = Bar [Param] -- * many more elsewhere I'd like to have a set of functions that operate on the list of params, eg to add and ...

Can liftM differ from liftA?

According to the Typeclassopedia (among other sources), Applicative logically belongs between Monad and Pointed (and thus Functor) in the type class hierarchy, so we would ideally have something like this if the Haskell prelude were written today: class Functor f where fmap :: (a -> b) -> f a -> f b class Functor f => Pointed f whe...

What's wrong with type classes?

Type classes seem to be a great possibility to write generic and reusable functions in a very consistent, efficient and extensible way. But still no "mainstream-language" provides them - On the contrary: Concepts, which are a quite analogical idea, have been excluded from the next C++! What's the reasoning against typeclasses? Apparentl...

Haskell dividing num

I'm having an issue I want to learn more about, and how to avoid. I've got this code len :: (Num r ) => [a] -> r len [] = 0 len xs = 1 + len ( tail xs ) avg :: (Num t) => [t] -> Double avg xs = ( sum xs ) / ( len xs ) Which renders the following error len.hs:6:9: Couldn't match expected type `Double' against inferred type `t' ...

Haskell typeclass

I have a Haskell typeclass question. I can't munge the syntax to get this (seemingly reasonable) program to compile under GHC. import Control.Concurrent.MVar blah1 :: [a] -> IO ([a]) blah1 = return blah2 :: [a] -> IO (MVar [a]) blah2 = newMVar class Blah b where blah :: [a] -> IO (b a) instance Blah [] where blah = blah1 -- BO...

What is the effect of type synonyms on instances of type classes? What does the TypeSynonymInstances pragma in GHC do?

I'm reading Real World Haskell Pg 151, and I've stared at the following passage for over an hour: Recall that String is a synonym for [Char], which in turn is the type [a] where Char is substituted for the type parameter a. According to Haskell 98's rules, we are not allowed to supply a type in place of a type parameter whe...

Why does Haskell stop short of inferring the datatype's typeclasses in the function signatures?

Firstly, this question isn't 100% specific to Haskell, feel free to comment on the general design of typeclasses, interfaces and types. I'm reading LYAH - creating types and typeclasses The following is the passage that I'm looking for more information on: Data (Ord k) => Map k v = ... However, it's a very strong convention in...

STUArray with polymorphic type

I want to implement an algorithm using the ST monad and STUArrays, and I want it to be able to work with both Float and Double data. I'll demonstrate on a simpler example problem: calculating a memoized scanl (+) 0 (I know it can be solved without STUArray, just using as example). {-# LANGUAGE FlexibleContexts, ScopedTypeVariables #-} ...

Is there a way to implement constraints in Haskell's type classes?

Is there some way (any way) to implement constraints in type classes? As an example of what I'm talking about, suppose I want to implement a Group as a type class. So a type would be a group if there are three functions: class Group a where product :: a -> a -> a inverse :: a -> a identity :: a But those are not any fu...

Manipulating the order of arguments to type constructors

I wrote something like this: instance Functor (Either e) where fmap _ (Left a) = Left a fmap f (Right b) = Right (f b) How do I do the same if I want fmap to change the value only if it's Left? I mean, what syntax do I use to indicate that I use type Either _ b instead of Either a _? ...