Skip to content

Instantly share code, notes, and snippets.

@roboguy13
Last active February 5, 2025 03:00
Show Gist options
  • Save roboguy13/e386301a24970ec84a9165228dbe4ea8 to your computer and use it in GitHub Desktop.
Save roboguy13/e386301a24970ec84a9165228dbe4ea8 to your computer and use it in GitHub Desktop.
module NumericDSL
where
---- Example GHCi session:
-- ghci> example (10 :: Int)
-- 130
-- ghci> example (10 :: Expr)
-- Mul (Add (Lit 10) (Lit 3)) (Lit 10)
example :: Num a => a -> a
example x = (x + 3) * x
data Expr
= Lit Int
| Add Expr Expr
| Mul Expr Expr
deriving (Show)
instance Num Expr where
fromInteger x = Lit (fromInteger x)
x + y = Add x y
x * y = Mul x y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment