Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a custom type to an Integer in Haskell?

I am trying to use my own data type in haskell for prime numbers, but i am currently running into a few issues.

newtype Prime = Prime Integer deriving (Eq, Ord, Typeable, Show)

As soon as i am doing any numeric operation on a prime number (e.g. the "phi" function below) i want to handle the result as an Integer but i don't know how to do it.

phi :: Prime -> Prime -> Integer
phi p q = (p-1)*(q-1)

phi should return an Integer because it's not a Prime number anymore. All i get is the expected error message:

    • Couldn't match expected type ‘Integer’ with actual type ‘Prime’
    • In the expression: (p - 1) * (q - 1)
      In an equation for ‘genPhi’: genPhi p q = (p - 1) * (q - 1)

So how can i convert a my custom type to an Integer? I don't have a lot of experience with Haskell.

like image 220
Leon K. Avatar asked Dec 30 '20 21:12

Leon K.


2 Answers

You can unwrap the Integer out of the Prime data constructor:

genPhi :: Prime -> Prime -> Integer
genPhi (Prime p) (Prime q) = (p-1) * (q-1)
like image 61
Willem Van Onsem Avatar answered Sep 21 '22 13:09

Willem Van Onsem


If you have a simple newtype wrapper over an existing type, a good trick is to use the DerivingVia extension:

{-# LANGUAGE DerivingVia #-}

newtype Prime = Prime { unPrime :: Integer }
   deriving Num via Integer

phi :: Prime -> Prime -> Integer
phi p q = unPrime $ (p-1)*(q-1)

Given this, you can say:

*Main> phi (Prime 3) (Prime 5)
8

And furthermore all other numeric operations will automatically work on your Prime type, simply using their Integer equivalents.

See deriving via for details.

NB As noted in the comments, this doesn't mean GHC will make sure the result of these operations is Prime by any means; it just lets you lift the underlying operations. (But you could've made the same mistake without the deriving mechanism anyway.) If your program maintains the invariant that the Prime constructor always means the argument is a prime number, then do not use this trick. This is often not an issue as the invariant is either not clearly defined or easily enforceable, and the constructor simply acts as a reminder. But it's best to be clear on that and not use the deriving trick if you crucially depend on it.

like image 24
alias Avatar answered Sep 20 '22 13:09

alias