Skip to content

refactor REPL commands to implement :help - #226

Closed
bookshelfdave wants to merge 6 commits into
masterfrom
dp_help_refactor
Closed

bookshelfdave wants to merge 6 commits into
masterfrom
dp_help_refactor

Conversation

@bookshelfdave

@bookshelfdave bookshelfdave commented Jul 2, 2020 •

Copy link
Copy Markdown
Collaborator

You mentioned in #215 that The tricky part of this is that different commands need different numbers and types of arguments, so actually including the actions to be run for each command would require some type trickery. What I have here might not be exactly what you're looking for, but it's my naive first attempt :-) I'm happy to rework the implementation, it was a fun learning experience.

A REPLCommand action is of type REPLExpr -> Disco IErr (), so you'll see wrapper functions that accept this type, unwrap it and pass it to the more specific function (handleAnn in this case, which is of type Term -> Disco IErr String). The sad part is the need to match all the other cases and return (). Maybe this isn't ideal, but it was easy enough to implement.

handleAnnWrapper :: REPLExpr -> Disco IErr ()
handleAnnWrapper (Ann t) = handleAnn t >>= iputStrLn
handleAnnWrapper _       = return ()

handleAnn :: Term -> Disco IErr String
handleAnn t = do

TODO:

  • add a test for :help
  • rebase on several commits from July 1st

metadave and others added 2 commits July 2, 2020 17:07
Restyle refactor REPL commands to implement :help
This way we don't need the toTag stuff, and we don't need any
handleXXXWrapper functions that match on just one thing and return ()
for everything else.

The key idea is that both commands and and REPLExprs are indexed by an
enumeration, so each REPLExpr and the command that can process it
share a label.  The 'dispatch' function uses the magic 'gcast'
function from Data.Typeable to find the first command which has a
matching type index, and uses its 'action' to process
the (guaranteed-matching) REPLExpr.  Each 'handleXXX' function
declares that it only handles REPLExprs with a particular type index,
which means it only has to match on the one constructor which actually
has that type index.
@byorgey

byorgey commented Jul 4, 2020

Copy link
Copy Markdown
Member

@metadave yes, this is exactly what I had in mind when I said this would be the tricky part. It's not very satisfying to have all the handleXXXWrapper functions which match on one constructor of REPLExpr and return () on everything else. I just opened a PR illustrating how to do it in a more satisfying (though more complicated!) way with type-indexed GADTs. Right now I only got the :ann command working as an illustration and everything else is still commented out, so it would still need a lot of cleanup and converting the other commands.

What do you think? Happy to explain more if you're unsure how it works.

show how to use GADTs and type indices to dispatch commands
@byorgey
byorgey deleted the dp_help_refactor branch May 27, 2021 16:34
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