purescript / purescript-typelevel-prelude

Types and kinds for basic type-level programming
BSD 3-Clause "New" or "Revised" License
63 stars 21 forks source link

Added RowLabels type class. #25

Closed coot closed 4 years ago

coot commented 7 years ago
labels :: forall l. RowLables l => RLProxy r -> Array String
labelsR :: forall r l. RowToList r l => RowLables l => RProxy r -> Array String

Promissed PR as discussed in https://github.com/purescript/purescript-record/pull/6 I think we could use pushd via ffi instead of cons x xs = [x] <> xs hack.

paf31 commented 7 years ago

I agree, it might be nice to try to improve the performance here a bit. We could add a builder api to arrays, or maybe it could just be as simple as specializing this for some small values of n.

coot commented 7 years ago

Or maybe using your approach with ST monad. I added Array String instance which does the same as the RowLabels class in this PR. Check out this gist.

coot commented 7 years ago

Maybe the way forward is to have a RowLabels (optimised by speciallising up to 10 arguments) and the ST builder published somewhere (in Control.Module.ST.Builder in purescript-st or in a new package).

coot commented 7 years ago

RowLabels at some point can be optimized by the compiler, since all the information is static, then there is no need for the ST builder.

paf31 commented 7 years ago

Yes, let's try that.

coot commented 7 years ago

If you want the ST stuff in purescript-contrib you'll need to create a repo for that. I can maintain it.

coot commented 7 years ago

I added test (in the prelude style in Unit -> Unit monad). Seems there is an issue with the development version of the compiler (the only one I tried). It takes too much time to compile the test file:

time purs compile ...
Compiling Test.Main
purs compile 'bower_components/purescript-*/src/**/*.purs' 'src/**/*.purs'   41.16s user 13.72s system 368% cpu 14.880 total
time pulp test
* Building project in /home/marcin/repos/purescript/purescript-typelevel-prelude
           Src   Lib   All
Warnings   0     0     0  
Errors     0     0     0  
* Build successful.
* Running tests...
* Tests OK.
pulp test  43.16s user 11.92s system 360% cpu 15.283 total
paf31 commented 6 years ago

@coot Thanks!

@LiamGoodacre What do you think of the unrolled loop idea?

LiamGoodacre commented 6 years ago

I'm okay-ish with the unrolling - at least, in the absence of compiler optimisations. However, I'm wondering if we should build a List instead of an Array? If the issue is specifically that arrays have poor cons perf.

paf31 commented 6 years ago

I think that would be fine too, but I'm also a little concerned that we'll have one dictionary lookup and function call per label.