module Main where
-- Integer arithmetic with (+) and (/)
data Term =
| Plus Term Term
| Div Term Term
--
-- Basic evaluator
--
eval ( N n) = n
eval ( Plus t1 t2) = eval t1 + eval t2
eval
( Div t1 t2
) = eval t1 `
div ` eval t2
--
-- Evaluator that counts the number of operations
--
-- Returns (Ok n) in case of success
-- Returns (Error "Division by zero") otherwise
--
-- Type of computations having access to a counter, which produce an "a"
type Counter a
= ( Int -> ( Int , a
) )
returnC :: a -> Counter a
bindC :: Counter a -> ( a -> Counter b) -> Counter b
-- "Run" a Counter computation "c", with initial counter state "s0"
runC
:: Counter a
-> Int -> ( Int , a
)
-- Increment the counter
inc :: Counter ( )
evalWithCount
:: Term
-> ( Int , Int ) evalWithCount t = runC ( eval' t) 0
where
eval' :: Term
-> Counter
Int eval' = undefined
--
-- Evaluator that catches division by zero
--
data Result a = Error String | Ok a deriving Show
returnR :: a -> Result a
returnR x = undefined
bindR :: Result a -> (a -> Result b) -> Result b
bindR m f = undefined
-- Raise an exception
throw :: String -> Result a
throw s = undefined
evalChecked :: Term -> Result Int
evalChecked = undefined
main = print (undefined::String)
bW9kdWxlIE1haW4gd2hlcmUKCi0tIEludGVnZXIgYXJpdGhtZXRpYyB3aXRoICgrKSBhbmQgKC8pCmRhdGEgVGVybSA9IAogICAgICBOIEludAogICAgfCBQbHVzIFRlcm0gVGVybQogICAgfCBEaXYgVGVybSBUZXJtCgotLQotLSBCYXNpYyBldmFsdWF0b3IKLS0KCmV2YWwgOjogVGVybSAtPiBJbnQKZXZhbCAoTiBuKSA9IG4KZXZhbCAoUGx1cyB0MSB0MikgPSBldmFsIHQxICsgICAgIGV2YWwgdDIKZXZhbCAoRGl2IHQxIHQyKSAgPSBldmFsIHQxIGBkaXZgIGV2YWwgdDIKCi0tCi0tIEV2YWx1YXRvciB0aGF0IGNvdW50cyB0aGUgbnVtYmVyIG9mIG9wZXJhdGlvbnMKLS0KLS0gUmV0dXJucyAoT2sgbikgaW4gY2FzZSBvZiBzdWNjZXNzCi0t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