# Difference between revisions of "Record access"

From HaskellWiki

(extended record accessors) |
(initialize records with accessor functions) |
||

Line 45: | Line 45: | ||

set :: Accessor r a -> a -> r -> r |
set :: Accessor r a -> a -> r -> r |
||

set f x = snd . f x |
set f x = snd . f x |
||

+ | |||

+ | {- | Set many fields at once. |
||

+ | |||

+ | This function could also be used for initialisation of record, |
||

+ | if record value with undefined fields is provided. |
||

+ | |||

+ | Drawback: |
||

+ | Since all types in a list must have the same type, |
||

+ | you can set only values of the same type. |
||

+ | -} |
||

+ | setMany :: [r -> (a, r)] -> r -> r |
||

+ | setMany = flip (foldl (\x f -> snd (f x))) |
||

+ | |||

+ | {- | |
||

+ | This is a general function, |
||

+ | but it is especially useful for setting many values of different type at once. |
||

+ | -} |
||

+ | compose :: [r -> r] -> r -> r |
||

+ | compose = flip (foldl (flip id)) |
||

{- | Get the value of a field. -} |
{- | Get the value of a field. -} |
||

Line 99: | Line 118: | ||

modifyState second succ |
modifyState second succ |
||

getState second |
getState second |
||

+ | |||

+ | exampleInit :: (Char,Int) |
||

+ | exampleInit = |
||

+ | compose [set first 'b', modify first succ, set second 7] |
||

+ | (undefined,undefined) |
||

+ | -- setMany [first 'b', second 7] (undefined,undefined) |
||

</haskell> |
</haskell> |
||

## Revision as of 08:30, 23 October 2006

Here some proposal for desugared fine functional record field access for HaskellTwo and above.

```
{- |
In Haskell 98 the name of a record field
is automatically also the name of a function which gets the value
of the according field.
E.g. if we have
@
data Pair a b = Pair {first :: a, second :: b}
@
then
@
first :: Pair a b -> a
second :: Pair a b -> b
@
However for setting or modifying a field value
we need to use some syntactic sugar, which is often clumsy.
@
modifyFirst :: (a -> a) -> (Pair a b -> Pair a b)
modifyFirst f r@(Pair {first=a}) = r{first = f a}
@
We propose to extend the meaning of the record field names
to a function which allows setting, getting and modifying values easily.
-}
module RecordAccess where
import Control.Monad.State (MonadState)
import qualified Control.Monad.State as State
{- |
The access functions we propose, look very similar to those
needed for List.mapAccumL (but parameter order is swapped) and State monad.
They get the new value of the field and the record
and return the old value of the field and the record with the updated field.
-}
type Accessor r a = a -> r -> (a, r)
{- *
Access helper functions,
these are similar to State methods and should be in Prelude
-}
{- | Set the value of a field. -}
set :: Accessor r a -> a -> r -> r
set f x = snd . f x
{- | Set many fields at once.
This function could also be used for initialisation of record,
if record value with undefined fields is provided.
Drawback:
Since all types in a list must have the same type,
you can set only values of the same type.
-}
setMany :: [r -> (a, r)] -> r -> r
setMany = flip (foldl (\x f -> snd (f x)))
{- |
This is a general function,
but it is especially useful for setting many values of different type at once.
-}
compose :: [r -> r] -> r -> r
compose = flip (foldl (flip id))
{- | Get the value of a field. -}
get :: Accessor r a -> r -> a
get f = fst . f undefined
{- | Transform the value of a field by a function. -}
modify :: Accessor r a -> (a -> a) -> (r -> r)
modify f g rOld =
let (a,rNew) = f (g a) rOld
in rNew
{- *
Access helper functions in a State monad.
-}
setState :: MonadState r m => Accessor r a -> a -> m ()
setState f x = State.modify (set f x)
getState :: MonadState r m => Accessor r a -> m a
getState f = State.gets (get f)
modifyState :: MonadState r m => Accessor r a -> (a -> a) -> m ()
modifyState f g = State.modify (modify f g)
{- * Example accessors for the pair type -}
{- | Access to the first value of a pair. -}
first :: Accessor (a,b) a
first xNew (xOld,y) = (xOld, (xNew,y))
{- | Access to the second value of a pair. -}
second :: Accessor (a,b) b
second yNew (x,yOld) = (yOld, (x,yNew))
{- * Example accessors for the pair type -}
{- | Example of using 'set', 'get', 'modify'. -}
example :: Int
example =
get second $
modify second succ $
set first 'a' $
('b',7)
exampleState :: State.State (Char,Int) Int
exampleState =
do setState first 'a'
modifyState second succ
getState second
exampleInit :: (Char,Int)
exampleInit =
compose [set first 'b', modify first succ, set second 7]
(undefined,undefined)
-- setMany [first 'b', second 7] (undefined,undefined)
```