Search Haskell Channel Logs

Tuesday, February 28, 2017

#haskell channel featuring Rupsbant_, mniip, benzrf, dolio, nshepperd_, EvanR, and 19 others.

thoughtpolice 2017-02-28 09:45:41
joseph07: Cool. Apparently S3 is beginning to come back online in read-only mode soon, so hopefully you won't need it forever.
thoughtpolice 2017-02-28 09:45:50
Or at least it is, for some people.
joseph07 2017-02-28 09:46:15
thoughtpolice: thanks for the tip! I was pretty pessimistic for some reason but it totally worked
doomlord 2017-02-28 09:46:26
can haskell make a function generic over the size of a tuple, e.g. get-first (x,y)=x get-first (x,y,z)=x etc
cocreature 2017-02-28 09:46:44
doomlord: no
joseph07 2017-02-28 09:47:13
cocreature: couldn't you make a typeclass for get-first?
doomlord 2017-02-28 09:47:37
and implement that for any types in the tuple slots?
doomlord 2017-02-28 09:48:03
(or would you need to explicitely tell the tuples that they're instances of that typeclass)
cocreature 2017-02-28 09:48:32
joseph07: that kind of works since there is an upper limit on the size of a tuple but it wouldn't otherwise since you would need to create an infinite number of instances
benzrf 2017-02-28 09:48:44
doomlord: you don't want to do this, trust me
doomlord 2017-02-28 09:48:45
heh ok
benzrf 2017-02-28 09:48:45
it's almost never what you want in practice
benzrf 2017-02-28 09:49:00
that said, lens does have a class for this:
joseph07 2017-02-28 09:49:28
doomlord: did you have a particular problem you were trying to solve?
benzrf 2017-02-28 09:49:34
> (1, 2, 3)^?each
lambdabot 2017-02-28 09:49:38
Just 1
benzrf 2017-02-28 09:49:40
> (1, 2, 3, 4) ^? each
lambdabot 2017-02-28 09:49:43
Just 1
benzrf 2017-02-28 09:49:53
this is probably a bad idea, though
benzrf 2017-02-28 09:51:52
bbl
doomlord 2017-02-28 09:58:56
'problem/solve' - i remembered thinking about this more from the pov of having an accessor for records - the same named field in the same slot
yaxu 2017-02-28 10:01:31
Any ideas why testA works fine, and testB fails to work out that `4` is an Int? http://lpaste.net/353074
Sonarpulse 2017-02-28 10:01:46
is it possible for seqing a function to do real work?
cocreature 2017-02-28 10:02:13
Sonarpulse: could you be a bit more precise? I don't understand your question
Sonarpulse 2017-02-28 10:02:16
I can't think of anything one can do with a function without applying it that doesn't already involve eta expansion and thus no work
cocreature 2017-02-28 10:02:47
ah ok
Sonarpulse 2017-02-28 10:03:03
cocreature: what "interesting" computation can happen from seqing a function
cocreature 2017-02-28 10:03:09
Sonarpulse: well you can evaluate to check that it evaluates _to_ a function, e.g. does not throw an exception
Sonarpulse 2017-02-28 10:03:09
except for divergence?
Sonarpulse 2017-02-28 10:03:34
cocreature: ok fair, anything else?
danielsmw 2017-02-28 10:04:27
is it possible to have the dimensionality of a Data.Vector.Unboxed.Mutable MVector element to be specified at runtime?
cocreature 2017-02-28 10:04:30
I don't think so but I don't think functions are fundamentally different here. seqing a function stops once you have evaluated it to a primitive function, sequing an Int stops once you have a primitive int, …
dolio 2017-02-28 10:05:05
let n = ack 4 4 in n `seq` \x -> x
danielsmw 2017-02-28 10:05:21
i've tried making an MVector of (V n k)'s from the Linear library, but I'm finding the type-level dimensionality impossilbe to resolve without specifying the dimensions explicitly at compile time.
dolio 2017-02-28 10:05:28
That computes the Ackermann function at 4 and 4 before evaluating to the identity function.
drostie 2017-02-28 10:06:19
yaxu: well I can tell you why testB fails but not why testA doesn't, does that help?
Sonarpulse 2017-02-28 10:06:27
dolio: ah that we can optomize it away
Sonarpulse 2017-02-28 10:06:35
but one can imagine closing over the value too
yaxu 2017-02-28 10:06:40
drostie: heh, it might
Sonarpulse 2017-02-28 10:06:47
let n = ack 4 4 in n `seq` \x -> + n x
drostie 2017-02-28 10:07:36
yaxu: the literal 4 really means the expression `fromInteger 4` wherever it appears, where the latter 4 is an actual value of type Integer.
Sonarpulse 2017-02-28 10:08:00
cocreature dolio: I think the answer is the only interesting thing that can happen is forcing the evaluation of eventually-closed-over variables
drostie 2017-02-28 10:08:02
yaxu: therefore in testB 4 you really have testB (fromInteger 4).
alexknvl 2017-02-28 10:08:17
Sonarpulse: unsafePerformIO can happen :P
drostie 2017-02-28 10:09:11
yaxu: now the compiler says "well that's a problem, because I might have many types which are both a Num and a Bar. If I did, which one would I choose?"
Sonarpulse 2017-02-28 10:09:17
alexknvl: but how do you force the unsafePerformIO call? :P
drostie 2017-02-28 10:09:44
yaxu: because anyone can define instances anywhere and come up with something useful.
drostie 2017-02-28 10:10:16
So it's now asking you to resolve that "ambiguous type variable" because it does not like the fact that based on whatever instances happen to be loaded, this might have a unique answer or not.
yaxu 2017-02-28 10:10:38
drostie: thanks, this makes some sense
drostie 2017-02-28 10:10:40
yaxu: that means that the fact that testA passes is the real mystery, not the fact that testB does.
drostie 2017-02-28 10:10:48
s/does/fails/
alexknvl 2017-02-28 10:11:36
Sonarpulse: lol. you can also reduce the amount of unevaluated stuff on the heap by sequing afaik
drostie 2017-02-28 10:11:41
yaxu: my guess is that somewhere inside the language this idea of "4 is really fromInteger 4" doesn't really apply if there is some way to view 4 as an Integer in the first place.
yaxu 2017-02-28 10:12:04
drostie: it 'works' for String as well
drostie 2017-02-28 10:13:09
yaxu: that doesn't have ambiguity unless you have the generic string literals -X set, whatever that was.
drostie 2017-02-28 10:13:27
-XOverloadedStrings, I think.
yaxu 2017-02-28 10:13:38
I do have that set
drostie 2017-02-28 10:13:45
kk.
yaxu 2017-02-28 10:14:17
With overloaded strings set, this works for strings, but not overloaded strings
yaxu 2017-02-28 10:14:20
(if that makes sense)
yaxu 2017-02-28 10:14:46
drostie: what I'm trying to do is have a function that can take different types of values that are part of the same typeclass, and have type inference work
drostie 2017-02-28 10:15:51
yaxu: do you want a polymorphic function or something weirder like a polymorphic list?
yaxu 2017-02-28 10:17:07
drostie: A polymorphic function. Thinking about it, I suppose that `show` does this quite happily. I should look at its definition
drostie 2017-02-28 10:17:19
because I mean, `sum :: (Foldable t, Num a) => t a -> a` does just fine as a polymorphic function, but I understand if you're having trouble with "I want to create a list of heterogeneously-typed values that are all polymorphic."
drostie 2017-02-28 10:17:25
er.
drostie 2017-02-28 10:17:44
replace the second "polymorphic" with "all Num instances."
mbw 2017-02-28 10:18:57
You can create such a list with forall.
drostie 2017-02-28 10:19:14
yaxu: right, note that the polymorphism isn't what's getting in your way, it's that you're feeding a polymorphic output into a polymorphic input in a way that totally destroys any attempt to work out which intermediate state you went through.
faberbrain 2017-02-28 10:20:12
hey, i'm wondering if theres a way to make this FromJSON instance less verbose: http://lpaste.net/1577998090089005056
yaxu 2017-02-28 10:20:20
drostie: What I'm actually trying to do is make a function with a parameter that is either a rational number, or a container of rational numbers (in this case the container is a Pattern, as defined by tidalcycles.org)
faberbrain 2017-02-28 10:20:36
problem i'm having is that the json int is wrapped in quotes, so it is failing
drostie 2017-02-28 10:21:40
yaxu: ok. You've got a couple options there.
mbw 2017-02-28 10:21:41
I also have another question. I have a fold that behaves like this: (sumx', sumy', sumxy', sumxx') = foldr f e xys; where: f (x, y) (sx, sy, sxy, sxx) = (x+sx, y+sy, x*y+sxy, x*x+sxx); e = (0, 0, 0, 0).
mbw 2017-02-28 10:21:59
Is it possible to define a Monoid instance for this quadruple that captures this behavior?
mbw 2017-02-28 10:22:21
It should be trivial for the first two elements, but I am not so sure about the other two.
mbw 2017-02-28 10:22:44
Let the input be a list of pairs of Nums.
kadoban 2017-02-28 10:22:46
(<>) in a Monoid is associative. I don't think that will be?
drostie 2017-02-28 10:22:47
yaxu: option 1: sum type. `data Music x = Single (Ratio x) | Multi (Container x)` or what have you. Dead simple to process and accept, make it part of whatever classes it needs to be, good to go.
drostie 2017-02-28 10:23:14
option 2: put every rational in its own container.
drostie 2017-02-28 10:23:38
option 3, a typeclass which admits of two instances, the `Ratio x` and the `Container x`.
drostie 2017-02-28 10:24:29
option 4, like option three but then with Rank2Types you can actually put these things into a monotyped black hole which removes all ability to figure anything else but the typeclass out from them.
mbw 2017-02-28 10:24:31
kadoban: I thought so, too, but I was not sure if I am not simply too unimaginative.
drostie 2017-02-28 10:25:38
Option 4 looks like data Music = Music (forall r. MusicClass r => r) or so. It forgets everything about your data except its instance as a MusicClass.
dolio 2017-02-28 10:25:41
mbw: Adding a pair to a quadruple doesn't make sense for a monoid, because it's not a binary operation on a single type.
kadoban 2017-02-28 10:25:56
Oh, also that
drostie 2017-02-28 10:26:00
yaxu: mix and match those options to your heart's desire.
dolio 2017-02-28 10:26:01
You could define an operation that turns pairs into quadruples, though.
dolio 2017-02-28 10:26:04
Then the monoid is addition.
dolio 2017-02-28 10:26:10
Point-wise.
dolio 2017-02-28 10:26:38
And foldMap with your function produces the equivalent of your foldr.
yaxu 2017-02-28 10:26:59
drostie: I think options 1 and 2 still have some overhead in terms of having to be explicit about what the type of something is
mbw 2017-02-28 10:27:12
dolio: The thing is, I had quite the same situation with a fold that calculated the sum and the maximum. The type signature for foldl also was of type (b -> a -> b), but I was able to convert it.
mbw 2017-02-28 10:27:28
hmm
mbw 2017-02-28 10:28:11
But then a copy couldn't be avoided?
yaxu 2017-02-28 10:28:14
drostie: option 3 is what I have but it only works if I declare the type of the input each time
mbw 2017-02-28 10:28:43
Ah, you mean just "quadruple" each pair?
yaxu 2017-02-28 10:28:50
drostie: I'll read into option 4.. thanks again!
drostie 2017-02-28 10:29:06
np.
mbw 2017-02-28 10:29:35
This would probably be what I did with the sumMax thing. I knew I was uncreative.
dolio 2017-02-28 10:31:11
Yes, (Integer, Integer) -> (Integer,Integer,Integer,Integer) or whatever.
Sonarpulse 2017-02-28 10:31:14
dolio: wait I meant the other side of the seq
Sonarpulse 2017-02-28 10:31:32
closed over ackerman would never be forced until function is run because laziness
dmwit 2017-02-28 10:31:58
mbw: `foldMap (\(x,y) -> (Sum x, Sum y, Sum (x*y), Sum (x*x)))` perhaps.
dolio 2017-02-28 10:32:05
My expression is a function that requires evaluation of the ackermann function when the function is evaluated.
dminuoso 2017-02-28 10:32:29
k
dmwit 2017-02-28 10:33:35
mbw: For sum and max, a similar trick applies. `foldMap (\x -> (Sum x, Max x))`.
mbw 2017-02-28 10:34:00
Yeah, that's what I did, although I defined my own newtype wrapper.
mbw 2017-02-28 10:34:35
But right now I am too stupid to define a mempty = (0,0,0,0)
byorgey 2017-02-28 10:34:59
mbw: why define your own newtype wrapper? The Monoid instance for (Sum a, Max a) comes for free!
mbw 2017-02-28 10:35:04
I have newtype Quad a = Quad (a,a,a,a) and instance Num a => Monoid (Quad a) where ...
mbw 2017-02-28 10:35:25
Yeah, I am just trying to figure out my mistake here.
EvanR 2017-02-28 10:35:29
data Quad a = Quad a a a a ?
dmwit 2017-02-28 10:35:47
EvanR: Basically equivalent to his definition
mbw 2017-02-28 10:35:52
ah
mbw 2017-02-28 10:35:57
I forgot ... Quad!
EvanR 2017-02-28 10:36:00
yeah but i dont see what (a,a,a,a) gets you
byorgey 2017-02-28 10:36:19
well, it's a newtype
mbw 2017-02-28 10:36:29
Force of habit?
byorgey 2017-02-28 10:36:31
so you can GeneralizedNewtypeDeriving and such
EvanR 2017-02-28 10:36:43
ok, theres that
EvanR 2017-02-28 10:36:58
but other than that theres no useful predefined support for (a,a,a,a)
beaky 2017-02-28 10:37:23
hello
mbw 2017-02-28 10:37:42
There actually is a monoid instance already
beaky 2017-02-28 10:37:43
https://ideone.com/9ws6qn is it a good idea to put typeclass constraints in `data` definitions
byorgey 2017-02-28 10:37:58
beaky: no, it's not
beaky 2017-02-28 10:38:03
why not :<
byorgey 2017-02-28 10:38:12
beaky: because they don't mean what you think they mean.
beaky 2017-02-28 10:38:19
hmm what do they mean
byorgey 2017-02-28 10:38:23
I don't remember.
EvanR 2017-02-28 10:38:37
> mempty :: (String,String,String,String)
lambdabot 2017-02-28 10:38:40
("","","","")
implementation 2017-02-28 10:39:43
also there's CarCdr support for (a,a,a,a)
mbw 2017-02-28 10:40:48
What is CarCdr?
mmaruseacph2 2017-02-28 10:41:10
beaky: because you have to replicate those constraint in _every_ function that uses that type
nshepperd_ 2017-02-28 10:41:15
mbw: common pattern with me is to define some monoid like Quad a = Quad a a a a, then a function usually called "inject" (because I'm lazy with naming) that turns one input value into a Quad
beaky 2017-02-28 10:41:18
ah so its more messy
mmaruseacph2 2017-02-28 10:41:39
whereas, if you didn't include the constraint in the definition then you'd only need it in the functions which require the constraint
drostie 2017-02-28 10:42:24
https://hackage.haskell.org/package/acme-lookofdisapproval-0.1/docs/Acme-LookOfDisapproval.html
drostie 2017-02-28 10:42:37
ahahahahahahahaha
mniip 2017-02-28 10:42:42
inject? why not use Control.Applicative.pure
nshepperd_ 2017-02-28 10:42:56
mbw: in this case inject (x, y) = Quad x y (x*y) (x*x)
mniip 2017-02-28 10:43:06
aah
dmwit 2017-02-28 10:43:07
nshepperd_: But why define a new type at all?
beaky 2017-02-28 10:43:08
isnt it a godo thing so that i can verify my type always satisfies that constraint everywhere
dmwit 2017-02-28 10:43:14
(Sum a, Sum a, Sum a, Sum a) works just fine.
Rupsbant_ 2017-02-28 10:43:24
Question: does haskell have problems with x87 floating point instructions that C has? https://gcc.gnu.org/bugzilla/show_bug.cgi?id=323
Rupsbant_ 2017-02-28 10:43:56
Can x+y == x+y fail?
nshepperd_ 2017-02-28 10:43:59
dmwit: Meh. I don't like the look of that as much
byorgey 2017-02-28 10:44:59
beaky: why would you care whether your type satisfies the constraint *everywhere*? Why not just verify that it satisfies the constraint *where the constraint is actually needed*?
mbw 2017-02-28 10:45:01
Probably the newtype thing might be preferable if you want to give it a domain specific name. Otherwise, it should be a matter of taste, no?