Skip to main content

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 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, ...
HaveMercy's user avatar
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. ...
Mikael Vejdemo-Johansson's user avatar
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() ...
Joan's user avatar
  • 4,260
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 ...
Saurabh Nanda's user avatar
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 ...
MartinHH's user avatar
  • 1,168
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) => ...
Yago's user avatar
  • 363
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: ...
Ali Tc's user avatar
  • 11
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 ...
Yago's user avatar
  • 363
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" ...
Cactus's user avatar
  • 27.5k
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 ...
141592653's user avatar
  • 689
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 ...
Johny T Koshy's user avatar
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 ...
Enlico's user avatar
  • 26.7k
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 ...
danidiaz's user avatar
  • 27.4k
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 (...
Enlico's user avatar
  • 26.7k
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 ...
CowNorris's user avatar
  • 432

15 30 50 per page
1
2 3 4 5
133