Aku 2017-02-25 01:47:24
a can be anyhting
Aku 2017-02-25 01:47:29
like Int,String
Aku 2017-02-25 01:47:38
Is that what you expect?
Aku 2017-02-25 01:47:52
I mean I....
bollu 2017-02-25 01:47:55
ok
bollu 2017-02-25 01:47:59
how do you use "Parser a"?
bollu 2017-02-25 01:48:04
or, let's take a simpler example
bollu 2017-02-25 01:48:06
have you seen Maybe?
Aku 2017-02-25 01:48:37
ya seen
bollu 2017-02-25 01:48:38
so, what does Maybe a represent?
bollu 2017-02-25 01:48:38
(Maybe a)
Aku 2017-02-25 01:48:38
Just a|Nothing
bollu 2017-02-25 01:48:38
right
bollu 2017-02-25 01:48:38
but conceptually
bollu 2017-02-25 01:48:47
it represents the possibility of having a value "a"
bollu 2017-02-25 01:48:52
or perhaps not having anything
Aku 2017-02-25 01:48:53
Okay
Aku 2017-02-25 01:49:00
Okhay
bollu 2017-02-25 01:49:01
we *implement* it as Just a | Nothing
bollu 2017-02-25 01:49:04
but that doesn't matter
Aku 2017-02-25 01:49:11
Allright
Aku 2017-02-25 01:49:17
Then?
Aku 2017-02-25 01:49:23
I am curious
bollu 2017-02-25 01:49:26
I can write the same thing in some complex way, but that doesn't matter. The _idea_ is that it represents the possiblity of a vaue
bollu 2017-02-25 01:49:31
now, consider ([a])
Aku 2017-02-25 01:49:40
Ya
bollu 2017-02-25 01:49:40
I ask the same question: what does [a] represent?
Aku 2017-02-25 01:50:13
It doesn't show a possibility here but just that it is a list of type a
Aku 2017-02-25 01:50:21
Is it?
bollu 2017-02-25 01:50:31
well, sure
bollu 2017-02-25 01:50:40
but, like, what does it mean "to have a list of stuff"?
bollu 2017-02-25 01:50:49
does it not mean "zero or more occurences of a"?
Aku 2017-02-25 01:50:58
Ya It does
bollu 2017-02-25 01:51:10
if you consider [Int], [], [1], [1, 2], … are all valid instances of [a]
bollu 2017-02-25 01:51:11
OK
Aku 2017-02-25 01:51:14
Okay..so thats what is expected
bollu 2017-02-25 01:51:21
so, now, similary, what does "Parser a" represent?
bollu 2017-02-25 01:51:25
(don't give me the type)
Aku 2017-02-25 01:51:52
so u want valid instances of Parser a
Aku 2017-02-25 01:52:03
?
Aku 2017-02-25 01:52:38
Srry ...its difficult
bollu 2017-02-25 01:52:40
I want what "Parser a" means. "Maybe a" means "possibility of having a". [a] means "zero or more occrences of a". (a -> b) means "something that takes a and returns b". (a, b) means "something that holds both a and b"
bollu 2017-02-25 01:52:45
no, don't worry
bollu 2017-02-25 01:52:51
it's a little weird to think of types as carrying meaning
bollu 2017-02-25 01:53:03
it's a slightly strange leap to make at first :)
Aku 2017-02-25 01:53:03
let me think
bollu 2017-02-25 01:53:06
sure
bollu 2017-02-25 01:53:20
so, to understand what "Parser a" means, we need to see what it is aliasing. (Since it is a type alias)
bollu 2017-02-25 01:53:31
it is an alias of [Token] -> [(a, Token)]
bollu 2017-02-25 01:53:37
now, this is not some random type
bollu 2017-02-25 01:53:44
this somehow means "Parser a " to us
bollu 2017-02-25 01:54:01
so how do we interpret the ([Token] -> [(a, Token)]) to mean "oh, it is a parser!"?
Aku 2017-02-25 01:54:10
okay
bollu 2017-02-25 01:54:30
you can say "I don't know"
bollu 2017-02-25 01:54:30
which is all right
Aku 2017-02-25 01:54:30
we can interpret it as a function
bollu 2017-02-25 01:54:34
yes
bollu 2017-02-25 01:54:41
which takes what as input and gives out what as output?
Aku 2017-02-25 01:54:45
ya
bollu 2017-02-25 01:55:03
no, I'm asking
bollu 2017-02-25 01:55:07
what is the input and what is the output?
Aku 2017-02-25 01:55:23
my typing is slow...
bollu 2017-02-25 01:55:24
the low level answer would be [Token] is the input and [(a, Token)] is the output
bollu 2017-02-25 01:55:26
yeah
bollu 2017-02-25 01:55:30
no worries
bollu 2017-02-25 01:55:36
but clearly those things have _meanings_ to us
bollu 2017-02-25 01:55:41
so, what is the [Token] representing
Aku 2017-02-25 01:55:44
Ya that's what I was typing
Aku 2017-02-25 01:55:51
It a list of Strings
Aku 2017-02-25 01:56:16
Zero or more occurences of String
bollu 2017-02-25 01:56:16
sure
bollu 2017-02-25 01:56:27
but, like, we are thinking of it as the "unprocessed input tokens" right?
bollu 2017-02-25 01:56:29
are we not?
Aku 2017-02-25 01:56:36
Yess
bollu 2017-02-25 01:56:46
now, what about the output?
Aku 2017-02-25 01:56:48
We are
bollu 2017-02-25 01:56:51
how are we thinking of [(a, Token)]?
Aku 2017-02-25 01:57:18
As processed output list
bollu 2017-02-25 01:57:26
OK, but why a list?
bollu 2017-02-25 01:57:29
why not just (a, Token)
bollu 2017-02-25 01:57:40
[(a, [Token])] sorry*
bollu 2017-02-25 01:57:44
why not just (a, [Token])
Aku 2017-02-25 01:58:01
Because there could be more occurences
Aku 2017-02-25 01:58:09
like in case of ambiguous gmrs
bollu 2017-02-25 01:58:23
OK, so what does (a, [Token]) mean first of all?
bollu 2017-02-25 01:58:33
(you are right about that BTW, Why we use [(a, Token)]
Aku 2017-02-25 01:59:09
here a is what we processed and token is the remaining list to be processed
bollu 2017-02-25 01:59:24
right
Aku 2017-02-25 01:59:51
Then?
bollu 2017-02-25 01:59:58
so we return [(a, [Token]) to represent collection of possible parses, where a "possible parse" is an "a" which we have parsed, and the rest of the input that is not processed, correct?
Aku 2017-02-25 02:00:24
ya perfect!
bollu 2017-02-25 02:01:46
and so, "Parser a" is a function, that takes a list of unprocessed tokens ([Token]]) and return [(a, [Token]) to represent collection of possible parses, where a "possible parse" is an "a" which we have parsed, and the rest of the input that is not processed. right?
Aku 2017-02-25 02:01:49
Yes
bollu 2017-02-25 02:01:49
OK
bollu 2017-02-25 02:01:49
so now, consider Parser [p]
Aku 2017-02-25 02:01:49
Then...curious
bollu 2017-02-25 02:01:49
replace "a" in the previous sentence with [p]
bollu 2017-02-25 02:01:49
what do you get?
Aku 2017-02-25 02:02:01
just a min..
bollu 2017-02-25 02:02:06
sure
Aku 2017-02-25 02:04:10
Now the difference is that what we have parsed is a collection
Aku 2017-02-25 02:04:10
[a]
bollu 2017-02-25 02:04:10
yes
bollu 2017-02-25 02:04:10
so we are able to parse a collection of things
bollu 2017-02-25 02:04:10
given a parser for one thing
bollu 2017-02-25 02:04:10
(Parser a)
Aku 2017-02-25 02:04:10
ya
bollu 2017-02-25 02:04:10
does this seem "logical" to you?
bollu 2017-02-25 02:04:10
like, doable?
Aku 2017-02-25 02:04:10
No eactly
Aku 2017-02-25 02:04:10
exactly
bollu 2017-02-25 02:04:10
it doesn't seem possible?
Aku 2017-02-25 02:04:11
Not exactly
bollu 2017-02-25 02:04:14
OK
bollu 2017-02-25 02:04:40
consider ["50", "50", "50", "30"] as my input list.
Aku 2017-02-25 02:04:41
ya it doesn't...I mean I dont get how we do it
bollu 2017-02-25 02:04:48
now, if I have a (Parser Int)
bollu 2017-02-25 02:04:55
that can take "50" and return 50 :: Int
bollu 2017-02-25 02:05:04
can I not use this parser
bollu 2017-02-25 02:05:11
to parse everything else in the list?
Aku 2017-02-25 02:05:30
Ya i have to use it recursively
bollu 2017-02-25 02:05:37
forget implementation
bollu 2017-02-25 02:05:42
let's try and see if this seems reasonable first of all
Aku 2017-02-25 02:05:51
Ya I can use
Aku 2017-02-25 02:06:11
first parse the first 50 and then parse the second
Aku 2017-02-25 02:06:13
and so on
bollu 2017-02-25 02:06:15
yes
bollu 2017-02-25 02:06:15
now
bollu 2017-02-25 02:06:18
what if I give you
bollu 2017-02-25 02:06:24
["50", "50", "a"]
bollu 2017-02-25 02:06:29
then what should the behaviour be?
bollu 2017-02-25 02:06:36
how much do we parse, and where do we stop processing?
Aku 2017-02-25 02:07:01
We should be stopping when the token /= 50
bollu 2017-02-25 02:07:10
not necessarily
bollu 2017-02-25 02:07:19
we have a parser that can parse integers
bollu 2017-02-25 02:07:21
so the behaviour I would aim for
bollu 2017-02-25 02:07:28
is to try to parse integers as much as we can
Aku 2017-02-25 02:07:29
Ya right
bollu 2017-02-25 02:07:35
and stop when there are no more integers
bollu 2017-02-25 02:07:39
so, if I give you the input stream
Aku 2017-02-25 02:07:43
Thats a broader set
bollu 2017-02-25 02:07:47
["30", "20", "10", "a", "b"]
bollu 2017-02-25 02:07:49
well
bollu 2017-02-25 02:07:59
sur
bollu 2017-02-25 02:08:02
sure
Aku 2017-02-25 02:08:10
But the tokens are all strings
bollu 2017-02-25 02:08:14
yes
bollu 2017-02-25 02:08:22
but your parser knows if it can parse something or not
bollu 2017-02-25 02:08:31
if it can, it will return a list of possible parses
bollu 2017-02-25 02:08:35
if the parse was not possible
bollu 2017-02-25 02:08:38
it will return empty list
Aku 2017-02-25 02:08:38
Yess right
Aku 2017-02-25 02:08:41
yes
Aku 2017-02-25 02:08:45
perfect!
bollu 2017-02-25 02:08:47
so
bollu 2017-02-25 02:08:52
we need to keep on trying to parse
bollu 2017-02-25 02:08:56
till we hit empty list
bollu 2017-02-25 02:08:59
at which point we stop parsing
Aku 2017-02-25 02:09:00
ohhhkay
bollu 2017-02-25 02:09:01
correct?
bollu 2017-02-25 02:09:40
(I'm asking)
Aku 2017-02-25 02:09:53
When the parse fails that is returns an empty list
bollu 2017-02-25 02:10:04
yes
bollu 2017-02-25 02:10:04
we can return how much we consumed
bollu 2017-02-25 02:10:06
because remember, we are trying to do "one or more"
bollu 2017-02-25 02:10:08
which means
bollu 2017-02-25 02:10:12
we keep on trying to parse as much as we can
bollu 2017-02-25 02:10:19
and when the parser says "you've exhaused this"
Aku 2017-02-25 02:10:25
Yes we can but how ?
bollu 2017-02-25 02:10:29
OK
bollu 2017-02-25 02:10:46
now
bollu 2017-02-25 02:10:49
let us go back to your code
Aku 2017-02-25 02:10:51
ya....
bollu 2017-02-25 02:11:51
can you tell me how your definition of pAlt works? you have a function that takes 2 inputs and returns one output (pAlt :: Parser a -> Parser a -> Parser a). BUT, when you are *defining* the function, you are taking *three inputs* (pAlt p1 p2 toks = …). Is this not wrong? :O
Aku 2017-02-25 02:12:31
That's exactly what I didn't understand since morning!
Aku 2017-02-25 02:12:45
Is it a partial function?
bollu 2017-02-25 02:12:53
OK
bollu 2017-02-25 02:12:56
so to understand this
Aku 2017-02-25 02:13:20
yes
bollu 2017-02-25 02:13:25
expand out the type pAlt :: Parser a -> Parser a -> Parser a
bollu 2017-02-25 02:13:27
expand "Parser a"
bollu 2017-02-25 02:13:31
wait
Aku 2017-02-25 02:13:32
okay
bollu 2017-02-25 02:13:33
first of all
bollu 2017-02-25 02:13:37
do you know the difference between
bollu 2017-02-25 02:13:44
f :: (a -> b) -> c and g :: a -> (b -> c)
bollu 2017-02-25 02:13:45
?
Aku 2017-02-25 02:14:00
Ya kind off
bollu 2017-02-25 02:14:06
OK, tell me what the difference is
Aku 2017-02-25 02:14:13
f takes a function and returns c
Aku 2017-02-25 02:14:22
g takes a and returns a function
Aku 2017-02-25 02:14:38
Is it?
bollu 2017-02-25 02:15:00
Aku: what does the function that g returns do?
Aku 2017-02-25 02:15:01
But when we write f:a->b->c
bollu 2017-02-25 02:15:06
yes
Aku 2017-02-25 02:15:10
haskell interprtes as g
bollu 2017-02-25 02:15:13
(also, notation f :: a -> b -> c)
bollu 2017-02-25 02:15:31
: is used in type theory, but haskell uses :: | :)
bollu 2017-02-25 02:15:39
OK, cool
Aku 2017-02-25 02:15:47
ohhkay
bollu 2017-02-25 02:15:48
so, let us expand the type of Parser a -> Parser a -> Parser a
bollu 2017-02-25 02:15:49
this is:
bollu 2017-02-25 02:16:03
f :: ([Token] -> [(a, [Token]) -> ([Token] -> [(a, [Token]) -> ([Token] -> [(a, [Token])
bollu 2017-02-25 02:16:07
whoops
Aku 2017-02-25 02:16:12
ya
Aku 2017-02-25 02:16:14
correct
bollu 2017-02-25 02:16:30
f :: ([Token] -> [(a, [Token])]) -> ([Token] -> [(a, [Token])] -> [Token] -> [(a, [Token])] *
bollu 2017-02-25 02:16:38
fuck that is hard to read without syntax highlighting
bollu 2017-02-25 02:16:40
but OK
bollu 2017-02-25 02:16:43
argh
bollu 2017-02-25 02:16:44
still wrong
bollu 2017-02-25 02:16:47
give me a second
Aku 2017-02-25 02:17:02
Ya u missed one parathesis
bollu 2017-02-25 02:17:16
f :: ( [Token] -> [(a, [Token])] ) ->
bollu 2017-02-25 02:17:16
( [Token] -> [(a, [Token])] ) ->
bollu 2017-02-25 02:17:16
([Token] -> [(a, [Token])] )
bollu 2017-02-25 02:17:18
yes?
Aku 2017-02-25 02:17:30
yess
bollu 2017-02-25 02:17:35
however
bollu 2017-02-25 02:17:52
now, let me "re-compact" some of the terms
Aku 2017-02-25 02:17:59
okay
bollu 2017-02-25 02:18:11
f :: Parser a -> Parser a -> ( [Token] -> [(a, [Token])] ) right?
Aku 2017-02-25 02:18:20
ya
Aku 2017-02-25 02:18:23
correct
bollu 2017-02-25 02:18:40
this is the "correct associativity" so I can write this as | f :: Parser a -> Parser a -> [Token] -> [(a, [Token])] ?
bollu 2017-02-25 02:18:58
(I would encourage you to convince yourself that f :: a -> b -> (c -> d) is the same as f' :: a -> b -> c -> d
Aku 2017-02-25 02:19:04
Maybe Yes
bollu 2017-02-25 02:19:11
OK, think of it like this
bollu 2017-02-25 02:19:16
a -> (b -> c) == a -> b -> c ?
Aku 2017-02-25 02:19:23
ya
Aku 2017-02-25 02:19:26
perfect
bollu 2017-02-25 02:19:28
so here
Aku 2017-02-25 02:19:29
Then?
bollu 2017-02-25 02:19:31
do you see it?
bollu 2017-02-25 02:19:38
why the types I wrote work out?
bollu 2017-02-25 02:19:44
if not, I shall walk you through it
Aku 2017-02-25 02:20:01
I got it but still lets walk !
bollu 2017-02-25 02:20:03
OK
bollu 2017-02-25 02:20:04
so
bollu 2017-02-25 02:20:05
we had
bollu 2017-02-25 02:20:16
f :: Parser a -> Parser a -> ( [Token] -> [(a, [Token])] )
Aku 2017-02-25 02:20:29
Just one question...
bollu 2017-02-25 02:20:32
yes?
Aku 2017-02-25 02:21:02
the toks in my function pAlt is the second last [Token]
bollu 2017-02-25 02:21:07
yep
Aku 2017-02-25 02:21:08
Is it?
bollu 2017-02-25 02:21:09
exactly
bollu 2017-02-25 02:21:24
beacuse f :: Parser a -> Parser a -> ( [Token] -> [(a, [Token])] ) ~= f' :: Parser a -> Parser a -> [Token] -> [(a, [Token])]
bollu 2017-02-25 02:21:31
we can "pretend" to take three parameters
bollu 2017-02-25 02:21:38
because haskell automatically curries its parameters
Aku 2017-02-25 02:21:48
I was convinced with this but then later it seems troublesome as I move to lefy
Aku 2017-02-25 02:21:51
*left
bollu 2017-02-25 02:22:19
well, it is "obvious" in some sense, because a -> b -> c is *defined* to be a -> (b -> c)
bollu 2017-02-25 02:22:33
so, a -> b -> c -> d ~= a -> (b -> (c -> d)) ~= a -> b -> (c -> d)
bollu 2017-02-25 02:22:41
right?
Aku 2017-02-25 02:22:47
yup
bollu 2017-02-25 02:22:49
OK
bollu 2017-02-25 02:22:52
so, now, similary
bollu 2017-02-25 02:22:58
similarly*
bollu 2017-02-25 02:23:05
pOneOrMore :: Parser a -> Parser [a]
bollu 2017-02-25 02:23:06
which is
bollu 2017-02-25 02:23:17
pOneOrMore :: Parser a -> [Token] -> [(a, [Token])]
bollu 2017-02-25 02:23:19
right?
Aku 2017-02-25 02:23:26
yes
bollu 2017-02-25 02:23:27
so we can take the parser and the toks as input
bollu 2017-02-25 02:23:30
which is why it works
bollu 2017-02-25 02:23:33
so now
bollu 2017-02-25 02:23:37
we have the first two parameters
Aku 2017-02-25 02:23:40
yessss
bollu 2017-02-25 02:23:48
we need to return [([a], [Token])]*
Aku 2017-02-25 02:23:52
curious.........
bollu 2017-02-25 02:23:56
I am sorry, I messed up the type before
bollu 2017-02-25 02:24:06
It should have been [([a], [Token])]* ([a], not a)
bollu 2017-02-25 02:24:10
right?
Aku 2017-02-25 02:24:23
ya ya
bollu 2017-02-25 02:25:27
now
bollu 2017-02-25 02:25:35
what does it mean to parse one or more times?
bollu 2017-02-25 02:25:38
it mean you parse once
bollu 2017-02-25 02:25:50
or you parse once, and then accept data zero or more times
bollu 2017-02-25 02:25:51
right?
bollu 2017-02-25 02:26:15
"once or more" = once or (once then (zero or more)) ?
bollu 2017-02-25 02:26:20
does the logic make sense?
Aku 2017-02-25 02:26:31
let me think..
bollu 2017-02-25 02:26:34
sure
Aku 2017-02-25 02:28:01
it is once or (once then (zero or more))
Aku 2017-02-25 02:28:16
Maybe
bollu 2017-02-25 02:28:35
OK
bollu 2017-02-25 02:29:34
why is it maybe?
Aku 2017-02-25 02:30:17
Otherwise pThen is doing the same thing but the second time it parses it uses a different parser
bollu 2017-02-25 02:30:17
yes
bollu 2017-02-25 02:30:17
we will use pThen to do this
bollu 2017-02-25 02:30:17
but, conceptually
Aku 2017-02-25 02:30:17
ya?
bollu 2017-02-25 02:30:17
does it make sense?
Aku 2017-02-25 02:30:17
yes it makes
bollu 2017-02-25 02:30:17
all I'm saying is
Aku 2017-02-25 02:30:17
yess!
Aku 2017-02-25 02:30:48
bollu?
bollu 2017-02-25 02:30:52
yes
bollu 2017-02-25 02:30:55
give me a minute
bollu 2017-02-25 02:31:17
yeah OK
bollu 2017-02-25 02:31:19
so
bollu 2017-02-25 02:31:29
conceptually, makes sense?
bollu 2017-02-25 02:31:49
Aku: ?
bollu 2017-02-25 02:31:50
if it does
Aku 2017-02-25 02:31:55
Once again!
bollu 2017-02-25 02:31:59
we have the ability to convert what I wrote into code
Aku 2017-02-25 02:32:00
plz tell
bollu 2017-02-25 02:32:04
tell what?
bollu 2017-02-25 02:32:12
"once or more" = once or (once then (zero or more)) ?
bollu 2017-02-25 02:32:16
what does once or more mean?
Aku 2017-02-25 02:32:17
yaa ya
bollu 2017-02-25 02:32:20
I can accept the input once
bollu 2017-02-25 02:32:23
or twice
bollu 2017-02-25 02:32:24
or thrice
bollu 2017-02-25 02:32:29
or four times
Aku 2017-02-25 02:32:50
But its not the same input everytime?
Aku 2017-02-25 02:32:57
Right?
bollu 2017-02-25 02:33:46
for then it won't be the sae
bollu 2017-02-25 02:33:48
same*
bollu 2017-02-25 02:33:49
actually
bollu 2017-02-25 02:33:53
let me restate that
Aku 2017-02-25 02:33:57
ya
bollu 2017-02-25 02:34:05
one or more == once then (zero or more)?
bollu 2017-02-25 02:34:09
be back in 10
bollu 2017-02-25 02:34:11
think about this for a while
bollu 2017-02-25 02:34:14
OK?
bollu 2017-02-25 02:34:18
and if you get it
Aku 2017-02-25 02:34:25
ya
bollu 2017-02-25 02:34:27
replace the "then" with `pThen`
bollu 2017-02-25 02:34:34
and zero or more is the same pZeroOrMore
Aku 2017-02-25 02:34:42
okay
bollu 2017-02-25 02:34:48
but we need to see how to correctly use pThen
bollu 2017-02-25 02:34:51
think about how
bollu 2017-02-25 02:34:54
I'll be back in 10
Aku 2017-02-25 02:35:01
fine
bollu 2017-02-25 02:35:04
(notice that we are now definition zeroOrMore and oneOrMore in terms of each other)
Aku 2017-02-25 02:35:31
yes that's confusing