Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lifting class instance in Haskell

Is there a way to "lift" a class instance in Haskell easily?

I've been frequently needing to create, e.g., Num instances for some classes that are just "lifting" the Num structure through the type constructor like this:

data SomeType a = SomeCons a

instance (Num a)=>Num SomeCons a where
    (SomeCons x) + (SomeCons y) = SomeCons (x+y)
    negate (SomeCons x) = SomeCons (negate x)
    -- similarly for other functions.

Is there a way to avoid this boilerplate and "lift" this Num structure automatically? I usually have to do this with Show and other classes also when I was trying to learn existencials and the compiler wouldn't let me use deriving(Show).

like image 593
Rafael S. Calsaverini Avatar asked Nov 29 '09 21:11

Rafael S. Calsaverini


People also ask

What does LIFT do in Haskell?

lift takes the side effects of a monadic computation within the inner monad m and lifts them into the transformed monad t m . We can easily define functions that lift functions between inner monads to functions between transformed monads.

What is a class instance in Haskell?

An instance of a class is an individual object which belongs to that class. In Haskell, the class system is (roughly speaking) a way to group similar types. (This is the reason we call them "type classes"). An instance of a class is an individual type which belongs to that class.

What is a type class in Haskell?

What's a typeclass in Haskell? A typeclass defines a set of methods that is shared across multiple types. For a type to belong to a typeclass, it needs to implement the methods of that typeclass. These implementations are ad-hoc: methods can have different implementations for different types.


1 Answers

The generalized newtype deriving extension is what you want here:

{-# LANGUAGE GeneralizedNewtypeDeriving #-}

module Main where

newtype SomeType a = SomeCons a deriving (Num, Show, Eq)

main = do
  let a = SomeCons 2
      b = SomeCons 3
  print $ a + b

Output:

*Main> main
SomeCons 5
like image 145
Greg Bacon Avatar answered Oct 11 '22 14:10

Greg Bacon