Created
December 7, 2020 17:24
-
-
Save rntz/2d69bf51a3040803b8b4cfec81ac7a07 to your computer and use it in GitHub Desktop.
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
fib :: Int -> Int | |
fib 0 = 1 | |
fib 1 = 1 | |
fib n = fib (n-1) + fib (n-2) | |
data Type = IntType | StringType deriving (Eq, Show) | |
data Expr = IntExpr Int | |
| StringExpr String | |
| PlusExpr Expr Expr | |
| IntToString Expr | |
deriving (Show) | |
-- instance Show Expr where | |
-- show (IntExpr i) = show i | |
-- show (PlusExpr x y) = "(" ++ show x ++ " + " ++ show y ++ ")" | |
infer :: Expr -> Maybe Type | |
infer (IntExpr x) = Just IntType | |
infer (StringExpr _) = Just StringType | |
infer (PlusExpr a b) = do _ <- check IntType a | |
_ <- check IntType b | |
Just IntType | |
infer (IntToString e) = do _ <- check IntType e | |
Just StringType | |
infer (IntToString e) = check IntType e >>= \_ -> Just StringType | |
infer (IntToString e) = case check IntType e of | |
Nothing -> Nothing | |
Just _ -> Just StringType | |
check :: Type -> Expr -> Maybe () | |
check tp exp = | |
case infer exp of | |
Nothing -> Nothing | |
Just actual -> if tp == actual then Just () else Nothing | |
check tp exp = | |
do actual <- infer exp | |
if tp == actual then Just () else Nothing | |
check tp exp = (infer exp) >>= f | |
where f :: Type -> Maybe () | |
f = (\actual -> if tp == actual then Just () else Nothing) | |
-- (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b | |
-- (Nothing >>= f) = Nothing | |
-- ((Just a) >>= f) = f a | |
x, y :: Maybe Int | |
x = Just 2 | |
y = Nothing | |
-- eval :: Expr -> Int | |
-- eval (IntExpr i) = i | |
-- eval (PlusExpr x y) = eval x + eval y | |
main :: IO () | |
main = putStrLn "hello, world" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment