Search Haskell Channel Logs

Tuesday, January 31, 2017

#haskell channel featuring Ferdirand, jophish, exferenceBot, noan, thatguy, zipper, and 14 others.

ocharles 2017-01-30 23:45:10
or more, I don't know what my options are
noan 2017-01-30 23:45:25
laz, thanks
laz 2017-01-30 23:45:27
noan: according to http://math.chapman.edu/~jipsen/structures/doku.php/monoids
roxxik 2017-01-30 23:45:49
laz: i often see it being called product
merijn 2017-01-30 23:45:57
ocharles: Well an absolute consistency would be that every thread always agrees on the exact order in which things have happened
roxxik 2017-01-30 23:45:59
and written by x in a circle
merijn 2017-01-30 23:46:08
ocharles: I believe writeIORef is sequential consistency
cocreature 2017-01-30 23:46:57
there is a "Memory Model" section at the bottom of the Data.IORef haddocs
merijn 2017-01-30 23:46:58
ocharles: Which translates to "in hindsight threads agree on *A* sequential order of operations which explains the observed behaviour, but that is not necessarily the actual behaviour"
jophish 2017-01-30 23:47:39
for logging an incrementing counter that shouldn't be a problem at all I'd think
merijn 2017-01-30 23:47:44
ocharles: That is, "thread A writes an IORef, thread B reads an IORef (at a later point in absolute time)", but thread B might (in sequential consistency) see the old unupdated value
merijn 2017-01-30 23:48:10
ocharles: Since there is a valid sequential ordering of operations in which 'B' reads before 'A''s write happens.
merijn 2017-01-30 23:51:11
iow, the wallclock ordering of events does not have to map to the logical ordering of events, as long as the logical ordering is a unique sequential order that accurately reproduces the observed behaviour
ocharles 2017-01-30 23:51:17
jophish: you mean you think non-atomic variations are probably ok?
merijn 2017-01-30 23:51:49
ocharles: Yeah, for logging it's usually fine to have sequential consistency
ocharles 2017-01-30 23:52:12
10x less allocation pressure is nice
roxxik 2017-01-30 23:52:41
i think MVar's are way simpler here, just treat them as mutex (empty: lock is taken, full: lock is released), don't know about allocation pressure
clinton 2017-01-30 23:53:07
is there a library that gives composition functions like this: (b -> c) -> (a1 -> ... -> an -> b) -> a1 -> ... -> an -> c?
merijn 2017-01-30 23:53:17
ocharles: Lemme put it this was: suppose your logging thread appears to have polled your IORef magically slightly before an operation happened (albeit factually happening slightly after), does that impact you?
clinton 2017-01-30 23:53:41
they're coming up frequently in my code, and whilst I could write my own I suspect someone already has and I'd rather stick with their conventionn
clinton 2017-01-30 23:53:46
hoogle comes up with nothing though
merijn 2017-01-30 23:54:04
clinton: Writing something like that isn't something you can do in general
ocharles 2017-01-30 23:54:08
clinton: I would advise against composition operators here. They might seem appealing, but imo they just introduce code noise
merijn 2017-01-30 23:54:17
clinton: Consider this: what if 'b' or 'c' are function types themselves?
mrkgnao 2017-01-30 23:56:22
sup, #haskell
mrkgnao 2017-01-30 23:56:45
I have a sorta-embarrassing question.
clinton 2017-01-30 23:57:08
merijn: indeed they are function types. In my actual problem I'm unwrapping an argument and forwarding it to about a dozen class functions. In all of these functions the unwrapped argument needs to be passed as the last argument
clinton 2017-01-30 23:57:48
So I actually need stuff like this: (b -> c) -> (a -> b -> d) -> a -> c -> d
clinton 2017-01-30 23:58:01
which is close to "." but not quite
clinton 2017-01-30 23:58:43
or this (b -> c) -> (a1 -> a2 -> b -> d) -> a1 -> a2 -> c -> d
Axman6 2017-01-30 23:58:43
mrkgnao: we're all friends, unless you've written some javascript and no longer remember how it works and under what conditions
ocharles 2017-01-30 23:58:52
sweet, with just writeIORef incrementing the request counter takes 0.5 nanoseconds (95% quantile). I think I can handwave that as "no performance impact"
Ferdirand 2017-01-30 23:59:32
:t \f g -> (((f.).).).g
lambdabot 2017-01-30 23:59:33
(b -> c) -> (a -> a1 -> a2 -> a3 -> b) -> a -> a1 -> a2 -> a3 -> c
Ferdirand 2017-01-30 23:59:44
it's not very pretty, but...
Axman6 2017-01-31 00:00:02
isn't that just contramap g?
Axman6 2017-01-31 00:00:31
oh not at all
roxxik 2017-01-31 00:00:33
isn't IORef basically a nice wrapper around direct memory access?
Axman6 2017-01-31 00:00:37
hmm
mrkgnao 2017-01-31 00:00:52
Axman6: :)
Axman6 2017-01-31 00:00:54
roxxik: not quite, it's more like a mutable pointer
mrkgnao 2017-01-31 00:01:28
so my problem is that I want to update an element of a list that's a field in a record datatype
roxxik 2017-01-31 00:01:31
ocharles: so your modifyIORef r (+1) should get compiled down to some inc instruction, depending on architecture. and those are fast :)
Axman6 2017-01-31 00:01:31
so you can mutate what it points to, not mutate a itself
mrkgnao 2017-01-31 00:01:42
what I want is basically a pointer. :(
Axman6 2017-01-31 00:01:42
mrkgnao: sounds like you need lenses
ocharles 2017-01-31 00:01:49
roxxik: yep, pretty much :)
mrkgnao 2017-01-31 00:02:05
I am using lenses, but I don't know how to use them to change elements of lists
roxxik 2017-01-31 00:02:16
ix ?
Axman6 2017-01-31 00:02:22
mrkgnao: can you give a more concrete example?
mrkgnao 2017-01-31 00:02:28
yeah
Axman6 2017-01-31 00:02:33
do you need to modify a specific element?
mrkgnao 2017-01-31 00:02:48
Yes, but not by index (although I can do that).
Axman6 2017-01-31 00:03:05
:t ix 3 . _Just %~ (+3)
lambdabot 2017-01-31 00:03:07
(IxValue t ~ Maybe a, Ixed t, Num (Index t), Num a) => t -> t
mrkgnao 2017-01-31 00:03:19
whoa
Axman6 2017-01-31 00:03:29
:t ix 3 . _Just %~ (+3) `asApliedTo` (undefined :: [Maybe Int]
roxxik 2017-01-31 00:03:29
how do you find your element?
lambdabot 2017-01-31 00:03:31
error:
lambdabot 2017-01-31 00:03:31
parse error (possibly incorrect indentation or mismatched brackets)
Axman6 2017-01-31 00:03:35
:t ix 3 . _Just %~ (+3) `asApliedTo` (undefined :: [Maybe Int])
lambdabot 2017-01-31 00:03:37
error:
lambdabot 2017-01-31 00:03:37
• Couldn't match type 'IxValue t0' with 'Maybe a0'
lambdabot 2017-01-31 00:03:37
Expected type: (Maybe a0 -> Identity (Maybe a0))
Axman6 2017-01-31 00:03:46
:t ix 3 . _Just %~ (+3) `asAppliedTo` (undefined :: [Maybe Int])
lambdabot 2017-01-31 00:03:49
[Maybe Int] -> [Maybe Int]
mrkgnao 2017-01-31 00:04:19
Axman6: what does your first example do?
Axman6 2017-01-31 00:04:21
mrkgnao: do you need to modify elements matching a specific predicate?
thatguy 2017-01-31 00:04:42
Can anyone explain me the difference between ! and !! when evaluating an array element
clinton 2017-01-31 00:04:45
is there a way to do a signature search of all of hackage?
Axman6 2017-01-31 00:04:55
[Nothing, Just 7, Just 3,Nothing] & ix 1 . _ Just %~ (+3)
Axman6 2017-01-31 00:05:01
> [Nothing, Just 7, Just 3,Nothing] & ix 1 . _ Just %~ (+3)
lambdabot 2017-01-31 00:05:07
error:
lambdabot 2017-01-31 00:05:07
• Found hole:
lambdabot 2017-01-31 00:05:07
_ :: (a0 -> Maybe a0)
Axman6 2017-01-31 00:05:14
> [Nothing, Just 7, Just 3,Nothing] & ix 1 . _Just %~ (+3)
lambdabot 2017-01-31 00:05:18
[Nothing,Just 10,Just 3,Nothing]
Axman6 2017-01-31 00:05:21
bloogy hell, I am bad at typing tonight
mrkgnao 2017-01-31 00:05:29
bloogy hell indeed :)
mrkgnao 2017-01-31 00:05:36
I think I'll work with indices for now
Axman6 2017-01-31 00:05:39
clinton: that's hoogle
mrkgnao 2017-01-31 00:05:47
in which case the last one should work
roxxik 2017-01-31 00:05:48
clinton: hayoo or hoogle
Axman6 2017-01-31 00:05:52
mrkgnao: what did you actually want to do? =)
Axman6 2017-01-31 00:06:30
mrkgnao: so that expression will increment the value in the second element of a list, as long as it is a Just constructor
roxxik 2017-01-31 00:08:00
just for fun: how would you increment the value in the second just value of a list?
mrkgnao 2017-01-31 00:08:38
Axman6: I have a VM datatype that has a [Process] list.
mrkgnao 2017-01-31 00:09:21
I want to step each Process, which changes some state associated with the VM as well as increments a counter associated to the Process.
Axman6 2017-01-31 00:09:32
roxxik: you'd probably use an indexed traversal for that (an optic that provides both the element, and its index)
Axman6 2017-01-31 00:09:53
mrkgnao: for all processes? or a specific one?
mrkgnao 2017-01-31 00:10:01
all
mrkgnao 2017-01-31 00:10:22
but the increment depends on the result of the step function.
Axman6 2017-01-31 00:10:37
then you probably don't need the ix, you can yse traverse . _Foo . bar to focus on all the elements of the list
roxxik 2017-01-31 00:11:04
just write a function Process -> Process and map that?
Axman6 2017-01-31 00:11:23
the proper answer will depend on several things though, like if the step function is stateful or monadic or something
Axman6 2017-01-31 00:11:33
yeah this sounds an awful lot like map =)
mrkgnao 2017-01-31 00:11:36
roxxik: but the step function also has to modify other parts of the VM state.
mrkgnao 2017-01-31 00:12:01
Axman6: it's inside what is my first real monad transformer stack!
Axman6 2017-01-31 00:12:39
:o
Axman6 2017-01-31 00:12:57
do you need any monadic context in this step function?
roxxik 2017-01-31 00:13:01
you need to do two things? step and inc? step needs access to VM and updates a Process? and afterwards you want to update (inc) each process? with some information gathered during step?
Axman6 2017-01-31 00:13:07
or can you just use Process -> Process?
Axman6 2017-01-31 00:13:48
traverse will be useful if you need the monadic context
Axman6 2017-01-31 00:13:50
:t traverse
mrkgnao 2017-01-31 00:13:51
no, as I said, the step function modifies other parts of the VM state, not just the part associated to the specific process.
roxxik 2017-01-31 00:13:51
maybe `c -> Process -> Process` where c is the context you need
lambdabot 2017-01-31 00:13:53
(Traversable t, Applicative f) => (a -> f b) -> t a -> f (t b)
roxxik 2017-01-31 00:13:59
ah it modifies those too
Axman6 2017-01-31 00:14:23
:t traverse :: (a -> Maybe b) -> [a] -> Maybe [b]
lambdabot 2017-01-31 00:14:25
(a -> Maybe b) -> [a] -> Maybe [b]
Axman6 2017-01-31 00:14:42
substitute Maybe with your awesome monad stack
mrkgnao 2017-01-31 00:15:02
okay, let's see where this gets me!
roxxik 2017-01-31 00:15:20
step updates stuff outside of the list... so you definitly need more than traverse
mrkgnao 2017-01-31 00:15:28
(it's an ExceptT and a StateT. nothing fancy, but I think things are starting to make sense)
Axman6 2017-01-31 00:15:28
(also note that traverse doesn't need Monad, only Applicative, whch is pretty nice)
roxxik 2017-01-31 00:15:29
(i think)
mrkgnao 2017-01-31 00:15:36
roxxik: yes
mrkgnao 2017-01-31 00:15:51
maybe using indices is the way to go. :(
roxxik 2017-01-31 00:16:43
nah if you have Process -> State VM Process then you could use traverse, but that's not particularly nice, because now you have the process two times
roxxik 2017-01-31 00:17:37
do you need to access the whole VM or other processes or just some specific stuff?
mrkgnao 2017-01-31 00:20:26
roxxik: the VM has a Memory field that I need to change, that's all
roxxik 2017-01-31 00:21:12
then go with State (Memory,Process) () or similar
Axman6 2017-01-31 00:21:26
you probably don't need to process the lsit twice, just use (fmap inc . statefulProcessor) as the first argument to traverse
roxxik 2017-01-31 00:21:30
or Process -> State Memory Process and use traverse
zipper 2017-01-31 00:21:46
NOT STATE! OMG NOT STATE! I DON'T UNDERSTAND STATE!!! :(
Axman6 2017-01-31 00:21:57
state's so easy though
roxxik 2017-01-31 00:22:09
State a <=> s -> (s,a)
roxxik 2017-01-31 00:22:15
State s a <=> s -> (s,a)
roxxik 2017-01-31 00:22:28
State s a <=> s -> (a,s)
zipper 2017-01-31 00:23:47
Axman6: Is it? :( well maybe I need to squint harder.
Ferdirand 2017-01-31 00:27:16
why s -> (s,a) and not s -> (a,s) indeed ?
mrkgnao 2017-01-31 00:27:34
pretty sure that's just how it's defined
Ferdirand 2017-01-31 00:27:40
given that the functor instance for 2-tuples operate on the second element
Ferdirand 2017-01-31 00:27:49
sorry i got that reversed
Ferdirand 2017-01-31 00:28:05
s -> (s,a) looks more natural to me
usman 2017-01-31 00:36:24
join
usman 2017-01-31 00:37:07
is it helpful for haskell?
usman 2017-01-31 00:37:47
hey? anybody there?
quchen 2017-01-31 00:37:51
zipper: State is something that has get and put. You don't need to understand the internals to use it.
Xnuk 2017-01-31 00:37:58
> join (Just Nothing)
lambdabot 2017-01-31 00:38:01
Nothing
quchen 2017-01-31 00:38:08
It is instructional to implement State yourself, but not required at all.
Xnuk 2017-01-31 00:38:18
> join (Just (Just 3))
lambdabot 2017-01-31 00:38:21
Just 3
ski 2017-01-31 00:38:29
Ferdirand : yes, i'd say it's more natural from a theoretical standpoint
ski 2017-01-31 00:38:32
also cf.
ski 2017-01-31 00:38:33
@type mapAccumL
lambdabot 2017-01-31 00:38:35
Traversable t => (a -> b -> (a, c)) -> a -> t b -> (a, t c)
ski 2017-01-31 00:38:38
(and `mapAccumR')
merijn 2017-01-31 00:39:15
roxxik: The best homework for understanding State is just reimplementing it yourself: https://gist.github.com/merijn/098106abd45c940dab09
roxxik 2017-01-31 00:39:46
yup
quchen 2017-01-31 00:39:54
usman: Example: a worker thread receives a computation (type IO a) from the scheduler. Reading from the worker thread gives it an »IO «. Since the channel contains »IO a«, reading from the channel is an action of type »IO (IO a)«. Executing this thing in the worker then looks like »result <- join (readChannel chan)«.
roxxik 2017-01-31 00:40:14
i just stumbled on Cont's Applicative instance (because i played around with exferenceBot)
ski 2017-01-31 00:40:17
(in the `Writer' case, it would be nicer to use `(w,a)', since then it would fit better with a "raw" `Monoid w => MonadWriter w (w,)')
merijn 2017-01-31 00:40:19
roxxik: Incidentally, the best homework for understanding transformers is generalising State like StateT like in that gist :)
roxxik 2017-01-31 00:40:28
:exf (((a -> b) -> r) -> r) -> ((a -> r) -> r) -> (b -> r) -> r
exferenceBot 2017-01-31 00:40:28
\ f1 f2 f3 -> f2 (\ f -> f1 (\ f9 -> f3 (f9 f)))
exferenceBot 2017-01-31 00:40:29
\ f1 f2 f3 -> f1 (\ f6 -> f2 (f3 . f6))
merijn 2017-01-31 00:40:30
roxxik: Cont still perpetually confuses me :p
quchen 2017-01-31 00:40:48
That was … =<
roxxik 2017-01-31 00:40:49
why is the second version in use and not the first (or better: what's the difference?)
roxxik 2017-01-31 00:40:59
i think it is something about ordering...
ski 2017-01-31 00:41:14
`ContT' can be good when you're doing "dynamic resource wrappers" (`withCString',&c.), and "quantifiers"
roxxik 2017-01-31 00:41:19
i really like how exf can infer ap for state: :exf (s -> (a -> b,s)) -> (s -> (a,s)) -> s -> (b,s)
roxxik 2017-01-31 00:41:20
:exf (s -> (a -> b,s)) -> (s -> (a,s)) -> s -> (b,s)
exferenceBot 2017-01-31 00:41:21
\ f1 f2 c ->
exferenceBot 2017-01-31 00:41:21
let ((,) f7 h) = f1 c
exferenceBot 2017-01-31 00:41:21
((,) k l) = f2 h
exferenceBot 2017-01-31 00:41:21
in (f7 k, l)
exferenceBot 2017-01-31 00:41:21
\ f1 f2 c ->
exferenceBot 2017-01-31 00:41:23
let ((,) g h) = f2 c
exferenceBot 2017-01-31 00:41:25
((,) f11 l) = f1 h
exferenceBot 2017-01-31 00:41:27
in (f11 g, l)
ski 2017-01-31 00:41:30
quchen : no `(<*>)'
roxxik 2017-01-31 00:41:40
one for forward state and one for backward state
quchen 2017-01-31 00:41:42
Ah, right.
merijn 2017-01-31 00:41:44
ski: I know what it's good for, but I try really hard not too think about it too much, because thinking hard about Cont(T) has a tendency to fry my brain
ski 2017-01-31 00:42:34
some months ago, someone had some code in here, where i suggested using `ContT' to avoid boilerplate code
ski 2017-01-31 00:42:40
they seemed to be happy with it
ski 2017-01-31 00:43:22
(they came looking for a better way to express what they were doing, cutting down on the "noise" and inconvenience)
merijn 2017-01-31 00:43:26
ContT with bracket is nice, although I usually think the ContT wrapping adds about as much noise as just the regular version
ski 2017-01-31 00:43:44
it certainly depends on the situation
ski 2017-01-31 00:44:44
roxxik : you should (learn to) be able to derive those implementations yourself, by "following the types"
Cale 2017-01-31 00:44:56
:t runCont . sequence . map cont
lambdabot 2017-01-31 00:44:59
[(a -> r) -> r] -> ([a] -> r) -> r