Réf. : [Haskell-beginners] problems with GADTs

corentin.dupont at ext.mpsa.com corentin.dupont at ext.mpsa.com
Thu Sep 2 05:16:15 EDT 2010


Hello,
I'm beginner too but i will try to respond.
I would do this like that:

> type Cost = Int
> type ID = Int
> type Student = String
> type Activity = String

> data Table k v = PopTable
>       { tableName :: String
>       , tableList :: [(k, v)]
>       } deriving (Show, Read)

> type Students = Table ID Student
> type ActivityCosts = Table Activity Cost
> type StudentActivities = Table Activity ID


> loaders :: [ProgramName]
> loaders = [CStudents $ PopTable "students" loadStudentIDs, CActivityCosts
$ PopTable "costs" loadActivityCosts]

> data ProgramName = CStudents Students
>                  | CActivityCosts ActivityCosts
>                  | CStudentActivities StudentActivities

> loadStudentIDs :: [(ID, Student)]
> loadStudentIDs = [(1, "marcel"), (2, "alphonse")]

> loadActivityCosts :: [(Activity, Cost)]
> loadActivityCosts = [("work", 10)]


There is no need for a GATD.
All you need here, is to hide your different types into one named
ProgramName.
Once this is done, you can have a list of ProgramName.

In the ProgramName datatype, you see different contructors, which allow you
to contruct a ProgramName.
In GHCI:
> :t CStudents
CStudents :: Students -> ProgramName


Cheers,
Corentin




                                                                           
             Michael Litchard                                              
             <michael at schmong                                              
             .org>                                                    Pour 
             Envoyé par :              beginners at haskell.org               
             beginners-bounce                                           cc 
             s at haskell.org                                                 
                                                                     Objet 
                                       [Haskell-beginners] problems with   
             02/09/2010 01:11          GADTs                               
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           




I'm learnined how to use GADTs, but for the moment I'm stuck on the
following problem.

some context first


> newtype Cost = Cost Int
> newtype ID = ID Int
> newtype Student = Student String
> newtype Activity = Activity String
> data Table k v = PopTable
>       { tableName :: String
>       , tableList :: [(k, v)]
>       } deriving (Show, Read)
> type Students = Table ID Student
> type ActivityCosts = Table Activity Cost
> type StudentActivities = Table Activity ID

I want to do something like this

> loaders = [("students", loadStudentIDs), ("costs", loadActivityCosts),
("activities", loadStudentActivities)]

But I can't have list with multiple types. Okay so ski (from #haskell)
suggested I use a GADT. This is new territory for me. Here's what it
looks like right now.

data ProgramName :: * -> *
     where
     PNStudents :: ProgramName Students
     PNCosts    :: ProgramName ActivityCosts
     PNActivities :: ProgramName StudentActivities

Now I should be able to use this to define a data structure called
"loaders" that contains three tuples ("students", loadStudentIDs),
("costs", loadActivityCosts), and ("activities",
loadStudentActivities).

So, what would "loaders" look like? I've done some reading but it's
not sunk in yet.
_______________________________________________
Beginners mailing list
Beginners at haskell.org
http://www.haskell.org/mailman/listinfo/beginners




More information about the Beginners mailing list