gpt4 book ai didi

haskell - 有人可以用*非常*简单的术语解释反射包 API 吗?

转载 作者:行者123 更新时间:2023-12-04 04:30:34 28 4
gpt4 key购买 nike

我很难理解描述反射包的文档/示例。我是一名命令式编程老手,但也是一名 Haskell 新手。你能引导我完成一个非常简单的介绍吗?
包裹:https://hackage.haskell.org/package/reflection
编辑:对于结束这个问题的人:这是对 Haskell 反射的初学者介绍。下面的答案非常好,其他答案也很有用,所以请重新打开。

最佳答案

在最简单的用例中,如果您有一些配置信息希望在一组函数中普遍可用:

data Config = Config { w :: Int, s :: String }
您可以添加 Given Config对需要访问配置的功能的约束:
timesW :: (Given Config) => Int -> Int
然后使用值 given引用当前配置(因此 w givens given 引用其字段):
timesW x = w given * x
还有一些其他功能,一些使用配置,一些不使用:
copies :: Int -> String -> String
copies n str = concat (replicate n str)

foo :: (Given Config) => Int -> String
foo n = copies (timesW n) (s given)
然后,您可以在不同的配置下运行计算 give :
main = do
print $ give (Config 5 "x") $ foo 3
print $ give (Config 2 "no") $ foo 4
这类似于:
  • 定义 given :: Config全局,除非您可以在同一程序中的多个配置下运行计算;
  • 将配置作为额外参数传递给每个函数,除非您避免显式接受配置并将其传递的麻烦,例如:
    timesW cfg x = w cfg * x
    foo cfg n = copies (timesW cfg n) (s cfg)
  • 使用 Reader monad,但您不必将所有内容都提升为笨拙的 monad 或应用程序级语法,例如:
    timesW x = (*) <$> asks w <*> pure x
    foo n = copies <$> timesW n <*> asks s

  • 完整的例子:
    {-# LANGUAGE FlexibleContexts #-}

    import Data.Reflection

    data Config = Config { w :: Int, s :: String }

    timesW :: (Given Config) => Int -> Int
    timesW x = w given * x

    copies :: Int -> String -> String
    copies n str = concat (replicate n str)

    foo :: (Given Config) => Int -> String
    foo n = copies (timesW n) (s given)

    main = do
    print $ give (Config 5 "x") $ foo 3
    print $ give (Config 2 "no") $ foo 4

    关于haskell - 有人可以用*非常*简单的术语解释反射包 API 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64870921/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com