Type declarations

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Type declarations

Patrik Iselind
Hi,

What's the difference between `delta :: (Point t) => t -> t -> Double`
and `delta :: Point p -> Point q -> Double`. The later one is accepted
by GHCI when i do :load.

As i see it the first one would make better sense to me. I want two
Point as in-parameters and delta will produce a Double, but GHCI refuse
this saying

```
nine.hs:10:11:
     Expected a constraint, but ‘Point t’ has kind ‘*’
     In the type signature for ‘delta’:
       delta :: (Point t) => t -> t -> Double
Failed, modules loaded: none.
```

Is this saying that Point t match 'everything' (*)?

In the second version, which is accepted by GHCI, i don't see the point
of p and q. Can i use these somehow?

All of delta using the accepted type declaration looks like this for
reference:

```
data Direction d = LEFT
                 | RIGHT
                 | STRAIGHT
                   deriving (Show)

data Point a = Coordinate Double Double
              deriving (Show)

-- Calculate the slope between two points (dy/dx)
delta :: Point p -> Point q -> Double
delta (Coordinate a b) (Coordinate c d)
         | (a == c) = 0
         | otherwise = (d-b)/(c-a)

angle (Coordinate g h) (Coordinate i d) (Coordinate e f)
         | (delta a b) > (delta b c) = RIGHT
         | (delta a b) < (delta b c) = LEFT
         | otherwise = STRAIGHT
         where a = Coordinate g h
               b = Coordinate i d
               c = Coordinate e f
```

I'm also wondering if there is a simpler way than recreating the
Coordinate as a, b, and c in angle. It seems to work ok to me, i just
feel that a, b, and c in angle should be possible to express in a better
way.

--
Patrik Iselind

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Type declarations

Francesco Ariis
On Sun, Nov 26, 2017 at 02:50:30PM +0100, Patrik Iselind wrote:
> Hi,
>
> What's the difference between `delta :: (Point t) => t -> t -> Double` and
> `delta :: Point p -> Point q -> Double`. The later one is accepted by GHCI
> when i do :load.

Hello Patrik,
    `delta :: (Point t) => t -> t -> Double` means Point is a typeclass
and t is an instance of a typeclass.
In your case point is a datatype (data Point a etc. etc.) so the second
signature is the correct one.

> In the second version, which is accepted by GHCI, i don't see the point of p
> and q. Can i use these somehow?

`p` and `q` are the parameter of `Point a`, but since the definition
of Point is:

    data Point a = Coordinate Double Double
                 deriving (Show)

that `a` most likely has... no point (ueueuee pardon the pun) and would
better be written as

    data Point = Coordinate Double Double
                 deriving (Show)

Does this make sense?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Type declarations

Patrik Iselind


Den 26 nov 2017 15:08 skrev "Francesco Ariis" <[hidden email]>:
On Sun, Nov 26, 2017 at 02:50:30PM +0100, Patrik Iselind wrote:
> Hi,
>
> What's the difference between `delta :: (Point t) => t -> t -> Double` and
> `delta :: Point p -> Point q -> Double`. The later one is accepted by GHCI
> when i do :load.

Hello Patrik,
    `delta :: (Point t) => t -> t -> Double` means Point is a typeclass
and t is an instance of a typeclass.
In your case point is a datatype (data Point a etc. etc.) so the second
signature is the correct one.

> In the second version, which is accepted by GHCI, i don't see the point of p
> and q. Can i use these somehow?

`p` and `q` are the parameter of `Point a`,

What do you mean by parameter of Point a?

but since the definition
of Point is:

    data Point a = Coordinate Double Double
                 deriving (Show)

that `a` most likely has... no point (ueueuee pardon the pun) and would
better be written as

    data Point = Coordinate Double Double
                 deriving (Show)

Does this make sense?

I think I'll have to chew that until I reach the chapter on type classes in real world haskell. Hopefully I'll get it then.

Do you think it would be a mistake to simply skip writing the type declarations completely until I've reached type classes?

// Patrik

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Type declarations

Francesco Ariis
On Sun, Nov 26, 2017 at 07:02:36PM +0100, mrx wrote:
> What do you mean by parameter of Point a?

Let's start with a type you probably know, Maybe:

    data Maybe a = Just a
                 | Nothing

The `a` in `Maybe a` is a type parameter, as the whole thing can
be a `Maybe Int`, `Maybe String`, etc.

Now let's check what `Point a` does

      data Point a = Coordinate Double Double

Uhhh, suspicious, there is an `a` on the left side, but it's pretty
useless, because there is no `a` on the right side. This is
most likely not correct. Better to write

    -- this, concrete
    data Point = Coordinate Double Double
    -- or parametric
    data Point a = Coordinate a a

> Do you think it would be a mistake to simply skip writing the type
> declarations completely until I've reached type classes?

As now you know how write signatures like `something :: Int -> [String]`,
when you meet `Something a => etc.` tread with care until you reach
the chapter on typeclasses.
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Type declarations

Patrik Iselind

Den 2017-11-26 kl. 19:20, skrev Francesco Ariis:
> On Sun, Nov 26, 2017 at 07:02:36PM +0100, mrx wrote:
>> What do you mean by parameter of Point a?
> Let's start with a type you probably know, Maybe:
>
>      data Maybe a = Just a
>                   | Nothing
Sorry, i've not used Maybe yet. Chapter 3 that i'm trying to get through
now mention a Maybe ever so briefly. I've heard of a Maybe monad, is
that it?

> The `a` in `Maybe a` is a type parameter, as the whole thing can
> be a `Maybe Int`, `Maybe String`, etc.
>
> Now let's check what `Point a` does
>
>        data Point a = Coordinate Double Double
>
> Uhhh, suspicious, there is an `a` on the left side, but it's pretty
> useless, because there is no `a` on the right side. This is
> most likely not correct.
Ah, i see. Thanks for the clarification.
>   Better to write
>
>      -- this, concrete
>      data Point = Coordinate Double Double
>      -- or parametric
>      data Point a = Coordinate a a
Does this mean that i can write `delta :: Point Double t -> Point Double
t -> Direction d` as a type declaration. Then i would require
`Coordinate Double Double` as in parameters. Correct?
>> Do you think it would be a mistake to simply skip writing the type
>> declarations completely until I've reached type classes?
> As now you know how write signatures like `something :: Int -> [String]`,
> when you meet `Something a => etc.` tread with care until you reach
> the chapter on typeclasses.
When i write type declarations, then i should stick with the non-`(Foo
f) =>` version until i've reached that chapter on type classes. It's
stilla few chapters until i reach it, i'm on chapter 3 and type classes
are chapter 6.

// Patrik
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Type declarations

Francesco Ariis
On Sun, Nov 26, 2017 at 07:39:49PM +0100, Patrik Iselind wrote:
> Does this mean that i can write `delta :: Point Double t -> Point Double t
> -> Direction d` as a type declaration. Then i would require `Coordinate
> Double Double` as in parameters. Correct?

Careful there! Let's take a simple data declaration

    data Point = Point Int Float

On the right you have *the type*, on the left you have *the (data)
constructor*.

When you are writing signatures you are writing types, so

    f :: Point -> Point

and not

    f :: Point Int Float -> Point Int Float

Much like you write `addition :: Int -> Int -> Int` and not
`addition :: 7 -> 2 -> 9`.
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Type declarations

Patrik Iselind

Den 2017-11-26 kl. 21:24, skrev Francesco Ariis:

> On Sun, Nov 26, 2017 at 07:39:49PM +0100, Patrik Iselind wrote:
>> Does this mean that i can write `delta :: Point Double t -> Point Double t
>> -> Direction d` as a type declaration. Then i would require `Coordinate
>> Double Double` as in parameters. Correct?
> Careful there! Let's take a simple data declaration
>
>      data Point = Point Int Float
>
> On the right you have *the type*, on the left you have *the (data)
> constructor*.
But if the type Point have a parameter, `data Point p = Coordinate p p`.
Then i must be able to tell which `p` when i use Point in a type
declaration right? Like `delta :: Point Double a -> Point Double b ->
Double` would say that p is Double. How else am i supposed to specify p
in type declaration?

// Patrik
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Type declarations

Francesco Ariis
On Sun, Nov 26, 2017 at 10:21:39PM +0100, Patrik Iselind wrote:
> But if the type Point have a parameter, `data Point p = Coordinate p p`.
> Then i must be able to tell which `p` when i use Point in a type declaration
> right? Like `delta :: Point Double a -> Point Double b -> Double` would say
> that p is Double. How else am i supposed to specify p in type declaration?

If the type has a parameter, like

    data Point a = Coordinates a a

it will specified *once* in the signature, like this:

    f :: Point Double -> Point Double -> String

implementation looking something like

    f (Coordinates x y) (Coordinates m q) = x + 7 -- etc. etc.
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Type declarations

Patrik Iselind

Patrik Iselind

Den 2017-11-26 kl. 22:41, skrev Francesco Ariis:

> On Sun, Nov 26, 2017 at 10:21:39PM +0100, Patrik Iselind wrote:
>> But if the type Point have a parameter, `data Point p = Coordinate p p`.
>> Then i must be able to tell which `p` when i use Point in a type declaration
>> right? Like `delta :: Point Double a -> Point Double b -> Double` would say
>> that p is Double. How else am i supposed to specify p in type declaration?
> If the type has a parameter, like
>
>      data Point a = Coordinates a a
>
> it will specified *once* in the signature, like this:
>
>      f :: Point Double -> Point Double -> String
So what you're saying is that the `a` and `b` characters in my examples
should not be there. Other than that it's correct?

// Patrik
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Type declarations

Francesco Ariis
On Sun, Nov 26, 2017 at 10:51:26PM +0100, Patrik Iselind wrote:
> So what you're saying is that the `a` and `b` characters in my examples
> should not be there. Other than that it's correct?

Indeed :)
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Type declarations

Patrik Iselind


Den 26 nov 2017 23:32 skrev "Francesco Ariis" <[hidden email]>:
On Sun, Nov 26, 2017 at 10:51:26PM +0100, Patrik Iselind wrote:
> So what you're saying is that the `a` and `b` characters in my examples
> should not be there. Other than that it's correct?

Indeed :)

Awesome, thanks a lot!

// Patrik

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners