我最近发现了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.)


当前回答

函数式编程可能从我上大学的时候就开始了,但我记得函数式编程系统的主要观点是阻止程序员产生任何“副作用”。然而,用户购买软件是由于其产生的副作用,例如更新用户界面。

其他回答

你可以看看Don Syme在f#上的系列,他演示了如何创建一个gui。下面的链接指向本系列的第三部分(您可以从那里链接到其他两部分)。

使用f#进行WPF开发将是一个非常有趣的GUI范例……

http://channel9.msdn.com/shows/Going+Deep/C9-Lectures-Dr-Don-Syme-Introduction-to-F-3-of-3/

The most apparent innovation noticed by people new to Haskell is that there is a separation between the impure world that is concerned with communicating with the outside world, and the pure world of computation and algorithms. A frequent beginner question is "How can I get rid of IO, i.e., convert IO a into a?" The way to to it is to use monads (or other abstractions) to write code that performs IO and chains effects. This code gathers data from the outside world, creates a model of it, does some computation, possibly by employing pure code, and outputs the result.

As far as the above model is concerned, I don't see anything terribly wrong with manipulating GUIs in the IO monad. The largest problem that arises from this style is that modules are not composable anymore, i.e., I lose most of my knowledge about the global execution order of statements in my program. To recover it, I have to apply similar reasoning as in concurrent, imperative GUI code. Meanwhile, for impure, non-GUI code the execution order is obvious because of the definition of the IO monad's >== operator (at least as long as there is only one thread). For pure code, it doesn't matter at all, except in corner cases to increase performance or to avoid evaluations resulting in ⊥.

控制台IO和图形化IO之间最大的哲学区别在于,实现前者的程序通常是用同步风格编写的。这是可能的,因为(撇开信号和其他打开的文件描述符不谈)只有一个事件源:通常称为stdin的字节流。gui本质上是异步的,必须对键盘事件和鼠标点击做出反应。

A popular philosophy of doing asynchronous IO in a functional way is called Functional Reactive Programming (FRP). It got a lot of traction recently in impure, non-functional languages thanks to libraries such as ReactiveX, and frameworks such as Elm. In a nutshell, it's like viewing GUI elements and other things (such as files, clocks, alarms, keyboard, mouse) as event sources, called "observables", that emit streams of events. These events are combined using familiar operators such as map, foldl, zip, filter, concat, join, etc., to produce new streams. This is useful because the program state itself can be seen as scanl . map reactToEvents $ zipN <eventStreams> of the program, where N is equal to the number of observables ever considered by the program.

Working with FRP observables makes it possible to recover composability because events in a stream are ordered in time. The reason is that the event stream abstraction makes it possible to view all observables as black boxes. Ultimately, combining event streams using operators gives back some local ordering on execution. This forces me to be much more honest about which invariants my program actually relies on, similar to the way that all functions in Haskell have to be referentially transparent: if I want to pull data from another part of my program, I have to be explicit ad declare an appropriate type for my functions. (The IO monad, being a Domain-Specific language for writing impure code, effectively circumvents this)

Elliot关于FRP的演讲可以在这里找到。

此外,这并不是真正的答案,而是一个评论和一些想法:“功能GUI”这个术语似乎有点矛盾(纯粹性和IO在同一个术语中)。

但我模糊的理解是,函数式GUI编程是关于声明性地定义一个与时间相关的函数,该函数接受(实际)与时间相关的用户输入,并产生与时间相关的GUI输出。

换句话说,这个函数像微分方程一样声明式地定义,而不是由算法命令式地使用可变状态定义。

因此,在传统FP中,我们使用时间无关函数,而在FRP中,我们使用时间相关函数作为描述程序的构建块。

让我们考虑在弹簧上模拟一个球,用户可以与之交互。球的位置是图形输出(在屏幕上),用户推球是按键(输入)。

在FRP中描述这个仿真程序(根据我的理解)是用一个微分方程(声明性地)来完成的:加速度*质量= -弹簧拉伸*弹簧常数+用户施加的力。

这里有一个关于ELM的视频,说明了这个观点。

实际上,我想说函数式编程(f#)对于用户界面编程来说是比c#更好的工具。你只需要稍微换个角度思考问题。

我在我的函数式编程书的第16章中讨论了这个主题,但是有一个免费的节选,它展示了(恕我直言)你可以在f#中使用的最有趣的模式。假设你想要实现矩形的绘制(用户按下按钮,移动鼠标并释放按钮)。在f#中,你可以这样写:

let rec drawingLoop(clr, from) = async { 
   // Wait for the first MouseMove occurrence 
   let! move = Async.AwaitObservable(form.MouseMove) 
   if (move.Button &&& MouseButtons.Left) = MouseButtons.Left then 
      // Refresh the window & continue looping 
      drawRectangle(clr, from, (move.X, move.Y)) 
      return! drawingLoop(clr, from) 
   else
      // Return the end position of rectangle 
      return (move.X, move.Y) } 

let waitingLoop() = async { 
   while true do
      // Wait until the user starts drawing next rectangle
      let! down = Async.AwaitObservable(form.MouseDown) 
      let downPos = (down.X, down.Y) 
      if (down.Button &&& MouseButtons.Left) = MouseButtons.Left then 
         // Wait for the end point of the rectangle
         let! upPos = drawingLoop(Color.IndianRed, downPos) 
         do printfn "Drawn rectangle (%A, %A)" downPos upPos }

这是一种非常必要的方法(在通常实用的f#风格中),但它避免使用可变状态来存储绘图的当前状态和存储初始位置。它可以做得更有功能,我写了一个库,作为我硕士论文的一部分,应该在几天后可以在我的博客上看到。

函数式响应式编程是一种更函数化的方法,但我发现它有点难以使用,因为它依赖于相当高级的Haskell特性(比如箭头)。然而,在大量的情况下,它是非常优雅的。它的局限性在于您不能轻松地对状态机进行编码(这是响应式程序的有用心理模型)。使用上面的f#技术,这是非常容易的。

函数式编程可能从我上大学的时候就开始了,但我记得函数式编程系统的主要观点是阻止程序员产生任何“副作用”。然而,用户购买软件是由于其产生的副作用,例如更新用户界面。