Created
June 10, 2017 20:40
-
-
Save wz1000/e01a2387e4ea9072485224831f05413e 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
{-# LANGUAGE TypeFamilies #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE OverlappingInstances #-} | |
{-# LANGUAGE ConstraintKinds #-} | |
type Value = String | |
type FromJSON = Read | |
fromJSON :: FromJSON a => Value -> a | |
fromJSON = read | |
type family Result a where | |
Result (a -> b) = Result b | |
Result b = b | |
class ParseArgs a where | |
parseArgs :: a -> [Value] -> Maybe (Result a) | |
instance (Result a ~ a) => ParseArgs a where | |
parseArgs x [] = Just x | |
parseArgs _ _ = Nothing | |
instance (FromJSON a, ParseArgs b) => ParseArgs (a -> b) where | |
parseArgs f [] = Nothing | |
parseArgs f (x:xs) = parseArgs (f (fromJSON x)) xs | |
add :: Int -> Int -> Int | |
add x y = x+y | |
five :: Maybe Int | |
five = parseArgs add ["2","3"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment