Created
January 12, 2019 21:15
-
-
Save Kazark/9fbf58244721478b79f074e753ae2236 to your computer and use it in GitHub Desktop.
How to create exhaustive print function for "enum" in Idris, off a string map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%default total | |
data RPS = Rock | Paper | Scissors | |
Eq RPS where | |
Rock == Rock = True | |
Paper == Paper = True | |
Scissors == Scissors = True | |
_ == _ = False | |
StrMap : List (RPS, String) | |
StrMap = | |
[ (Rock, "rock") | |
, (Paper, "paper") | |
, (Scissors, "scissors") | |
] | |
print' : RPS -> Maybe String | |
print' rps = lookup rps StrMap | |
print_exhaustive : (rps : RPS) -> IsJust (print' rps) | |
print_exhaustive Rock = ItIsJust | |
print_exhaustive Paper = ItIsJust | |
print_exhaustive Scissors = ItIsJust | |
justGetIt : (m : Maybe a) -> IsJust m -> a | |
justGetIt (Just y) ItIsJust = y | |
print : RPS -> String | |
print rps with (isItJust (print' rps)) | |
| Yes itIs = justGetIt (print' rps) itIs | |
| No itsNot = absurd $ itsNot $ print_exhaustive rps |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment