# Learning how to define the type of a function

**URL:** https://discourse.haskell.org/t/learning-how-to-define-the-type-of-a-function/840
**Category:** Learn
**Created:** [August 15, 2019, 5:48pm UTC](https://discourse.haskell.org/t/learning-how-to-define-the-type-of-a-function/840 "2019-08-15T17:48:03Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Sheaf](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@Sheaf](https://discourse.haskell.org/u/Sheaf)
#### Post date: [August 15, 2019, 5:48pm UTC](https://discourse.haskell.org/t/learning-how-to-define-the-type-of-a-function/840/1 "2019-08-15T17:48:03Z")

</div>

Hi everyone, I already read how to define a function and its type. I know that this is just a trivial example and that’s why I’m starting with it

intoint :: (Integral a) =\> a -\> Int  
intoint x = x :: Int

---

<div class="post-metadata">

### Author: ![belka](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@belka](https://discourse.haskell.org/u/belka)
#### Post date: [August 15, 2019, 6:37pm UTC](https://discourse.haskell.org/t/learning-how-to-define-the-type-of-a-function/840/2 "2019-08-15T18:37:31Z")

</div>

```haskell
x :: Int

```

helps to resolve ambigious types. In some situations the compiler can’t infer the exact type of a variable and you can specify the type, with `x :: Int` for example, explicitly.  
But in your code you have a different situation. You have some `a` which is an `Integral` and you’re trying to convert it into `Int` which cannot be done automatically since `Integral a` is more generic than `Int`. The function you’re looking for is `fromIntegral`:

```haskell
fromIntegral :: (Integral a, Num b) => a -> b

```

So it takes an `Integral` and returns a `Num` (and `Int` has an instance of `Num`).

So you can write:

```haskell
intoint :: (Integral a) => a -> Int
intoint x = fromIntegral x

```

or just:

```haskell
intoint :: (Integral a) => a -> Int
intoint = fromIntegral

```

Edit: You probably wanted to “cast” to `Int`. It is not what `::` is for. I don’t think Haskell has casts (at least a special syntax for that, after all Haskell is strongly typed :)) like many other languages, but there functions like `fromIntegral` that do similar conversions.
