10

別の質問へのこの回答では、ラッパー関数を使用してコマンドライン引数の構文チェックを行うためのコードを除外するHaskellコードスケッチが少し与えられました。これが私が単純化しようとしているコードの部分です:

takesSingleArg :: (String -> IO ()) -> [String] -> IO ()
takesSingleArg act [arg] = act arg
takesSingleArg _   _     = showUsageMessage

takesTwoArgs :: (String -> String -> IO ()) -> [String] -> IO ()
takesTwoArgs act [arg1, arg2] = act arg1 arg2
takesTwoArgs _   _            = showUsageMessage

引数の数ごとに余分な関数を書く必要を回避する方法はありますか(おそらくテンプレートHaskellを使用していますか?)?理想的には、次のようなものを記述できるようにしたいと思います(私はこの構文を作成しています)

generateArgumentWrapper<2, showUsageMessage>

そしてそれはに拡大します

\fn args -> case args of
                 [a, b] -> fn a b
                 _      -> showUsageMessage

generateArgumentWrapper理想的には、メタ関数に対して可変数の引数を持つこともできるので、次のことができます。

generateArgumentWrapper<2, asInt, asFilePath, showUsageMessage>

そしてそれはに拡大します

\fn args -> case args of
                 [a, b] -> fn (asInt a) (asFilePath b)
                 _      -> showUsageMessage

誰かがこれを達成する方法を知っていますか?コマンドライン引数([String])を任意の関数にバインドするのは本当に簡単な方法です。それとも、まったく異なる、より良いアプローチがありますか?

4

3 に答える 3

12

Haskell has polyvariadic functions. Imagine you had a type like

data Act = Run (String -> Act) | Res (IO ())

with some functions to do what you want

runAct (Run f) x = f x
runAct (Res _) x = error "wrong function type"

takeNargs' 0 (Res b) _ = b
takeNargs' 0 (Run _) _ = error "wrong function type"
takeNargs' n act (x:xs) = takeNargs' (n-1) (runAct act x) xs
takeNargs' _ _ [] = error "not long enough list"

now, all you you need is to marshal functions into this Act type. You need some extensions

{-# LANGUAGE FlexibleInstances, FlexibleContexts #-}

and then you can define

class Actable a where
  makeAct :: a -> Act
  numberOfArgs :: a -> Int

instance Actable (String -> IO ()) where
  makeAct f = Run $ Res . f
  numberOfArgs _ = 1

instance Actable (b -> c) => Actable (String -> (b -> c)) where
  makeAct f = Run $ makeAct . f
  numberOfArgs f = 1 + numberOfArgs (f "")

now you can define

takeNArgs n act = takeNargs' n (makeAct act) 

which makes it easier to define your original functions

takesSingleArg :: (String -> IO ()) -> [String] -> IO ()
takesSingleArg = takeNArgs 1

takesTwoArgs :: (String -> String -> IO ()) -> [String] -> IO ()
takesTwoArgs = takeNArgs 2

But we can do even better

takeTheRightNumArgs f = takeNArgs (numberOfArgs f) f

Amazingly, this works (GHCI)

*Main> takeTheRightNumArgs putStrLn ["hello","world"]
hello
*Main> takeTheRightNumArgs (\x y -> putStrLn x >> putStrLn y)  ["hello","world"] 
hello
world

Edit: The code above is much more complicated than it needs to be. Really, all you want is

class TakeArgs a where
   takeArgs :: a -> [String] -> IO ()

instance TakeArgs (IO ()) where
   takeArgs a _ = a

instance TakeArgs a => TakeArgs (String -> a) where
   takeArgs f (x:xs) = takeArgs (f x) xs
   takeArgs f [] = error "end of list"
于 2012-04-05T08:47:21.170 に答える
2

You might want to make use of existing libraries to deal with command line arguments. I believe the de-facto standard right now is cmdargs, but other options exist, such as ReadArgs and console-program.

于 2012-04-05T14:24:13.267 に答える
1

Combinators are your friend. Try this:

take1 :: [String] -> Maybe String
take1 [x] = Just x
take1 _ = Nothing

take2 :: [String] -> Maybe (String,String)
take2 [x,y] = Just (x,y)
take2 _ = Nothing

take3 :: [String] -> Maybe ((String,String),String)
take3 [x,y,z] = Just ((x,y),z)
take3 _ = Nothing

type ErrorMsg = String

with1 :: (String -> IO ()) -> ErrorMsg -> [String] -> IO ()
with1 f msg = maybe (fail msg) f . take1

with2 :: (String -> String -> IO ()) -> ErrorMsg -> [String] -> IO ()
with2 f msg = maybe (fail msg) (uncurry f) . take2

with3 :: (String -> String -> String -> IO ()) -> ErrorMsg -> [String] -> IO ()
with3 f msg = maybe (fail msg) (uncurry . uncurry $ f) . take3

foo a b c = putStrLn $ a ++ " :: " ++ b ++ " = " ++ c

bar = with3 foo "You must send foo a name, type, definition"

main = do
  bar [ "xs", "[Int]", "[1..3]" ]
  bar [ "xs", "[Int]", "[1..3]", "What am I doing here?" ]

And if you like overpowered language extensions:

{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, FlexibleContexts, UndecidableInstances #-}

foo a b c = putStrLn $ a ++ " :: " ++ b ++ " = " ++ c
foo_msg = "You must send foo a name, type, definition"

class ApplyArg a b | a -> b where
  appArg :: ErrorMsg -> a -> [String] -> IO b

instance ApplyArg (IO b) b where
  appArg _msg todo [] = todo
  appArg msg _todo _ = fail msg

instance ApplyArg v q => ApplyArg (String -> v) q where
  appArg msg todo (x:xs) = appArg msg (todo x) xs
  appArg msg _todo _ = fail msg

quux :: [String] -> IO ()
quux xs = appArg foo_msg foo xs

main = do
  quux [ "xs", "[int]", "[1..3]" ]
  quux [ "xs", "[int]", "[1..3]", "what am i doing here?" ]
于 2012-04-05T08:38:32.263 に答える