Can I constrain the parametric polymorphic type on type/data constructor in Haskell?

Alex

I have a parameterised type that I'd like to constrain to a numeric type, more specifically a Fractional, e.g.:

data Rating a = (Fractional a) => Score a | Unscored deriving (Show, Eq)

so that the user of the API can define which non-integer type they might utilise (Float or Double?), but the internal API code I write can still perform arithmetic operations on a numeric type. I do not want it to be an integer because results of my "internal operations" may not be integers and my understanding is that using Fractional would lead to more accurate results.

Compiling the above (in GHCI at least) gives me the following error:

    Data constructor `Score' has existential type variables, a context, or a specialised result type
  Score :: forall a. Fractional a => a -> Rating a
  (Use ExistentialQuantification or GADTs to allow this)
In the definition of data constructor `Score'
In the data declaration for `Rating'

which suggests to me I'm doing something that I probably don't want to keep trying; i.e. my design is rubbish.

I guess I'm trying to say the following in this API: "when you use a Rating type, its parameter must be a subclass of Fractional so I can perform accurate arithmetic on it". How might I achieve this? Or am I way off the mark and/or overengineering?

Twan van Laarhoven

You shouldn't put the Fractional constraint on the datatype, but rather on the functions that work with it. So

data Rating a = Score a | Unscored deriving (Show, Eq)

makeSomeRating :: Fractional a => a -> Rating a
makeSomeRating x = Score (x / 2) -- can use Fractional functions here

doSomethingElseWithRating :: Fractional a => Rating a -> Something

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can I build a parameterless constructor for a parametric type in an outer constructor?

From Dev

Can Haskell display an object of polymorphic type ?

From Dev

Haskell - Illegal Polymorphic type?

From Dev

haskell polymorphic type functions

From Dev

Polymorphic type aliases in haskell

From Dev

How can I type a "polymorphic" React component?

From Dev

Can a function be parametrically polymorphic over a non-nullary type constructor?

From Dev

How can I constrain a Swift protocol to a concrete type?

From Dev

F# generics: can I constrain a type to not be "unit"?

From Dev

How can I constrain an OCaml integer type to a range of integers?

From Dev

Can I have generic constrain of type procedure of object in Delphi

From Dev

F# generics: can I constrain a type to not be "unit"?

From Dev

Can I use std::make_shared with structs that don't have a parametric constructor?

From Dev

Is there a default polymorphic unit type haskell

From Dev

Can I match a data constructor wildcard in Haskell?

From Dev

Constrain elements of a type-level list in Haskell

From Dev

Constrain elements of a type-level list in Haskell

From Dev

Why I can't use a case object as a polymorphic type

From Dev

Can I make a polymorphic `mult` function, that takes any numeric type?

From Dev

Type constructor for integers in Haskell

From Dev

polymorphic constructor for type-level list

From Dev

How do I use a Haskell type constructor as an enumeration?

From Dev

How do I use a Haskell type constructor as an enumeration?

From Dev

How do I understand the set of valid inputs to a Haskell type constructor?

From Dev

How can I write a function have a polymorphic return type based on the type argument of its type parameter?

From Dev

How can I reduce syntactic clutter when tagging a polymorphic tree in Haskell?

From Dev

How can I constrain weights of linear classifier?

From Java

Haskell Type vs Data Constructor

From Dev

Undefined type constructor "Int" in Haskell

Related Related

  1. 1

    Can I build a parameterless constructor for a parametric type in an outer constructor?

  2. 2

    Can Haskell display an object of polymorphic type ?

  3. 3

    Haskell - Illegal Polymorphic type?

  4. 4

    haskell polymorphic type functions

  5. 5

    Polymorphic type aliases in haskell

  6. 6

    How can I type a "polymorphic" React component?

  7. 7

    Can a function be parametrically polymorphic over a non-nullary type constructor?

  8. 8

    How can I constrain a Swift protocol to a concrete type?

  9. 9

    F# generics: can I constrain a type to not be "unit"?

  10. 10

    How can I constrain an OCaml integer type to a range of integers?

  11. 11

    Can I have generic constrain of type procedure of object in Delphi

  12. 12

    F# generics: can I constrain a type to not be "unit"?

  13. 13

    Can I use std::make_shared with structs that don't have a parametric constructor?

  14. 14

    Is there a default polymorphic unit type haskell

  15. 15

    Can I match a data constructor wildcard in Haskell?

  16. 16

    Constrain elements of a type-level list in Haskell

  17. 17

    Constrain elements of a type-level list in Haskell

  18. 18

    Why I can't use a case object as a polymorphic type

  19. 19

    Can I make a polymorphic `mult` function, that takes any numeric type?

  20. 20

    Type constructor for integers in Haskell

  21. 21

    polymorphic constructor for type-level list

  22. 22

    How do I use a Haskell type constructor as an enumeration?

  23. 23

    How do I use a Haskell type constructor as an enumeration?

  24. 24

    How do I understand the set of valid inputs to a Haskell type constructor?

  25. 25

    How can I write a function have a polymorphic return type based on the type argument of its type parameter?

  26. 26

    How can I reduce syntactic clutter when tagging a polymorphic tree in Haskell?

  27. 27

    How can I constrain weights of linear classifier?

  28. 28

    Haskell Type vs Data Constructor

  29. 29

    Undefined type constructor "Int" in Haskell

HotTag

Archive