Zyxoas 2017-02-19 08:51:31
Okay, stop the presses! I honestly have no idea how this works, since I cooked it up by mixing functions randomly: ^. key "dudes" . _Array . traverse . key "name"
Tuplanolla 2017-02-19 08:51:50
The usual lens experience.
Rembane 2017-02-19 08:51:54
+1
dolio 2017-02-19 08:52:10
bollu: The analogy (although I'm against analogies) that I think is most helpful coming from functional programming to Kan extensions is that Kan extensions are a very general, category theory version of Church encoding.
Zyxoas 2017-02-19 08:52:28
I don't know whta _Array and traverse do, but this does the trick. Now to figure out how to combine two of theses with (,), instead of using zip...
dolio 2017-02-19 08:52:31
(And a dual of church encoding.)
johnw 2017-02-19 08:53:27
that's not the lens he asked for
johnw 2017-02-19 08:53:39
he said he wanted [(foo, meister), (bar, ness)], not [foo, bar]
dolio 2017-02-19 08:54:00
All Church encoding is an example of the Ran U Id ⊣ U principle I originally brought them up.
Tuplanolla 2017-02-19 08:55:04
Actually that might even be the usual Haskell experience.
Tuplanolla 2017-02-19 08:55:20
You have to make peace with not understanding everything completely.
Gurkenglas_ 2017-02-19 08:56:08
Zyxoas, _Array . traverse is values
Tuplanolla 2017-02-19 08:56:26
He left, Gurkenglas_.
lpaste_ 2017-02-19 08:57:24
pv2b pasted "An attempt at a naive prime number generator" at http://lpaste.net/352727
Gurkenglas 2017-02-19 08:57:35
Ah. Is there a better answer to his question than '^.. key "dudes" . values . to (view name &&& view surname)'?
Gurkenglas 2017-02-19 08:58:27
pv2b, genDivisors is takeWhile (<= n*n)
pv2b 2017-02-19 08:58:28
ghc throws a parse error at the | in the guards after the genDevisors helper function in the where clause of isPrime. I'm clearly doing something wrong, but I can't figure out what?
pv2b 2017-02-19 08:58:53
Gurkenglas: ah, thanks, that would probably be more ideomatic haskell, still, my original question stands :)
kadoban 2017-02-19 08:59:24
pv2b: Are you sure it's not on line 10? That looks like you used parens instead of [ ]
pv2b 2017-02-19 09:00:22
kadoban: Of course it is, that'll teach me to look at closer at the line numbers. Thank you!
kadoban 2017-02-19 09:00:38
Anytime
johnw 2017-02-19 09:10:59
I'm not sure of a better way of dealing with inner tuples like this:
johnw 2017-02-19 09:11:01
> "{ \"dudes\": [ {\"name\" : \"foo\", \"surname\" : \"meister\"}, {\"name\" : \"bar\", \"surname\" : \"ness\"} ]}" ^.. key "dudes".values.to (\x -> (x ^. key "name"._String, x ^. key "surname"._String))
lambdabot 2017-02-19 09:11:07
error:
lambdabot 2017-02-19 09:11:07
• Variable not in scope:
lambdabot 2017-02-19 09:11:07
key :: [Char] -> b0 -> [Char] -> Const (Endo [(t, t1)]) [Char]
johnw 2017-02-19 09:11:13
=> [("foo","meister"),("bar","ness")]
confused_functor 2017-02-19 09:16:02
Hi! Is there a way to get this function to typecheck; let f 1 = (0 :: Int8) ; f 2 = (0 :: Int16)
pv2b 2017-02-19 09:16:17
Okay, I've made some progress now, it works (or seems to at least) from a cursory glance. However, it only works if i leave off the type annotation of isPrime. I can't make head or tails of the error. I think it has something to do with trying to compare an Integral with an Integer, but shouldn't that "work"?
pv2b 2017-02-19 09:16:26
Here's the code https://www.irccloud.com/pastebin/sBKbHtn7/
johnw 2017-02-19 09:16:31
confused_functor: no
pv2b 2017-02-19 09:16:44
And the error https://www.irccloud.com/pastebin/J5KVdPAJ/
johnw 2017-02-19 09:16:50
confused_functor: unless you use a general type like: f :: Num a => a
kadoban 2017-02-19 09:17:06
confused_functor: Not really, no. There's some related things that are possible, depending on the goal.
johnw 2017-02-19 09:17:47
no, even then it's not possible
ReinH 2017-02-19 09:18:09
let f 1 = Left (0 :: Int8); let f 2 = Right (0 :: Int16)
confused_functor 2017-02-19 09:18:09
I'm trying to read a binary file. There's a byte in the file, which can be 1, 2, or so on. If the byte is 1, the remaining bytes are 8bits. If its 2, the remaining bytes are 16bits.
kadoban 2017-02-19 09:18:15
let f 1 = Left (0 :: Int8); f 2 = Right (0 :: Int16) would be a possibility, but it's pretty funky. You can let the caller of the function choose the instance of Num if you want instead ... not really clear the goal though.
confused_functor 2017-02-19 09:19:18
I did write a custom data type earlier, that covered all possible values. But it gets messy. And then I have to write the Num instance for each manually. Was wondering if there's a better way
ReinH 2017-02-19 09:19:22
confused_functor: You'll need to use a sum type or, if it suits your domain, choose a single type that accommodates both values (like Int16).
ReinH 2017-02-19 09:19:58
i.e., if you need to know whether to *read* 8 bits or 16 bits, but you don't care which type you store the result as.
confused_functor 2017-02-19 09:21:36
ReinH: makes sense. Is there a way to derive the Num instance in the sum type? data I = I1 Int8 | I2 Int16 deriving Num throws an error
ReinH 2017-02-19 09:21:48
No
ReinH 2017-02-19 09:22:03
It isn't derivable even in theory
ReinH 2017-02-19 09:22:18
the compiler doesn't know whether to choose I1 or I2
confused_functor 2017-02-19 09:22:29
But, here's the thing. At runtime, I only have I1 or I2, definitely not both.
ReinH 2017-02-19 09:22:44
Yes, but what should fromIntegral 1 :: I do?
ReinH 2017-02-19 09:23:13
Anyway, Num isn't derivable
ReinH 2017-02-19 09:23:57
and what should I1 1 + I2 1 be?
ReinH 2017-02-19 09:24:13
so there are both theoretic and practical problems
ReinH 2017-02-19 09:24:19
*theoretical
confused_functor 2017-02-19 09:25:32
makes sense. I can write the Num instance manually then.
ReinH 2017-02-19 09:27:30
Well, what should I1 + I2 be?
ReinH 2017-02-19 09:27:37
er, I1 1 + I2 1
confused_functor 2017-02-19 09:28:14
I1 and I2 cannot occur simultaneously at runtime. I can leave that as undefined. It's a property I know, but not sure how to tell the compiler that.
confused_functor 2017-02-19 09:30:03
that property is guaranteed by the file format standard. So should be safe to just say I1 n + I2 n = undefined
jmcarthur 2017-02-19 09:36:33
You can tell the compiler that by keeping them different types.
sternmull 2017-02-19 09:37:12
can i define a class method parameter type with a constraint instead of a concrete type? I want to have something like "length :: a -> Num" instead of "length :: a -> Int".
pikajude 2017-02-19 09:37:41
sternmull: why do you want to do that
jmcarthur 2017-02-19 09:38:04
How about length :: Num b => a -> b ?
pikajude 2017-02-19 09:38:16
a -> Integer and forall b. Num b => a -> b are identical
pikajude 2017-02-19 09:38:25
except that the compiler doesn't like the second one very much
jmcarthur 2017-02-19 09:38:39
pikajude: What do you mean they are identical?
sternmull 2017-02-19 09:38:40
because one instance will have a Int and another an Int64 as result of length. It would be nice to keep that instead of reducing it to Int in both cases.
reactormonk 2017-02-19 09:38:45
Is there a way to override non-export from within ghci?
reactormonk 2017-02-19 09:38:54
To basically force-export a function.
pikajude 2017-02-19 09:38:57
jmcarthur: forall a. Num a and Integer are the same because you can call fromInteger on the second to get the first
reactormonk 2017-02-19 09:39:04
Debugging purposes.
pikajude 2017-02-19 09:39:16
jmcarthur: as far as return values are concerned.
pikajude 2017-02-19 09:39:26
which in this case is what's going on
jmcarthur 2017-02-19 09:39:36
pikajude: That just means that Integer is a subtype of (Num a => a), but it doesn't mean that every (Num a => a) might as well just be an Integer.
pikajude 2017-02-19 09:39:51
well, i mean in this case specifically
sternmull 2017-02-19 09:40:13
"length :: Num b => a -> b" look good. Thanks!
jmcarthur 2017-02-19 09:40:31
I also disagree with that. Perhaps you might want to have a different evaluation strategy than Integer would permit, for example.
pikajude 2017-02-19 09:40:46
ok
sternmull 2017-02-19 09:42:15
the reason i want to do this is to have a type class that lazy and strict ByteStrings are instances of. So that i can write generic functions that work for both without having me to explicitly call the functions for the exact type.
ReinH 2017-02-19 09:42:47
You can, e.g., lazily calculate the length such that you can determine that an infinite list has more than 65 elements, but not with Integer.