typeclass

"Ambigous type variable" error when defining custom "read" function

While trying to compile the following code, which is enhanced version of read build on readMay from Safe package. readI :: (Typeable a, Read a) => String -> a readI str = case readMay str of Just x -> x Nothing -> error ("Prelude.read failed, expected type: " ++ (show (typ...

What's the "|" for in a Haskell class definition?

I can't figure out what the "| m -> w"-part means in a class definition like this: class (Monoid w, Monad m) => MonadWriter w m | m -> w What additional information does this add to the class definition? ...

How can I make a Maybe-Transformer MaybeT into an instance of MonadWriter?

I am trying to build a MaybeT-Transformer Monad, based on the example in the Real World Haskell, Chapter Monad Transformers: data MaybeT m a = MaybeT { runMT :: m (Maybe a) } instance (Monad m) => Monad (MaybeT m) where m >>= f = MaybeT $ do a <- runMT m case a of Just x -> runMT (...

Haskell: Defaulting constraints to type

Consider this example: applyKTimes :: Integral i => i -> (a -> a) -> a -> a applyKTimes 0 _ x = x applyKTimes k f x = applyKTimes (k-1) f (f x) applyThrice :: (a -> a) -> a -> a applyThrice = applyKTimes 3 The 3 in applyThrice is defaulted by GHC to an Integer as shown when compiling with -Wall: Warning: Defaulting the following con...

Linking/Combining Type Classes in Haskell

Say I have two type classes defined as follows that are identical in function but different in names: class Monad m where (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a class PhantomMonad p where pbind :: p a -> (a -> p b) -> p b preturn :: a -> p b Is there a way to tie these two classes together so someth...

Haskell's TypeClasses and Go's Interfaces

What are the similarities and the differences between Haskell's TypeClasses and Go's Interfaces? What are the relative merits / demerits of the two approaches? ...

Orphaned instances in Haskell

When compiling my Haskell application with the -Wall option, GHC complains about orphaned instances, for example: Publisher.hs:45:9: Warning: orphan instance: instance ToSElem Result The type class ToSElem is not mine, it's defined by HStringTemplate. Now I know how to fix this (move the instance declaration into the module where...

Using haskell read and typeclasses - ambiguous type variable error

Hi all, I have an ambiguous type variable error on the definition of "trial" below, I am wondering if there is anything that can be done to make this situation work? I want to really just deal with instances and not explicit data types (such as the MO1, MO2 included below). module Tc102 where class (Show a, Read a) => MyObj a where ...

Difference between Scala trait and C++ concepts

What is the difference between Scala traits Haskell type class and C++0x Concepts? ...

[Scala] "can't existentially abstract over parameterized type..."

I was messing around with Scala 2.8 for fun and trying to define a pimp which adds an "as" method to type constructors, allowing to convert from one functor to another (please overlook the fact that I'm not necessarily dealing with functors here). So for instance, you could use it like this: val array:Array[T] val list:List[T] = array.a...

Modelling Typeclasses in C++

Is it possible to implement Haskell typeclasses in C++? If yes, then how? ...

How do typeclasses and modules interact?

Hi, In order to grasp better typeclasses (starting pretty much form scratch) I had a go at modelling 2-D shapes with area calculations, like this: module TwoDShapes where class TwoDShape s where area :: s -> Float data Circle = Circle Float deriving Show aCircle radius | radius < 0 = error "circle radius must be non-negative" ...

Haskell typeclass inheritance and parametric typeclass

Hi! I wish to say that a certain parametrized monad st works with a regular memory, but a subclass of my parametrized monad should impose an additional constraint on the type of memory. In code: class Memory m where ... class State st where unit :: Memory m => a -> st m m a bind :: (Memory m1, Memory m2, Memory m3) => st m1...

Implicit type parameters in Haskell class definition?

It normally seems the following is illegal: class Foo a where foo :: a -> b -> a Which makes sense; how do we know what b is? However, if we look at Functor's definition: class Functor f where fmap :: (a -> b) -> f a -> f b we see a and b showing up even though we only specify f as a type variable. I'm guessing this is all...

Scala: reconciling type classes with dependency injection

There seems to be a lot of enthusiasm among Scala bloggers lately for the type classes pattern, in which a simple class has functionality added to it by an additional class conforming to some trait or pattern. As a vastly oversimplified example, the simple class: case class Wotsit (value: Int) can be adapted to the Foo trait: trait F...

Scala double definition (2 methods have the same type erasure)

Hi I wrote this in scala and it won't compile: class TestDoubleDef{ def foo(p:List[String]) = {} def foo(p:List[Int]) = {} } the compiler notify: [error] double definition: [error] method foo:(List[String])Unit and [error] method foo:(List[Int])Unit at line 120 [error] have same type after erasure: (List)Unit I know JVM has no...

Automatic conversion of types for FFI calls in Haskell

I have defined the following module to help me with FFI function export: {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, TypeSynonymInstances #-} module ExportFFI where import Foreign import Foreign.C class FFI basic ffitype | basic -> ffitype where toFFI :: basic -> IO ffitype fromFFI :: ffitype -> IO basic f...

List of custom type and instance of class

Hi, I'm playing around in Haskell to try and get the hang of it. I'm running into problems with my typeclasses. What I'm trying to do is to make a generalized a* module by defining classes and methods and then I'm trying to use those in a program. My problem is that when I try to make a list of my Box data types an instance of my Board ...

polymorphic instances for typeclasses in Mercury language

Consider next declaration: :- type wrap(T) ---> wrap(T). :- inst wrap(I) ---> wrap(I). :- typeclass infer_wrap(A, B) <= ((A -> B)). :- instance infer_wrap(A, wrap(A)). Mercury (10.04) produces: monad.m:011: In instance declaration for `monad.infer_wrap(A, monad.wrap(A))': monad.m:011: the first arg is a type variable Even without...

alternative to typeclasses?

haskell programmer. using F#. no typeclasses in F#. what to use when I need typeclasses? ...