使 ReadArgs 1.0 使用单个参数

发布于 2024-12-24 21:51:04 字数 522 浏览 1 评论 0原文

使用 ReadArgs 包,它似乎不支持单参数情况。

{-# LANGUAGE ScopedTypeVariables #-}

import ReadArgs (readArgs)

main = do
  (foo :: Int) <- readArgs
  print foo

错误是(当使用版本 1.0 时):

No instance for (ReadArgs.ArgumentTuple Int)
  arising from a use of `readArgs'

我的问题有两个:

  1. readArgs 是如何工作的?
  2. 如何调整该库以使其也可以使用单个参数?

NB ReadArgs 1.1 版本消除了这个“错误”;见评论。

Playing around with the ReadArgs package, it seems that it does not support single-argument situations.

{-# LANGUAGE ScopedTypeVariables #-}

import ReadArgs (readArgs)

main = do
  (foo :: Int) <- readArgs
  print foo

The error is (when using version 1.0):

No instance for (ReadArgs.ArgumentTuple Int)
  arising from a use of `readArgs'

My question is twofold:

  1. How does readArgs work?
  2. How can that library be adjusted to allow it to work with a single argument as well?

N.B. version 1.1 of ReadArgs eliminates this "error"; see comments.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笙痞 2024-12-31 21:51:04

据我所知,该包使用元组来模拟类型安全的异构列表。正如您所注意到的,当您只需要一个参数时,这会导致问题,因为 Haskell 中没有一元组。

但是,该包还为异构列表提供了适当的类型,可以使用它来代替元组: :& 类型。您可以使用类似于 : 运算符的方式使用它,并使用空元组 () 作为终止符:

(foo :: Int) :& (bar :: String) :& () <- readArgs

这对于一个参数来说很简单:

(foo :: Int) :& () <- readArgs

From what I can tell, the package uses tuples to emulate type-safe heterogeneous lists. As you noticed, this causes problems when you want only one argument, as there are no one-tuples in Haskell.

However, the package also provides a proper type for heterogeneous lists, which can be used instead of tuples: the :& type. You use it similar to the : operator, with the empty tuple () as a terminator:

(foo :: Int) :& (bar :: String) :& () <- readArgs

This works trivially with one argument:

(foo :: Int) :& () <- readArgs
记忆で 2024-12-31 21:51:04

另一个答案,只是为了好玩:具体化 Daniel Wagner 为 OneTuple 添加实例的建议:

{-# LANGUAGE ScopedTypeVariables #-}

import ReadArgs
import Data.Tuple.OneTuple

instance (Argument a) => ArgumentTuple (OneTuple a) where
  parseArgsFrom ss = do
    a :& () <- parseArgsFrom ss
    return $ OneTuple a
  usageFor (OneTuple a) = usageFor (a :& ())

main = do
    OneTuple (foo :: Int) <- readArgs
    print foo

大部分是从 Adam Wagner 的解决方案中窃取的。令人惊讶的是,所有额外的语言编译指示都可以被删除。

这可能听起来很愚蠢,但由于某种原因,OneTuple (foo :: Int) <- readArgs 并不像我想象的那么难看。

Another answer, just for fun: reifying Daniel Wagner's suggestion of adding an instance for OneTuple:

{-# LANGUAGE ScopedTypeVariables #-}

import ReadArgs
import Data.Tuple.OneTuple

instance (Argument a) => ArgumentTuple (OneTuple a) where
  parseArgsFrom ss = do
    a :& () <- parseArgsFrom ss
    return $ OneTuple a
  usageFor (OneTuple a) = usageFor (a :& ())

main = do
    OneTuple (foo :: Int) <- readArgs
    print foo

Mostly stolen from Adam Wagner's solution. Amazingly, all the extra language pragmas can be removed.

This may sound silly, but the OneTuple (foo :: Int) <- readArgs isn't nearly as ugly as I imagined it would be for some reason.

寄居者 2024-12-31 21:51:04

我不太明白我需要启用的所有扩展,但是您可以定义一个 ReadArgs.ArgumentTuple a 的实例(即使它实际上不是一个语义上正确的名称),如下所示:

{-# LANGUAGE FlexibleInstances, UndecidableInstances,
             OverlappingInstances, ScopedTypeVariables #-}

import ReadArgs

instance (Argument a) => ArgumentTuple a where
  parseArgsFrom ss = do
    a :& () <- parseArgsFrom ss
    return a
  usageFor a = usageFor (a :& ())

main = do
    (foo :: Int) <- readArgs
    print foo

另外,我我不太确定这个实例是否有任何问题,尽管它适用于您提供的示例。我认为图书馆丢失它是有原因的,但我可能是错的。

更新

在尝试了一些事情之后,为了确保它们仍然有效(如下面的示例),我相当确信这不会导致任何问题,所以也许它(或类似的东西)的存在只是一个疏忽。

main = do
    (foo :: Int, bar :: Int) <- readArgs
    print foo
    print bar

I don't quite understand all of the extensions I needed to enable, but you could define an instance of ReadArgs.ArgumentTuple a (even though it's not really a semantically correct name) like this:

{-# LANGUAGE FlexibleInstances, UndecidableInstances,
             OverlappingInstances, ScopedTypeVariables #-}

import ReadArgs

instance (Argument a) => ArgumentTuple a where
  parseArgsFrom ss = do
    a :& () <- parseArgsFrom ss
    return a
  usageFor a = usageFor (a :& ())

main = do
    (foo :: Int) <- readArgs
    print foo

Also, I'm not really sure if there are any problems with this instance, though it works for the example you presented. I would assume there's a reason it's missing from the library, but I may be wrong.

Update

After looking trying a few things out, to be sure they still work (like the following example), I'm fairly convinced this doesn't cause any problems, so maybe it's (or something similar) existance was just an oversight.

main = do
    (foo :: Int, bar :: Int) <- readArgs
    print foo
    print bar
淡莣 2024-12-31 21:51:04

一种迟钝的解决方案是滥用“可选参数”功能:

(foo :: Int, _ :: Maybe ()) <- readArgs

即使您提供 () 作为第二个参数,它也会默默地工作:

$ runhaskell args.hs 3 ()
3

并稍微搞乱使用消息:

$ runhaskell args.hs 3 foo
usage: args.hs Int [()]

但它确实拒绝额外的参数不是 (),并且它确实按预期工作:

$ runhaskell args.hs 3
3

One retarded solution is to abuse the "optional argument" feature:

(foo :: Int, _ :: Maybe ()) <- readArgs

This silently works even if you supply () as the second arg:

$ runhaskell args.hs 3 ()
3

And screws up the usage message a little:

$ runhaskell args.hs 3 foo
usage: args.hs Int [()]

But it does reject extra arguments that are not (), and it does work as desired:

$ runhaskell args.hs 3
3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文