Managing invariants in Clojure / Haskell

I have been comparing OOP and FP methodologies and I could not put my head around one thing in functional programming - keeping invariants in the data structure. For example imagine the following requirements. We have a list of projects and every project a has list of tasks and a list of assigned members. Every task can have a worker assigned to it but only from the list of project assigned

在Clojure / Haskell中管理不变量

我一直在比较面向对象和面向对象的方法,而且我不能在功能编程中把我的头放在一件事上 - 在数据结构中保持不变。 例如,想象下面的要求。 我们有一个项目清单,每个项目都有一个任务清单和一个分配成员清单。 每个任务都可以分配一个工作人员,但只能从项目分配成员列表中分配。 我可以想象,如何通过添加所需的检查和异常来在OOP语言中解决此问题(例如在Java中),这将导致我认为更强大的代码。 但是,由于数据与FP中

Best Practice on design and usage of data type in Haskell

My question is related to a more general question on Haskell program design. But I would like to focus on a specific use case. I defined a data type (eg Foo ), and used it in a function (eg f ) through pattern matching. Later, I realized that the type ( Foo ) requires some additional field to support new functionalities. However, adding the field would change how the type can be used; ie th

在Haskell中设计和使用数据类型的最佳实践

我的问题涉及到关于Haskell程序设计的更一般的问题。 但我想关注一个特定的用例。 我定义了一个数据类型(例如Foo ),并通过模式匹配将它用于一个函数(例如f )中。 后来,我意识到类型( Foo )需要一些额外的字段来支持新的功能。 但是,添加该字段会改变该类型的使用方式; 即取决于类型的现有功能可能受到影响。 为现有代码添加新功能,但无法吸引,难以避免。 我想知道Haskell语言级别的最佳实践是什么,以最大限

Can not deduce superclass

In the following code, GHC can not find the Functor instance in the definition of Monoidal instance. Why isn't GHC deducing that given the Applicative constraint is satisfied, then the Functor has to be somewhere already? (is there a name to this reasoning 'capability' ?) import Prelude hiding (Applicative (..), Monad (..)) class Functor f => Applicative f where pure :: a -&

无法推断超类

在下面的代码,GHC找不到定义的函子实例Monoidal实例。 为什么GHC没有推断出,如果Applicative约束得到满足,那么Functor必须已经在某个地方? (这个推理能力是否有名字?) import Prelude hiding (Applicative (..), Monad (..)) class Functor f => Applicative f where pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b class Functor f => Monoidal f where unit::f () (*) ::

How to set the module name when extracting Coq to Haskell

When I extract/compile Coq to Haskell using Extraction Language Haskell. in the Coq file and running coqtop -compile mymodule.v > MyModule.hs , I get a Haskell module which starts with module Main where . Is there an option to set the resulting Haskell module name? I currently pipe to sed like this - coqtop -compile mymodule.v | sed s/Main/MyModule/ > MyModule.hs but I'm looking

将Coq提取到Haskell时如何设置模块名称

当我使用Extraction Language Haskell.提取/编译Coq到Extraction Language Haskell. 在Coq文件中运行coqtop -compile mymodule.v > MyModule.hs ,我得到一个Haskell模块,它以module Main where开头。 是否有选项可以设置生成的Haskell模块名称? 我现在管这样sed - coqtop -compile mymodule.v | sed s/Main/MyModule/ > MyModule.hs 但我正在寻找更清洁的解决方案。 您可以使用Extraction "file"

Complexity of two cumulative sum (cumsum) functions in Haskell

Consider the following two cumulative sum (cumsum) functions: cumsum :: Num a => [a] -> [a] cumsum [] = [] cumsum [x] = [x] cumsum (x:y:ys) = x : (cumsum $ (x+y) : ys) and cumsum' :: Num a => [a] -> [a] cumsum' x = [sum $ take k x | k <- [1..length x]] Of course, I prefer the definition of cumsum to that of cumsum' and I understand that the former has linear complexity. B

Haskell中两个累积和(cumsum)函数的复杂性

考虑以下两个累积和(cumsum)函数: cumsum :: Num a => [a] -> [a] cumsum [] = [] cumsum [x] = [x] cumsum (x:y:ys) = x : (cumsum $ (x+y) : ys) 和 cumsum' :: Num a => [a] -> [a] cumsum' x = [sum $ take k x | k <- [1..length x]] 当然,我更喜欢cumsum'和cumsum的定义,我知道前者具有线性复杂性。 但是为什么cumsum'也具有线性复杂性? take本身具有线性在它的参数和的长度复杂k运行

Fibonacci sequence generation

I was writing a fibonacci sequence generator, and I was trying to understand the following code in Haskell fibs = 1 : 1 : zipWith (+) fibs (tail fibs) I understand what zipWith is, but I do not exactly know how the program executes and why it does generate all the fibonacci numbers. I was trying to understand why it does not terminate using the environment concept in functional languages as

斐波纳契序列的生成

我正在编写一个斐波那契序列生成器,并试图理解Haskell中的以下代码 fibs = 1 : 1 : zipWith (+) fibs (tail fibs) 我明白zipWith是什么,但我不完全知道该程序是如何执行的,以及它为什么产生所有斐波纳契数字。 我试图理解为什么它不会终止在函数式语言中使用环境概念,如下所示: 最初,由于Haskell的惰性评估, env的绑定应该是fibs : [1,1,x] ,然后为了评估fibs ,解释器在这种情况下评估x是zipWith (+) fibs (tail

Generating Fibonacci numbers in Haskell?

In Haskell, how can I generate Fibonacci numbers based on the property that the nth Fibonacci number is equal to the (n-2)th Fibonacci number plus the (n-1)th Fibonacci number? I've seen this: fibs :: [Integer] fibs = 1 : 1 : zipWith (+) fibs (tail fibs) I don't really understand that, or how it produces an infinite list instead of one containing 3 elements. How would I write haskel

在Haskell中生成斐波那契数列?

在Haskell中,如何根据第n个斐波纳契数等于第(n-2)个斐波那契数加上第(n-1)个斐波那契数的性质生成斐波纳契数? 我见过这个: fibs :: [Integer] fibs = 1 : 1 : zipWith (+) fibs (tail fibs) 我真的不明白,或者它如何产生一个无限列表,而不是一个包含3个元素的列表。 我如何编写可以通过计算实际定义来工作的haskell代码,而不是通过对列表函数执行一些非常奇怪的操作? 这是一个简单的函数,可以计算第n个斐波

Equational reasoning with tying the knot

I'm trying to wrap my head around Cont and callCC, by reducing this function: s0 = (flip runContT) return $ do (k, n) <- callCC $ k -> let f x = k (f, x) in return (f, 0) lift $ print n if n < 3 then k (n+1) >> return () else return () I've managed to reach this point: s21 = runContT (let f x = ContT $ _ -> cc

与结合的方程式推理

我试图通过减少这个函数来绕过Cont和callCC: s0 = (flip runContT) return $ do (k, n) <- callCC $ k -> let f x = k (f, x) in return (f, 0) lift $ print n if n < 3 then k (n+1) >> return () else return () 我设法达到了这一点: s21 = runContT (let f x = ContT $ _ -> cc (f, x) in ContT ($(f,0))) cc where cc = ((k,n) ->

Hide a constructor but not the type on import

I've got an internal module I'd like to provide an external API for module Positive.Internal where newtype Positive a = Positive { getPositive :: a } deriving (Eq, Ord) -- smart constructor toPositive :: (Num a, Ord a) => a -> Maybe (Positive a) toPositive a | a <= 0 = Nothing | otherwise = Just $ Positive a -- ... I want to hide the dumb constructor, and re

隐藏一个构造函数,但不是导入时的类型

我有一个内部模块,我想为其提供一个外部API module Positive.Internal where newtype Positive a = Positive { getPositive :: a } deriving (Eq, Ord) -- smart constructor toPositive :: (Num a, Ord a) => a -> Maybe (Positive a) toPositive a | a <= 0 = Nothing | otherwise = Just $ Positive a -- ... 我想隐藏愚蠢的构造函数,并将其替换为单向模式,以便用户仍可以对匹配值进行模式

How are free objects constructed?

So I understand that a free object is defined as being the left-hand side of an adjunction. But how does that lead you to the Haskell definition of such objects? More concretely: given a "forgetful functor" from the category of monads to the category of endofunctors, newtype Forget m a = Forget (m a) instance Monad m => Functor (Forget m) where fmap f (Forget x) = Forget (lif

自由物体是如何构建的?

所以我明白一个免费的对象被定义为一个副本的左侧。 但是,这是如何引导您了解这些对象的Haskell定义? 更具体地说:给予一个单子类型的“健忘的函数”到内生函数的范畴, newtype Forget m a = Forget (m a) instance Monad m => Functor (Forget m) where fmap f (Forget x) = Forget (liftM f x) 那么免费的monad Free :: (* -> *) -> (* -> *)是一种类型的入门( Monad实例和)以下同构: type f ~> g