[Haskell-cafe] ANN: has-0.4 Entity based records

HASHIMOTO, Yusaku nonowarn at gmail.com
Sat May 22 18:53:23 EDT 2010


>> There are many libraries to write function "takes an record has Foo
>> and Bar and returns something." But writing type of the function is
>> still difficult. I can't write such types using HList or records
>> without reading documents. I think, using has, There's few effort to
>> write such types.
>
> In which manner do you need to read less documentation to write:
>
> ] f :: Has Foo r => r -> ...
>
> Instead when using HList:
>
> ] f :: HasField Foo record fieldType => ...

HasField only gives projection function (hLookupByLabel), but Has
gives projection, injection and modification function.

If I want to write a generic function injecting a value into field Foo
in a record by HList, I should read documentation more.

>> I think `has' fits the needs of Haskellers who have the good habit of
>> writing a type of a function before its definition.
>
> What does this mean exactly in terms of the type inference possible?

Probably, yes. it's still fragile due to some reasons e.g. the
behavior of UndecidableInstances language extension.

> import Data.Has
> data Foo = Foo; type instance TypeOf Foo = Int
> data Bar = Bar; type instance TypeOf Bar = Int
> f r = (Foo ^. r) + (Bar ^. r)

*Main> :t f
f :: forall s.
     (Contains (Labelled Foo Int) s, Contains (Labelled Bar Int) s) =>
     s -> TypeOf Foo

-nwn


More information about the Haskell-Cafe mailing list