HList and Type signatures / synonyms

Hi, I keep accumulating values and right now use plain tuples for that. I end up with a 12 element tuple and things are a bit messy. I'd like to use extensible Records from HList instead, thing is I'd like to keep putting type signatures in my code. As it turns out that seems to be where it gets messy with HList. Is there a simple way to do this? Günther PS: Does anyone know why there are Label-n modules in HList, and which one to use?

Excerpts from Günther Schmidt's message of Sun Sep 06 19:40:05 -0400 2009:
I keep accumulating values and right now use plain tuples for that. I end up with a 12 element tuple and things are a bit messy.
Hi, you may want to consider using the Writer monad. [1]
I'd like to use extensible Records from HList instead, thing is I'd like to keep putting type signatures in my code. As it turns out that seems to be where it gets messy with HList.
Perhaps more precisely explaining your problem domain would be useful. Cheers, Edward [1] http://www.haskell.org/all_about_monads/html/writermonad.html

Hi Edward,
I suppose you're right, I could have made this a little more detailed.
I keep reading in and processing data in an accumulating way, ie. lets say
I have a DB table (don't take this literally please, just an example),
into in-memory records of type
data MyRecord = MyRecord {
name :: String,
birthDate :: LocalDate}
in a second step I read in Gender information so now I'd need an extended
Record
data MyRecord = MyRecord {
name :: String,
birthDate :: LocalDate,
gender :: Gender }
and so on for another 12 times.
In other words I need to extend the record.
I had been using tuples, ie:
(String, LocalDate) -> Gender -> (String, LocalDate, Gender)
but after 6 or so *extensions* things get a tad messy.
Anyway this is where HList really shines, but it is a bit akward when you
want to state the type of an HList record, ie.
type Entg =
Record
(HCons
(LVPair (Label HZero) DRG)
(HCons
(LVPair (Label (HSucc HZero)) BelegungsTyp)
(HCons
(LVPair (Label (HSucc (HSucc HZero))) EntgeltBetrag)
(HCons
(LVPair (Label (HSucc (HSucc (HSucc HZero)))) Zuschläge)
(HCons
(LVPair (Label (HSucc (HSucc (HSucc (HSucc HZero))))) Abschläge)
HNil)))))
which happens to be my actual stage 1 data type (Entg), one that has 5
"fields" initial and in several distinct steps 8 more are *added*.
So I wonder if there is a more simple way to do this, ie to *extend* the
*type* signatures of extend records without such a lot of typing.
Günther
Am 07.09.2009, 02:44 Uhr, schrieb Edward Z. Yang
Excerpts from Günther Schmidt's message of Sun Sep 06 19:40:05 -0400 2009:
I keep accumulating values and right now use plain tuples for that. I end up with a 12 element tuple and things are a bit messy.
Hi, you may want to consider using the Writer monad. [1]
I'd like to use extensible Records from HList instead, thing is I'd like to keep putting type signatures in my code. As it turns out that seems to be where it gets messy with HList.
Perhaps more precisely explaining your problem domain would be useful.
Cheers, Edward
[1] http://www.haskell.org/all_about_monads/html/writermonad.html

2009/9/6 Günther Schmidt
Hi Edward,
I suppose you're right, I could have made this a little more detailed.
I keep reading in and processing data in an accumulating way, ie. lets say I have a DB table (don't take this literally please, just an example), into in-memory records of type
data MyRecord = MyRecord { name :: String, birthDate :: LocalDate}
in a second step I read in Gender information so now I'd need an extended Record
data MyRecord = MyRecord { name :: String, birthDate :: LocalDate, gender :: Gender }
and so on for another 12 times.
In other words I need to extend the record.
I had been using tuples, ie:
(String, LocalDate) -> Gender -> (String, LocalDate, Gender)
but after 6 or so *extensions* things get a tad messy.
Anyway this is where HList really shines, but it is a bit akward when you want to state the type of an HList record, ie.
type Entg = Record (HCons (LVPair (Label HZero) DRG) (HCons (LVPair (Label (HSucc HZero)) BelegungsTyp) (HCons (LVPair (Label (HSucc (HSucc HZero))) EntgeltBetrag) (HCons (LVPair (Label (HSucc (HSucc (HSucc HZero)))) Zuschläge) (HCons (LVPair (Label (HSucc (HSucc (HSucc (HSucc HZero))))) Abschläge) HNil)))))
which happens to be my actual stage 1 data type (Entg), one that has 5 "fields" initial and in several distinct steps 8 more are *added*.
So I wonder if there is a more simple way to do this, ie to *extend* the *type* signatures of extend records without such a lot of typing.
Günther
Am 07.09.2009, 02:44 Uhr, schrieb Edward Z. Yang
: Excerpts from Günther Schmidt's message of Sun Sep 06 19:40:05 -0400 2009:
I keep accumulating values and right now use plain tuples for that. I end up with a 12 element tuple and things are a bit messy.
Hi, you may want to consider using the Writer monad. [1]
I'd like to use extensible Records from HList instead, thing is I'd like to keep putting type signatures in my code. As it turns out that seems to be where it gets messy with HList.
Perhaps more precisely explaining your problem domain would be useful.
Cheers, Edward
[1] http://www.haskell.org/all_about_monads/html/writermonad.html
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Why don't you define your record to be the final set of all the information that you will collect, and just leave some of the fields undefined until you collect all the data? That would work well if you are going to be collecting all of the data pretty much all at once. You could also have a record of Maybe values that started out as all Nothing and became Justs as the data was collected. Maybe I'm misunderstanding your problem, but that's how I would do it. Alex

2009/9/6 Günther Schmidt
I keep reading in and processing data in an accumulating way, ie. lets say I have a DB table (don't take this literally please, just an example), into in-memory records of type
data MyRecord = MyRecord { name :: String, birthDate :: LocalDate}
in a second step I read in Gender information so now I'd need an extended Record
data MyRecord = MyRecord { name :: String, birthDate :: LocalDate, gender :: Gender }
and so on for another 12 times.
In other words I need to extend the record.
I had been using tuples, ie:
(String, LocalDate) -> Gender -> (String, LocalDate, Gender)
but after 6 or so *extensions* things get a tad messy.
<snip>
So I wonder if there is a more simple way to do this, ie to *extend* the *type* signatures of extend records without such a lot of typing.
Do you actually need the intermediate data? More specifically, do you
need the data to be partial records?
One simple way to represent a partial record is a function which takes
the missing fields as arguments and returns the complete record, i.e.,
a constructor like MyRecord :: String -> LocalDate -> Gender ->
MyRecord.
So a partial result with the name filled in would have type LocalDate
-> Gender -> MyRecord.
More specifically, if the computations which produce the fields are
described using a monad or applicative functor, e.g., getName ::
Process String, you can just use <$> and <*> to glue them together.
MyRecord <$> getName <*> getDate <*> getGender
Or, if your process abstraction is a monad, you can do more complex operations.
do name <- getName
date <- getDate
gender <- getGender name date
return $ MyRecord name date gender
Instead of constructing intermediate records, all the data is kept in
variables until the record is complete.
--
Dave Menendez

Gunther,
I've got a little experience with HList - read below.
2009/9/6 Günther Schmidt
Hi,
I keep accumulating values and right now use plain tuples for that. I end up with a 12 element tuple and things are a bit messy.
I'd like to use extensible Records from HList instead, thing is I'd like to keep putting type signatures in my code. As it turns out that seems to be where it gets messy with HList.
There is a template haskell function called "makeLabel" that can generate the boilerplate for you. If you are comfortable not seeing all the type definitions, it might do the trick.
PS: Does anyone know why there are Label-n modules in HList, and which one to use?
I don't but Label4 works for me. Justin
participants (5)
-
Alexander Dunlap
-
David Menendez
-
Edward Z. Yang
-
Günther Schmidt
-
Justin Bailey