Skip to content

Instantly share code, notes, and snippets.

@wz1000
Created June 10, 2017 20:40
Show Gist options
  • Save wz1000/e01a2387e4ea9072485224831f05413e to your computer and use it in GitHub Desktop.
Save wz1000/e01a2387e4ea9072485224831f05413e to your computer and use it in GitHub Desktop.
{-# 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