Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Type declarations (Patrik Iselind)
   2. Re:  Type declarations (Francesco Ariis)
   3. Re:  Type declarations (mrx)
   4. Re:  Type declarations (Francesco Ariis)
   5. Re:  Type declarations (Patrik Iselind)
   6. Re:  Why do i need to specify the class of a here at all?
      (Quentin Liu)


----------------------------------------------------------------------

Message: 1
Date: Sun, 26 Nov 2017 14:50:30 +0100
From: Patrik Iselind <patrik....@gmail.com>
To: beginners@haskell.org
Subject: [Haskell-beginners] Type declarations
Message-ID: <a5e68c6f-c3fb-2163-309a-eaf7c1049...@gmail.com>
Content-Type: text/plain; charset=windows-1252; format=flowed

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



------------------------------

Message: 2
Date: Sun, 26 Nov 2017 15:07:36 +0100
From: Francesco Ariis <fa...@ariis.it>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] Type declarations
Message-ID: <20171126140736.gnpice6l3qa7h...@x60s.casa>
Content-Type: text/plain; charset=iso-8859-1

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?


------------------------------

Message: 3
Date: Sun, 26 Nov 2017 19:02:36 +0100
From: mrx <patrik....@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Type declarations
Message-ID:
        <canzojbjxhzxjecev3k8vjbj6aurxxt0jt+lpsgi8whg9tak...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Den 26 nov 2017 15:08 skrev "Francesco Ariis" <fa...@ariis.it>:

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20171126/e57105aa/attachment-0001.html>

------------------------------

Message: 4
Date: Sun, 26 Nov 2017 19:20:16 +0100
From: Francesco Ariis <fa...@ariis.it>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] Type declarations
Message-ID: <20171126182016.q3aqfnmcd5a5z...@x60s.casa>
Content-Type: text/plain; charset=us-ascii

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.


------------------------------

Message: 5
Date: Sun, 26 Nov 2017 19:39:49 +0100
From: Patrik Iselind <patrik....@gmail.com>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] Type declarations
Message-ID: <74ddf555-9c14-cd18-5c0d-1b3062efc...@gmail.com>
Content-Type: text/plain; charset=utf-8; format=flowed


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


------------------------------

Message: 6
Date: Sun, 26 Nov 2017 14:48:22 -0500
From: Quentin Liu <quentin.liu.0...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Why do i need to specify the class of
        a here at all?
Message-ID: <883dc5c3-2c67-4c9c-a71a-cab090337426@Spark>
Content-Type: text/plain; charset="utf-8"

> ```
> exercises.hs:33:13:
>     Couldn't match expected type ‘[b0]’ with actual type ‘a’
>       ‘a’ is a rigid type variable bound by
>           the type signature for myOrderFunc :: a -> a -> Ordering
>           at exercises.hs:31:16
>     Relevant bindings include
>       y :: a (bound at exercises.hs:32:15)
>       x :: a (bound at exercises.hs:32:13)
>       myOrderFunc :: a -> a -> Ordering (bound at exercises.hs:32:1)
>     In the first argument of ‘myLen’, namely ‘x’
>     In the first argument of ‘(<)’, namely ‘myLen x’
> Failed, modules loaded: none.
> ```

Your guess is correct. The problem is, Haskell does not consider `a` in 
`myOrderFunc` and `[b]` in `myLen` equivalent. `a` means you feed the function 
any type, while `[b]` means it must be a list of values of the same type. So 
changing `a` to `[a]` woud eliminate the error.

Regards,
Qingbo Liu

On Nov 24, 2017, 16:33 -0500, Patrik Iselind <patrik....@gmail.com>, wrote:
>
> Den 2017-11-24 kl. 20:04, skrev Quentin Liu:
> >
> > > > > Yes, you could pass the function a list of strings as well. A string 
> > > > > is just a list of Chars. The type signature `a` does not restrict the 
> > > > > range of types you could pass to the function.
> > > >
> > > > That seem strange to me. Wouldn't that mean that i could write the 
> > > > declaration of myOrderFunc as `myOrderFunc :: a -> a -> Ordering` as 
> > > > well? GHCI give me an error on this though so obviously it's wrong. I 
> > > > just don't see why. Why cannot a represent [b]?
> >
> > Could you copy and paste the error message here?
> Sure, the error i get follows
> ```
> exercises.hs:33:13:
>     Couldn't match expected type ‘[b0]’ with actual type ‘a’
>       ‘a’ is a rigid type variable bound by
>           the type signature for myOrderFunc :: a -> a -> Ordering
>           at exercises.hs:31:16
>     Relevant bindings include
>       y :: a (bound at exercises.hs:32:15)
>       x :: a (bound at exercises.hs:32:13)
>       myOrderFunc :: a -> a -> Ordering (bound at exercises.hs:32:1)
>     In the first argument of ‘myLen’, namely ‘x’
>     In the first argument of ‘(<)’, namely ‘myLen x’
> Failed, modules loaded: none.
> ```
> Attaching the updated exercises.hs for reference.
>
> I'm still not very good at interpreting Haskell's error messages, they are 
> quite cryptic to me. My interpretation/guess of the above is that my `a` is 
> too 'wide' or how you express it. Haskell seem to expect some form of list. 
> Most likely since i want a length and lists are perhaps everything in Haskell 
> that can produce a length. I've hardly scratched the surface of what i 
> imagine is Haskell so i cannot say anything for sure yet.
>
> >
> > The way I use to think about type signature is, when you trying to 
> > substitute type variables such as `a`, substitute it into a concrete type 
> > that you are working with.
> I'm having a hard time understanding your way of thinking about type 
> signatures. Could you perhaps elaborate a bit more on it?
>
> // Patrik
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20171126/dd685676/attachment.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 113, Issue 26
******************************************

Reply via email to