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. Custom type classes (Daniel Hinojosa) 2. Re: Custom type classes (Daniel Hinojosa) 3. Re: Custom type classes (Daniel Hinojosa) 4. Re: Custom type classes (Imants Cekusins) ---------------------------------------------------------------------- Message: 1 Date: Sun, 24 Jan 2016 23:25:49 -0700 From: Daniel Hinojosa <dhinoj...@evolutionnext.com> To: beginners@haskell.org Subject: [Haskell-beginners] Custom type classes Message-ID: <caoxuh-4pgfbqpso1gzthjxdfpgsx_vdiccp+en5xbmg0fgq...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" I am pretty sure I have a good handle on type classes, but this one is perplexing me in Haskell. I created custom Tuples called Tuple2 and Tuple3 (I know Haskell already has Tuples, just thought I would create my own for this exercise). Then I wanted to create a type class that would have a method called first that would get the first element of the tuple regardless of what kind of Tuple it is. Tuple2, Tuple3, etc. Here is what I have: data Tuple3 a b c = Tuple3 a b c deriving (Show) data Tuple2 a b = Tuple2 a b deriving (Show) class Indexable idx where first :: idx c -> a instance Indexable (Tuple2 a) where first (Tuple2 a b) = a In my main, I try to get call putStrLn $ show $ first $ Tuple2 1 "One" I was greeted with the following trace: Couldn't match expected type ?a1? with actual type ?a? ?a? is a rigid type variable bound by the instance declaration at TypeClasses.hs:35:10 ?a1? is a rigid type variable bound by the type signature for first :: Tuple2 a c -> a1 at TypeClasses.hs:36:4 Relevant bindings include a :: a (bound at TypeClasses.hs:36:18) first :: Tuple2 a c -> a1 (bound at TypeClasses.hs:36:4) In the expression: a In an equation for ?first?: first (Tuple2 a b) = a Help is appreciated. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160124/198acbe7/attachment-0001.html> ------------------------------ Message: 2 Date: Sun, 24 Jan 2016 23:39:11 -0700 From: Daniel Hinojosa <dhinoj...@evolutionnext.com> To: beginners@haskell.org Subject: Re: [Haskell-beginners] Custom type classes Message-ID: <caoxuh-49cf+8bofkiniexy3jdpyzgms4znwdskckhv5wxz-...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Quick adjustment, playing around too much with it, that should be: class Indexable idx where first :: idx a -> a Problem still exists. On Sun, Jan 24, 2016 at 11:25 PM, Daniel Hinojosa < dhinoj...@evolutionnext.com> wrote: > I am pretty sure I have a good handle on type classes, but this one is > perplexing me in Haskell. I created custom Tuples called Tuple2 and Tuple3 > (I know Haskell already has Tuples, just thought I would create my own for > this exercise). Then I wanted to create a type class that would have a > method called first that would get the first element of the tuple > regardless of what kind of Tuple it is. Tuple2, Tuple3, etc. > > Here is what I have: > > data Tuple3 a b c = Tuple3 a b c deriving (Show) > > data Tuple2 a b = Tuple2 a b deriving (Show) > > class Indexable idx where > first :: idx c -> a > > instance Indexable (Tuple2 a) where > first (Tuple2 a b) = a > > In my main, I try to get call putStrLn $ show $ first $ Tuple2 1 "One" > > I was greeted with the following trace: > Couldn't match expected type ?a1? with actual type ?a? > ?a? is a rigid type variable bound by > the instance declaration at TypeClasses.hs:35:10 > ?a1? is a rigid type variable bound by > the type signature for first :: Tuple2 a c -> a1 > at TypeClasses.hs:36:4 > Relevant bindings include > a :: a (bound at TypeClasses.hs:36:18) > first :: Tuple2 a c -> a1 (bound at TypeClasses.hs:36:4) > In the expression: a > In an equation for ?first?: first (Tuple2 a b) = a > > Help is appreciated. > > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160124/5c4da218/attachment-0001.html> ------------------------------ Message: 3 Date: Sun, 24 Jan 2016 23:43:23 -0700 From: Daniel Hinojosa <dhinoj...@evolutionnext.com> To: beginners@haskell.org Subject: Re: [Haskell-beginners] Custom type classes Message-ID: <CAOXUh-6STv0j-oBk+uAYOg0EEfxgrKB_9y92hcnN35fFNLf=_...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Got it to work this way but it got the wrong one. Still looking. instance Indexable (Tuple2 a) where first (Tuple2 b a) = a On Sun, Jan 24, 2016 at 11:39 PM, Daniel Hinojosa < dhinoj...@evolutionnext.com> wrote: > Quick adjustment, playing around too much with it, that should be: > > class Indexable idx where > first :: idx a -> a > > Problem still exists. > > On Sun, Jan 24, 2016 at 11:25 PM, Daniel Hinojosa < > dhinoj...@evolutionnext.com> wrote: > >> I am pretty sure I have a good handle on type classes, but this one is >> perplexing me in Haskell. I created custom Tuples called Tuple2 and Tuple3 >> (I know Haskell already has Tuples, just thought I would create my own for >> this exercise). Then I wanted to create a type class that would have a >> method called first that would get the first element of the tuple >> regardless of what kind of Tuple it is. Tuple2, Tuple3, etc. >> >> Here is what I have: >> >> data Tuple3 a b c = Tuple3 a b c deriving (Show) >> >> data Tuple2 a b = Tuple2 a b deriving (Show) >> >> class Indexable idx where >> first :: idx c -> a >> >> instance Indexable (Tuple2 a) where >> first (Tuple2 a b) = a >> >> In my main, I try to get call putStrLn $ show $ first $ Tuple2 1 "One" >> >> I was greeted with the following trace: >> Couldn't match expected type ?a1? with actual type ?a? >> ?a? is a rigid type variable bound by >> the instance declaration at TypeClasses.hs:35:10 >> ?a1? is a rigid type variable bound by >> the type signature for first :: Tuple2 a c -> a1 >> at TypeClasses.hs:36:4 >> Relevant bindings include >> a :: a (bound at TypeClasses.hs:36:18) >> first :: Tuple2 a c -> a1 (bound at TypeClasses.hs:36:4) >> In the expression: a >> In an equation for ?first?: first (Tuple2 a b) = a >> >> Help is appreciated. >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160124/07e80afb/attachment-0001.html> ------------------------------ Message: 4 Date: Mon, 25 Jan 2016 08:42:10 +0100 From: Imants Cekusins <ima...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Custom type classes Message-ID: <cap1qinzxkabm2-w25kjd-z6athvu8twzgadz2jkr9vwsqyz...@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 Hello Daniel, it works with these tweaks: -- begin {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FlexibleContexts #-} module TupInst where data Tuple3 a b c = Tuple3 a b c deriving (Show) data Tuple2 a b = Tuple2 a b deriving (Show) class Indexable idx a where first :: idx -> a instance Indexable (Tuple2 a b) a where first (Tuple2 a0 b0) = a0 instance Indexable (Tuple3 a b c) a where first (Tuple3 a0 b0 c0) = a0 -- end call it in ghci like this: first $ Tuple3 (1::Int) 'a' False::Int ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 91, Issue 30 *****************************************