haskellbounded-types# How to get the maxBound of a type that has a Bounded instance

# Type signature is sufficient

# Using the

The following Haskell code does not compile:

```
getMaxBound :: (Bounded a) => a -> a
getMaxBound _ = maxBound :: a
```

The error I get is the following:

```
Could not deduce (Bounded a1) arising from a use of ‘maxBound’
from the context: Bounded a
bound by the type signature for:
getMaxBound :: Bounded a => a -> a
at rot13.hs:3:1-36
```

Why can't I get the maxBound of a bounded type in Haskell?

Ideally, I would use this function like this:

```
getMaxBound 3
> 9223372036854775807
getMaxBound 'c'
> '\1114111'
```

I feel like every time I have any type `a`

which has a Bounded instance, I should be able to get the maxBound of that type.

What am I missing?

Solution

Since the signature already restricts the type, you can drop the `:: a`

part in the function body:

`getMaxBound :: Bounded a => `**a** -> **a**
getMaxBound _ = maxBound

The type signature specifies that type of the input of the function is the same as the type of the output of the function.

For example:

```
Prelude> getMaxBound (3 :: Int)
9223372036854775807
Prelude> getMaxBound 'c'
'\1114111'
Prelude> getMaxBound True
True
Prelude> getMaxBound LT
GT
```

`ScopedTypeVariables`

extensionWe can also use the ** ScopedTypeVariables** extension [Haskell-wiki], and then implement it with a reference to the

`a`

type variable:`{-# LANGUAGE `**ScopedTypeVariables** #-}
getMaxBound :: **forall a .** Bounded a => a -> a
getMaxBound _ = maxBound **:: a**

- Why do I get "Unexpected reply type" from notify-send when using this Haskell notification server?
- Haskell fails to infer the return type of a monad after using the sequence operator
- Don't understand notation of morphisms in Monoid definition
- Foldln in haskell
- Is this property of a functor stronger than a monad?
- How to Instantiate a Custom Data Type with Record Syntax and Multiple Constructors
- How do I make a minimal working example for the a DBus server?
- Is it safe to downgrade Haskell stack version?
- Haskell, list of natural number
- unfamiliar syntax in Haskell / Clash function type signature
- foldM with monad State does not type check
- Why does my Runge-Kutta implementation oscillate to 0?
- How do I get the desired behavior in my TCP server?
- Why does the Haskell PVP describe new functions as non-breaking?
- How do I correctly use toLower in Haskell?
- Every Lens' is a Traversal'... how?
- How do I crate a value of type a{sv} for a call to org.freedesktop.Notifications.Notify via DBus?
- Web Scraping With Haskell
- Double exclamation marks in Haskell
- Haskell Servant POST FormUrlEncoded for (Vector String) field
- Confusion about list types in Haskell
- Idiomatic way to define new vs persisted types in Haskell
- Why does Cabal, unlike GHC, not automatically enable GeneralizedNewtypeDeriving if I explicitly enabled DerivingStrategies?
- What is the proper way of wrapping an Int (not a general type) in another type if type safety is the only motive?
- Parsing inside `between` with Megaparsec
- takeWhile implementation in JavaScript - Looking for better ideas
- How to setup MINGW environment variables for Haskell language server in vscode?
- mysql-haskell no invoke of Right case of try function
- Run cleanup function in multiple Haskell child threads when POSIX Signal sent (SIGTERM etc)
- Gloss animations jerky and hope to add `-O2` to GHCi