Skip to content
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

show how to use GADTs and type indices to dispatch commands #229

Merged
merged 1 commit into from
Jul 5, 2020

Conversation

byorgey
Copy link
Member

@byorgey byorgey commented Jul 4, 2020

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.

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.
@bookshelfdave
Copy link
Collaborator

this is really interesting, and it looks like I have some reading to do :-)

I'll merge into my wip branch and refactor all the rest. Thanks @byorgey!

@bookshelfdave bookshelfdave merged commit 73b849f into dp_help_refactor Jul 5, 2020
bookshelfdave pushed a commit that referenced this pull request Jul 6, 2020
GADT/typed indices implementation from byorgey via #229
bookshelfdave pushed a commit that referenced this pull request Jul 6, 2020
GADT/typed indices implementation from byorgey via #229
bookshelfdave pushed a commit that referenced this pull request Jul 6, 2020
GADT/typed indices implementation from byorgey via #229
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.

2 participants