Questions tagged [typeclass]
Type classes in Haskell are a language mechanism to support ad hoc polymorphism. They also exist in Scala, Coq, and Isabelle.
typeclass
1,981
questions
1
vote
3
answers
88
views
`type <nameOfType> = Typeclass` compiles, but `type <nameOfType> = (Typeclass, Typeclass)` doesn't
I've been reading about the renaming of pre-existing types using the haskell's type and, as much as I could understand, the type is only used for renaming concrete types, like Int, Integer, Char, ...
0
votes
0
answers
46
views
Type class in Scala with extension that overlaps in name with methods on type members
For a library I'm building in Scala 3 (current target 3.3), I am trying to construct a type class hierarchy to express various functionality I want to require from the types I'm using and introducing.
...
1
vote
1
answer
68
views
Using `opaque type` as a typeclass in Scala
Any idea why this does not compile: https://scastie.scala-lang.org/iPmLyim4Q8aQ2aMNIfj2gA
opaque type Global[Value] = Unit
object Global:
def derived[Value]: Global[Value] = ()
case class Yo() ...
1
vote
1
answer
89
views
How to write a Haskell function that can call "getField @k" on "obj" as well as "Maybe obj"
I'm trying to write HTML form helpers where I'd like the call-sites to support both of the following use-cases:
{-# LANGUAGE AllowAmbiguousTypes, DataKinds, OverloadedStrings #-}
import Data.Proxy
...
2
votes
1
answer
161
views
givens / implicits for Scala 3 Union Types
I have some typeclass, e.g.:
trait ExampleTypeClass[A]
// imagine one or more methods here
object ExampleTypeClass:
given ExampleTypeClass[Int] = new ExampleTypeClass{/* ... */}
given ...
2
votes
1
answer
52
views
Error in Haskell call to equation with expected type
I'm performing the following call to haskell method
rollD6 :: (RandomGen g, Int a) => Int -> ([a], g)
rollD6 times = roll Die6 times (mkStdGen 6)
rollD6InRange :: (RandomGen g, Int a) => ...
1
vote
0
answers
36
views
I use Scala 3.3.3 and would like to test a small program using import cats.kernel.laws.IsEq - I get this error: laws is not a member of cats.kernel
I am using Scala 3.3.3 and would like to test a small program using import cats.kernel.laws.IsEq, I get this error message: "laws" is not a member of cats.kernel and here is my
build.sbt:
...
0
votes
2
answers
62
views
Why I can´t pattern match in Haskell Typeclass?
I´m trying to roll an algebraic data type Die in Haskell defined with the following definition
data Die = Die6 | Die4 | Die20 | Die100
I have defined the following typeclass
class Roll a where
roll ...
5
votes
1
answer
72
views
Rewrite rules with typeclass constraints
There's been a couple of previous questions about rewrite rules that have typeclass constraints, but they seem to involve higher-rank polymorphism. Here, I have a much more "vanilla" ...
2
votes
0
answers
65
views
Multiparameter classes versus data types [closed]
It often happens that we get to choose between writing a (often multiparameter) class or using some (sometimes n-ranked) data type. Let me give two examples to clarify my point:
Example 1
An action of ...
1
vote
1
answer
83
views
When trying to convert a Map to scala objects using Shapeless, Coproduct derivatives are not picked up the compiler
I am trying to convert Map[String, AttributeValue] to Scala objects. AttributeValue represents data in DynamoDB. FromAttributeValue in the below stub is used to convert basic cases
Following is code ...
2
votes
1
answer
39
views
How to approach testing for polymorphic functions taking newtype smart wrappers around simple types?
I have written a program in whcih I've defined several functions for which I've defined a concrete signature, say
work :: Foo -> [(Foo, Bar)] -> [(Foo, Bar)]
When approaching QuickCheck-based ...
6
votes
1
answer
79
views
How to check from the REPL that a given constraint is satisfied?
For the sake of exploring libraries with complex types and typeclass instances, I want to check from the REPL whether a typeclass constraint is satisfied or not (provided all the relevant dataypes and ...
3
votes
1
answer
54
views
Custom `Read` instance fails when type is wrapped in Max, but derived instance works - What's wrong with my Read instance?
(Why am I writing Positive32? See my previous question.)
Here's a module exposing a newtype for 32-bits postive integers:
module Positive32 (Positive32, positive32, getNum) where
import Data.Word (...
0
votes
1
answer
67
views
Typeclass projections as inheritance
I want to allow typeclasses to be easily "inherited" on union types, by deriving the typeclass automatically when there exists a projection (this projection is another typeclass that I ...