Created
March 13, 2025 23:24
-
-
Save ryukzak/8da8998a0e7b93f56ee3d234a31914a8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| data Code = Add | Div | |
| class Compiler a where | |
| compile :: Code -> (String, String) -> String | |
| instance Compiler Float where | |
| compile Add (a, b) = show (Unsafe.read a + Unsafe.read b :: Float) | |
| compile Div (a, b) = show (Unsafe.read a / Unsafe.read b :: Float) | |
| instance Compiler Int where | |
| compile Add (a, b) = show $ uncurry (+) $ parseArgs @Int (a, b) | |
| compile Div (a, b) = show (Unsafe.read a `div` Unsafe.read b :: Int) | |
| parseArgs :: forall x. (Read x) => (String, String) -> (x, x) | |
| parseArgs (a, b) = (Unsafe.read a, Unsafe.read b) | |
| x :: String -> String | |
| x tname = case tname of | |
| "float" -> compile @Float Add ("1", "2") | |
| "int" -> compile @Int Add ("1", "2") | |
| _ -> "Unsupported type" | |
| main :: IO () | |
| main = putStrLn $ x "float" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment