Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type Constraints in Data Declaration Haskell

I'm using Haskell and trying to write the following:

data Scale s = Scale s s

However, I want to make it so that s must be something that of the Num type class, like Int or Double. Is that possible to do using Haskell and GHC?

like image 307
Avi Caspe Avatar asked Nov 27 '16 04:11

Avi Caspe


People also ask

What is a type constraint Haskell?

Type constraints (Num a) => is a type constraint, which restricts the type a to instances of the class Num .

What is type declaration in Haskell?

Haskell has three basic ways to declare a new type: The data declaration, which defines new data types. The type declaration for type synonyms, that is, alternative names for existing types. The newtype declaration, which defines new data types equivalent to existing ones.

How do you define data types in Haskell?

The Data Keyword and Constructors In general, we define a new data type by using the data keyword, followed by the name of the type we're defining. The type has to begin with a capital letter to distinguish it from normal expression names. To start defining our type, we must provide a constructor.

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.


2 Answers

Yes:

{-# LANGUAGE GADTs #-}
data Scale s where
    Scale :: Num s => s -> s -> Scale s

However, it's generally considered best practice not to do this. Instead, put the Num constraint only on the functions that use Scales and need the Num constraint. Being relaxed about such constraints allows you to temporarily break the invariant where appropriate; e.g. it's common to wish for a Functor instance for such a type, which is impossible if you constrain the constructor as above.

like image 109
Daniel Wagner Avatar answered Sep 23 '22 08:09

Daniel Wagner


I had a similar situation with Point type. But I thought not about constraints, I thought about how to do generalizing element type of my point. Then I understood if I'll have point type like this data Point a = Point a a then I can do it instance of Functor, Applicative, Foldable and Traversable. And I'll can design function by standart general way. For example:

dist :: Floating a => Point a -> Point a -> a
dist a b = sqrt $ sum $ (^2) <$> ((-) <$> a <*> b)

And I had question. What is going on? :) If I add constraint (as you asked) I would not can design by this way and I would need to implement a lot of functions like pointSub.

So, there is something to think about :)

like image 42
freestyle Avatar answered Sep 27 '22 08:09

freestyle