Skip to content

Instantly share code, notes, and snippets.

@ryukzak
Created March 13, 2025 23:24
Show Gist options
  • Select an option

  • Save ryukzak/8da8998a0e7b93f56ee3d234a31914a8 to your computer and use it in GitHub Desktop.

Select an option

Save ryukzak/8da8998a0e7b93f56ee3d234a31914a8 to your computer and use it in GitHub Desktop.
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