我最近发现了FP错误(试图学习Haskell),到目前为止,我对我所看到的(一流函数、惰性求值和所有其他好东西)印象深刻。我还不是专家,但我已经开始发现对基本算法进行“功能性”推理比命令式推理更容易(而且我很难回到我必须回到的地方)。

The one area where current FP seems to fall flat, however, is GUI programming. The Haskell approach seems to be to just wrap imperative GUI toolkits (such as GTK+ or wxWidgets) and to use "do" blocks to simulate an imperative style. I haven't used F#, but my understanding is that it does something similar using OOP with .NET classes. Obviously, there's a good reason for this--current GUI programming is all about IO and side effects, so purely functional programming isn't possible with most current frameworks.

My question is, is it possible to have a functional approach to GUI programming? I'm having trouble imagining what this would look like in practice. Does anyone know of any frameworks, experimental or otherwise, that try this sort of thing (or even any frameworks that are designed from the ground up for a functional language)? Or is the solution to just use a hybrid approach, with OOP for the GUI parts and FP for the logic? (I'm just asking out of curiosity--I'd love to think that FP is "the future," but GUI programming seems like a pretty large hole to fill.)


当前回答

无论您使用的是f#或OCaml这样的函数/OO混合语言,还是Haskell这样的纯函数式语言(其中副作用被归为IO单子),大多数情况下,管理GUI所需的大量工作更像是“副作用”,而不是纯函数式算法。

也就是说,已经有一些关于功能性gui的可靠研究。甚至还有一些(主要)功能性工具包,如Fudgets或FranTk。

其他回答

所有这些其他答案都建立在函数式编程的基础上,但它们自己做出了很多设计决策。一个基本上完全由函数和简单抽象数据类型构建的库是gloss。下面是它的play函数的源类型

-- | Play a game in a window. Like `simulate`, but you manage your own input events.
play    :: Display              -- ^ Display mode.
        -> Color                -- ^ Background color.
        -> Int                  -- ^ Number of simulation steps to take for each second of real time.
        -> world                -- ^ The initial world.
        -> (world -> Picture)   -- ^ A function to convert the world a picture.
        -> (Event -> world -> world)    
                -- ^ A function to handle input events.
        -> (Float -> world -> world)
                -- ^ A function to step the world one iteration.
                --   It is passed the period of time (in seconds) needing to be advanced.
        -> IO ()

正如您所看到的,它完全通过提供具有简单抽象类型的纯函数来工作,其他库可以帮助您。

截至2016年,Haskell还有几个相对成熟的FRP框架,如Sodium和Reflex(还有Netwire)。

Manning关于函数式响应式编程的书展示了Java版本的Sodium,作为工作示例,并举例说明了FRP GUI代码库与命令式和基于Actor的方法相比是如何行为和扩展的。

最近还有一篇关于箭头化FRP的论文,以及将副作用、IO和突变纳入遵守法律的纯FRP环境的前景:http://haskell.cs.yale.edu/wp-content/uploads/2015/10/dwc-yale-formatted-dissertation.pdf。

同样值得注意的是,JavaScript框架(如ReactJS和Angular)以及其他许多框架已经或正在使用FRP或其他函数式方法来实现可伸缩和可组合的GUI组件。

无论您使用的是f#或OCaml这样的函数/OO混合语言,还是Haskell这样的纯函数式语言(其中副作用被归为IO单子),大多数情况下,管理GUI所需的大量工作更像是“副作用”,而不是纯函数式算法。

也就是说,已经有一些关于功能性gui的可靠研究。甚至还有一些(主要)功能性工具包,如Fudgets或FranTk。

Haskell方法似乎只是包装命令式GUI工具包(如GTK+或wxWidgets),并使用“do”块来模拟命令式样式

这并不是真正的“Haskell方法”——这只是通过命令式接口最直接地绑定到命令式GUI工具包的方法。Haskell恰好有相当突出的绑定。

有几种比较成熟的,或者更具实验性的纯函数式/声明式gui方法,主要是在Haskell中,主要使用函数式响应式编程。

一些例子是:

reflex-platform, https://github.com/reflex-frp/reflex-platform 葡萄柚,http://hackage.haskell.org/package/grapefruit-ui-gtk 无功,http://hackage.haskell.org/package/reactive-glut wxFruit, http://hackage.haskell.org/package/wxFruit reactive-banana, http://hackage.haskell.org/package/reactive-banana

对于那些不熟悉Haskell、Flapjax、http://www.flapjax-lang.org/的人来说,http://www.flapjax-lang.org/是一个基于JavaScript的函数式响应式编程的实现。

函数式响应式编程背后的一个开放思想是让事件处理函数同时产生对事件的反应和下一个事件处理函数。因此,一个进化的系统被表示为事件处理函数的序列。

对我来说,学习Yampa成为正确理解函数生成函数的关键。有一些关于扬帕的不错的论文。我推荐The Yampa Arcade:

http://www.cs.nott.ac.uk/~nhn/Talks/HW2003-YampaArcade.pdf(幻灯片,PDF) http://www.cs.nott.ac.uk/~nhn/Publications/hw2003.pdf(全文,PDF)

在Haskell.org上有一个关于Yampa的维基页面

http://www.haskell.org/haskellwiki/Yampa

原Yampa首页:

http://www.haskell.org/yampa(不幸的是目前坏了)