Questions tagged [deriving]

In Haskell, a derived instance is an instance declaration that is generated automatically in conjunction with a data or newtype declaration. The body of a derived instance declaration is derived syntactically from the definition of the associated type.

In Haskell, a derived instance is an instance declaration that is generated automatically in conjunction with a data or newtype declaration. The body of a derived instance declaration is derived syntactically from the definition of the associated type.

139 questions
1
vote
2 answers

Is there a way to derive Num class functions in own data type in Haskell?

Let's say I have a type declaration: data MyType = N Double | C Char | Placeholder I want to be able to treat MyType as a Double whenever it's possible, with all the Num, Real, Fractional functions resulting in N (normal result) for arguments…
1
vote
1 answer

Deriving Generic from data declared in another file fails

I have these two modules: module Server where import Data.JSON.Schema.Generic (gSchema) import Data.JSON.Schema.Types (JSONSchema(schema)) import Two instance JSONSchema Data where schema = gSchema main :: IO () main = undefined {-# LANGUAGE…
ryskajakub
  • 6,351
  • 8
  • 45
  • 75
1
vote
1 answer

Haskell: Creating an instance of Show

In the code below, I define an algebraic data type and I (attempt to) make it an instance of Show. However, I'm getting a compile-time error (included below). What am I doing wrong? I believe I'm using the correct syntax (atleast as per this…
iceman
  • 2,020
  • 2
  • 17
  • 24
1
vote
1 answer

How to use deriving and batteries at same time in OCaml?

So basically in my project I need to use batteries (for File.lines_of and other related functions) and deriving (for Show.show) at the same time. However, when I use opam to install package deriving on my Mac, it seems that it will firstly remove…
lllllllllllll
  • 8,519
  • 9
  • 45
  • 80
1
vote
1 answer

Random value of a user-defined data Type in Haskell

I've defined the following Data Type: data NewBool = Truth | Lie deriving (Show) and I've created a function which should return a random NewBool-value giveMeBool :: IO() giveMeBool = do bool <- randomIO :: IO NewBool putStrLn("Your…
GniruT
  • 731
  • 1
  • 6
  • 14
0
votes
1 answer

Deriving newtype recursively like functionality

Lets say, in package P, I've got a type A, defined something like so: newtype A = A Int There's then external package X (which I don't control, unlike P and Q), which has a class C and an instance: instance C Int where ... So X depends on…
Clinton
  • 22,361
  • 15
  • 67
  • 163
0
votes
1 answer

How to use deriving Show() in mutually recursive types?

Let's directly see the codes. type symbol = | JumpDes of int | CallDes of func | StarDes of exp (*here needs the definition of type exp*) deriving (Show) type exp = | Const of const | Symbol of symbol (*here needs the definition of…
S1mple
  • 35
  • 6
0
votes
1 answer

Using "deriving via" with a type family

I have a typeclass with a default implementation, and would like to provide a simple way to derive the typeclass if a user wants to use their custom monad. Here's a solution someone else provided me: {-# LANGUAGE DerivingVia #-} {-# LANGUAGE…
Edward
  • 89
  • 1
  • 5
0
votes
1 answer

-std=c++11 undefined reference to vtable for -- Chess

I've an issue and I can't solve it (with google). As practice I write a program about chess. There is a general piece class: pieces.h : #ifndef PIECES_H #define PIECES_H [...] class piece{ public: bool dark; //0 = light, 1 = dark virtual…
0
votes
2 answers

defining new monad instance

A few days ago I was trying to prove monad laws by creating a new monad instance but I found myself stuck at defining the new monad instance. {-# LANGUAGE DeriveFunctor, InstanceSigs #-} import Control.Monad newtype Test a = Test { getTest ::…
teraque
  • 15
  • 2
0
votes
2 answers

Use the lowest subtype in a typeclass?

I have the following code: sealed trait Animal case class Cat(name: String) extends Animal case class Dog(name: String) extends Animal trait Show[A] { def show(a: A): String } class Processor[A](a: A) { def print(implicit S: Show[A]): Unit =…
fungtional
  • 69
  • 4
0
votes
1 answer

How to derive std::vector?

I want a class derived from std::vector for my operator [] template class MyVector : public std::vector { public: // ?... const T &operator[](size_t index) const { //... } T &operator[](size_t…
Ufx
  • 2,595
  • 12
  • 44
  • 83
0
votes
0 answers

How to call a deriving class operator()?

I want create an expression structure, to calculate value (by calling operator(). expr - base expression class, it should work like result = expression(100500). const_value - struct returns constant value. argument - returns it's argument (==…
Nex
  • 1,423
  • 2
  • 10
  • 7
0
votes
1 answer

A typecheck errors in deriving wrapper for Linear.V

I am trying to make a newtype wrapper for the Linear.V type and derive useful instances. I was trying this: {-# LANGUAGE DataKinds, PolyKinds, ScopedTypeVariables, StandaloneDeriving, FlexibleContexts, UndecidableInstances,…
user1747134
  • 2,374
  • 1
  • 19
  • 26
0
votes
1 answer

Can I consider this derivation as Leftmost or/and Rightmost?

For example I would like to derive the string 'aabbccdd' from the given set of production: S -> AB | C A -> aAb | ab B -> cBd | cd C -> aCd | aDd D -> bDc | bc I can derive the string from AB using the leftmost and rightmost derivation. But how…
mmuncada
  • 528
  • 1
  • 11
  • 31
1 2 3
9
10