Questions tagged [typeclass]

Type classes in Haskell are a language mechanism to support ad hoc polymorphism. They also exist in Scala, Coq, and Isabelle.

2
votes
2answers
52 views

Problem with creating a type class for a heterogenous operation

What follows is a very simplified version of what I'm trying to do.Suppose I want to create a generic difference operation that can accept operands of different types.class Diff a b c where...
4
votes
2answers
95 views

Is the Haskell type class 'Real' a misnomer?

I am trying to understand the numeric type class hierarchy in Haskell. The basic numeric type isclass Num a where...As a side note, according to some of my sources (slides), it should actually be...
4
votes
1answer
47 views

Recursive use of typeclass methods in Coq

Is there a way to use recursion with Coq's typeclasses? Like for e.g., in defining show for lists, if you want to call the show function for lists recursively, then you will have to use a fixpoint ...
0
votes
2answers
118 views

Semigroup with function in Scala

I'm trying to convert an Haskell Semigroup to Scala. The Haskell code works fine but I can't write it in ScalaHaskell:import Data.Semigroupnewtype Combine a b=Combine { unCombine :: (a -> b)...
4
votes
1answer
50 views

How to use path-dependent types with type classes in Scala

I’m having some problems with path dependent types.I have some types Foo with an abstract type member F. Instances such as Bar will provide the concrete type.Then there is a type class Baz. I have ...
2
votes
0answers
54 views

Easiest way to extend GHC with new deriving capabilities?

I would like to teach GHC to derive new typeclasses. Is it possible to do this externally without using Generic or TemplateHaskell? Like, with source plugins or something similar. So I can write:...
0
votes
1answer
28 views

Non type-variable argument in the constraint for Arbitrary typeclass

For an exercise in Chapter 15 of Haskell Programming From First Principles, I'm trying to write an Arbitrary instance based on another Arbitrary instance:module AccumulateRight whereimport Data....
0
votes
0answers
127 views

Why does the Functor class in Haskell not include a function on objects? Is `pure` that function? [duplicate]

In Category theory, it is very conspicuous that a definition of a functor should include two functions: on objects and on arrows. However, the usual Haskell Prelude.Functor does not make any mention ...
0
votes
1answer
53 views

How to write a type instance for a compound type having existentially quantified type variables?

I have a typeclass with an associated type:class Foo a wheretype Bar a :: *...Now, I'd like to instance this class for a compound type containing existentially quantified type variables:...
1
vote
1answer
37 views

How to handle a Typeclass Instance requiring additional Type Constraints

I found myself in a situation where a typeclass instance I want to define requires additional type constraints. Specifically I want to define Show for a type Trie a:data Trie a=Node {label :: ...
6
votes
1answer
85 views

What does `~` (tilde) mean in an instance context, and why is it necessary to resolve overlap in some cases?

A complication.Consider the following snippet:class D u a where printD :: u -> a -> Stringinstance D a a where printD _ _="Same type ...
0
votes
1answer
43 views

Implicit classes and “not a member of type parameter” error

I have the following type class definition -trait ToBigInt[A] {def toBigInt(n: A): BigInt}object ToBigInt {def apply[A](implicit t: ToBigInt[A]): ToBigInt[A]=timplicit val ...
0
votes
0answers
46 views

Could not find implicit value for parameter defined in companion object

In the minimal type class example below, scala 2.12.6 / sbt 1.2.1 complains could not find implicit value for parameter tc: tryout.Tryout.TypeClassTrait[Int]. If I uncomment the println line, it ...
2
votes
1answer
105 views

Return `show a` if (Show a) exists, otherwise its type representation if (Typeable a)

I would like to writeclass Described a wheredescribe :: a -> Stringinstance {-# OVERLAPPING #-} (Show a)=> Described a wheredescribe=showinstance {-# OVERLAPPABLE #-} (Typeable a) ...
4
votes
1answer
222 views

Recursive transformation between nested case classes where the fields in the target are unaligned subsets of the source class

Given a pair of case classes, Source and Target, that have nested case classes, and at each level of nesting, the fields in Target are unaligned subsets of the ones in Source, is there a way to write ...
1
vote
1answer
97 views

Why passing a concrete type to function solves the error?

p.s: I wasn't sure how to name my question, fell free to let me know how I should have named it.If not specifying the concrete type, I get this error, which is very clear and easy to solve:...
1
vote
0answers
54 views

How to implement a collection of key, value pairs with mixed but restricted types for values

A use case here is to imagine a config object, that is a collection of key-value pairs. I would like a to create a type representing this collection of key, value pairs. And I'd also like to have ...
3
votes
1answer
407 views

If `zip` were a method of a lawful type class, of which then?

why the question   One may say that zip is a method of Applicative, the usual instance being ZipList. I am unhappy with it because it is unsafe. I am unhappy with Align too, because it is, by ...
1
vote
1answer
41 views

custom type define in haskell

Im new to haskell, I defined a custom list type.But when I try to define this function mymaximum. I notice that it only works if a is of type Num. what should I change if I want it to work with all ...
1
vote
1answer
51 views

Haskell typeclass constraint cannot be resolved due to Paterson's conditions

I'm trying to build an AST with indexed nested annotations. I added a typeclass for peeling the annotation at the top-level and tried to provide default instances that effectively says "if you know ...
3
votes
0answers
73 views

In GHC-8.2.2, can overlapping instance resolution depend on whether a file is included as an exposed module?

I'm encountering the following hard to understand behavior from GHC-8.2.2I have some overlapping typeclass instances. No incoherent typeclass instances. There's a certain typeclass instance of the ...
2
votes
1answer
77 views

Why can't we have Random class instances derived for enumerations in Haskell?

I wrote this today:data Door=A | B | Cderiving (Eq,Bounded,Enum)instance Random Door whererandomR (lo,hi) g=(toEnum i, g')where (i,g')=randomR (fromEnum lo, fromEnum hi) grandom=...
1
vote
2answers
38 views

Implicit lookup for Typeclass of None is not compatible with Contravariant Typeclass of Option

I don't get the following code to compile and I am curious of what I did wrong.I defined a Contravariant Jsonwriter Trait and a function accepting implicit writers:trait JsonWriter[-A] {def ...
52
votes
4answers
3k views

Can I define the Negatable interface in Java?

Asking this question to clarify my understanding of type classes and higher kinded types, I'm not looking for workarounds in Java.In Haskell, I could write something likeclass Negatable t where...
2
votes
1answer
105 views

Single-function typeclass naming in Haskell | “er” vs “has”

I find it sometimes hard to come up with reasonable single-function typeclass names. If the method name is a regular verb (like get, write, read...), it's easy to do the same thing Gophers do with ...
0
votes
0answers
36 views

Scala implicitly extend an interface

How can I make arbitrary type T to conform to trait Run without having inherit the trait? Type class works at method level but class is expecting type Run and I cannot change method signature of run() ...
0
votes
0answers
27 views

scala pureconfig sealed family slays the compiler in the REPL

I am following along with https://pureconfig.github.io/docs/overriding-behavior-for-sealed-families.htmlimport com.typesafe.config.ConfigFactoryimport pureconfig._sealed trait Seasoncase object ...
1
vote
1answer
33 views

Singleton Classes in Coq

I don't really understand singleton classes in Coq. I am currently looking at the following definition:Class Decision (P : Prop) :=decide : {P} + {¬P}.As far as I undestand this creates the ...
2
votes
1answer
156 views

Unified interface between functions of different number of arguments

I have two functions like these:foo :: a -> b -> x -> xbar :: c -> y -> yI would like to unify them into single interface so they both could have same name. In my case it's ...
1
vote
2answers
99 views

Returning a subset of types in Haskell

I'm trying to restrict the return type of a function to a subset of (kind) constructors. I can use typeclasses to restrict the input types, but when I try the same technique on return types as shown ...

153050per page
angop.ao, elkhabar.com, noa.al, afghanpaper.com, bbc.com, time.com, cdc.gov, nih.gov, xnxx.com, github.com,