Skip to content

Conversation

@thealmarty
Copy link
Contributor

@thealmarty thealmarty commented Apr 21, 2022

As mentioned in #4368. It's similar to before the megaparsec merge, but now it parses to DefaultFun.

Pre-submit checklist:

  • Branch
    • Tests are provided (if possible)
    • Commit sequence broadly makes sense
    • Key commits have useful messages
    • Relevant tickets are mentioned in commit messages
    • Formatting, materialized Nix files, PNG optimization, etc. are updated
  • PR
    • (For external contributions) Corresponding issue exists and is linked in the description
    • Self-reviewed the diff
    • Useful pull request description
    • Reviewer requested

Copy link
Contributor

@effectfully effectfully left a 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.

Comment on lines 20 to 21
builtinFunction = lexeme $ choice $ map parseBuiltin [minBound .. maxBound]
where parseBuiltin builtin = try $ string (display builtin) >> pure builtin
Copy link
Contributor

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.

Copy link
Contributor

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!

Copy link
Contributor Author

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
Copy link
Contributor

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)?

Copy link
Contributor Author

@thealmarty thealmarty Apr 21, 2022

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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!

Copy link
Contributor

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.

@thealmarty thealmarty force-pushed the thealmarty/autogen-builtins-parser branch from 0a6a6f0 to 50adb0c Compare April 21, 2022 15:59
@thealmarty thealmarty enabled auto-merge (squash) April 21, 2022 18:52
@kozross kozross mentioned this pull request Apr 21, 2022
@thealmarty thealmarty merged commit 892d9b0 into master Apr 21, 2022
@effectfully effectfully deleted the thealmarty/autogen-builtins-parser branch April 21, 2022 20:08
Comment on lines +22 to +25
mkCachedBuiltin :: [DefaultFun] -> Map T.Text DefaultFun -> Map T.Text DefaultFun
mkCachedBuiltin (hdFns:tlFns) builtinMap =
mkCachedBuiltin tlFns (insert (display hdFns) hdFns builtinMap)
mkCachedBuiltin [] builtinMap = builtinMap
Copy link
Contributor

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.

Copy link
Contributor Author

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
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants