-
Notifications
You must be signed in to change notification settings - Fork 501
Parse all DefaultFun builtin functions. #4546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
effectfully
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. It would be nice to generalize the machinery, though.
| builtinFunction = lexeme $ choice $ map parseBuiltin [minBound .. maxBound] | ||
| where parseBuiltin builtin = try $ string (display builtin) >> pure builtin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that we know a builtin is expected here, we could parse a raw textual name and then look it up in a cached Map Text fun. I don't think we care about performance here though, so that definition should be perfectly fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does seem somewhat wasteful to print every builtin each time we try to parse one!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I just did it?
| , (MkNilPairData,"mkNilPairData") | ||
| ] | ||
|
|
||
| builtinFunction :: Parser DefaultFun |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generalize to an arbitrary fun (implementing Pretty, Bounded etc)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parser can only parse terms in the DefaultUni and is a DefaultFun...the old parser also did that. (Now a parsable term is Term Name DefaultUni DefaultFun SourcePos). I can still parse it more generically but it probably still won't work for other funs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we decided to leave the current parser monomorphic for now. I think we can leave making it generic over universes for a future chunk of work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did add a comment reminding people that only DefaultFun will work. Will the error be easier to read this way? Or maybe restricting the parser to DefaultFun will show better errors I think? If so I'll drop the last commit and make it parse to DefaultFun only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see you generalized it anyway. That seems fine!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can leave making it generic over universes for a future chunk of work.
I'm not suggesting to make it generic over universes. It's harder and we have only one universe anyway. However generalizing over sets of built-in functions is fairly trivial and we do have multiple of them. I don't really care, just proposing.
0a6a6f0 to
50adb0c
Compare
| mkCachedBuiltin :: [DefaultFun] -> Map T.Text DefaultFun -> Map T.Text DefaultFun | ||
| mkCachedBuiltin (hdFns:tlFns) builtinMap = | ||
| mkCachedBuiltin tlFns (insert (display hdFns) hdFns builtinMap) | ||
| mkCachedBuiltin [] builtinMap = builtinMap |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpicking (i.e. not important at all): instead of writing the recursion manually you define mkCachedBuiltin as Map.fromList . map (\fun -> (display fun, fun)) or something like that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah HLint told me to do that too but I thought it's harder to read. Maybe not. I can change that. I was gonna open a follow up PR for the error anyway.
| builtinFunction = lexeme $ do | ||
| txt <- takeWhileP (Just "identifier") isIdentifierChar | ||
| case lookup txt cachedBuiltin of | ||
| Nothing -> error $ "Not a builtin" <> show cachedBuiltin <> show txt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't call error here! There's nothing exceptional about a bulitin not being recognized. The Parser monad allows you to throw an error in it. I think the right function is customFailure, but not sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, yeah, the error to throw is UnknownBuiltinFunction from ParserError.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, thank you! I was rushing to merge the rest so that the blocked PR can go through. I want to show the cachedBuiltin somehow, I think it's useful to see. I'll work on that today.
As mentioned in #4368. It's similar to before the megaparsec merge, but now it parses to
DefaultFun.Pre-submit checklist: