有功能编程的软件工程方法吗? [英] Is there a software-engineering methodology for functional programming?

查看:142
本文介绍了有功能编程的软件工程方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天教授的软件工程完全专注于面向对象编程和面向世界的自然面向对象的视图。有一个详细的方法,描述如何将域模型转换为具有几个步骤的类模型和大量(UML)工件,如用例图或类图。许多程序员已经内化了这种方法,并且对如何从头设计面向对象的应用程序有一个好主意。



新的炒作是功能性编程,在许多书和教程中教授。但是功能软件工程呢?
在阅读关于Lisp和Clojure的过程中,我发现了两个有趣的语句:


  1. 函数式程序员使用地图,OO程序员使用对象/类(Clojure for Java Programmers),而不是自上而下('On Lisp',Paul Graham)


那么什么是功能应用程序的系统或Clojure?什么是通用步骤,我使用什么工件,如何将它们从问题空间映射到解决方案空间?

解决方案

感谢软件工程人员还没有发现功能编程。这里有一些相似之处:




  • 许多OO设计模式被捕获为高阶函数。例如,访问者模式在功能世界中被称为折叠(或者如果你是一个尖锐的理论家,变态)。在函数式语言中,数据类型主要是树或元组,并且每个树类型都具有与其相关的自然变态。



    这些高阶函数通常伴随着某些程序规则,也称为自由定理。


  • p>功能程序员使用的图比OO程序员少得多。在OO图中表示的大部分表示在类型或签名中,您应该将其视为模块类型。 Haskell还有类型类,这有点像一个接口类型。



    使用类型的函数式程序员一般认为一旦你得到正确的类型;



    并非所有函数式语言都使用显式类型,但如何设计程序 book,一本优秀的学习计划Scheme / Lisp / Clojure非常依赖于与数据类型密切相关的数据描述。





那么,什么是功能应用程序的系统的(基于模型的?)设计的方法,即在Lisp或Clojure中?


任何基于数据抽象的设计方法都很好。我偶然认为这是更容易的,当语言有显式类型,但它工作甚至没有。关于抽象数据类型的设计方法的一本好书,易于适用于函数式编程,是Barbara Liskov和John Guttag的第一版版本的 程序开发中的抽象和规范。利斯科夫赢得了图灵奖。这一工作的一部分。



Lisp独有的另一种设计方法是决定哪些语言扩展在问题领域是有用的正在工作,然后使用卫生宏将这些构造添加到您的语言。阅读有关这种设计的好地方是Matthew Flatt的文章 在球拍中创建语言 。文章可能在付费墙后面。您还可以通过搜索领域专用嵌入语言这一术语找到更多关于这种设计的一般材料;对于超出Matthew Flatt所涵盖范围的特殊建议和示例,我可能会先从Graham的 在Lisp上 或者 ANSI Common Lisp



< blockquote>

通常的步骤是什么,我使用什么工件?


常见步骤:


  1. 识别程序中的数据及其操作,并定义表示此数据的抽象数据类型。

    li>
  2. 识别常见的操作或计算模式,并将其表示为较高阶函数或宏。


  3. 如果您使用的是类型化的函数式语言,请尽早使用类型检查器。如果你使用Lisp或Clojure,最好的做法是先编写函数契约,包括单元测试 - 它是测试驱动开发的最大。并且您将希望使用已移植到您的平台的任何版本的QuickCheck,在您的情况下,它被称为 ClojureCheck 。这是一个非常强大的库,用于构建使用高阶函数的代码的随机测试。



Software Engineering as it is taught today is entirely focused on object-oriented programming and the 'natural' object-oriented view of the world. There is a detailed methodology that describes how to transform a domain model into a class model with several steps and a lot of (UML) artifacts like use-case-diagrams or class-diagrams. Many programmers have internalized this approach and have a good idea about how to design an object-oriented application from scratch.

The new hype is functional programming, which is taught in many books and tutorials. But what about functional software engineering? While reading about Lisp and Clojure, I came about two interesting statements:

  1. Functional programs are often developed bottom up instead of top down ('On Lisp', Paul Graham)

  2. Functional Programmers use Maps where OO-Programmers use objects/classes ('Clojure for Java Programmers', talk by Rich Hickley).

So what is the methodology for a systematic (model-based ?) design of a functional application, i.e. in Lisp or Clojure? What are the common steps, what artifacts do I use, how do I map them from the problem space to the solution space?

解决方案

Thank God that the software-engineering people have not yet discovered functional programming. Here are some parallels:

  • Many OO "design patterns" are captured as higher-order functions. For example, the Visitor pattern is known in the functional world as a "fold" (or if you are a pointy-headed theorist, a "catamorphism"). In functional languages, data types are mostly trees or tuples, and every tree type has a natural catamorphism associated with it.

    These higher-order functions often come with certain laws of programming, aka "free theorems".

  • Functional programmers use diagrams much less heavily than OO programmers. Much of what is expressed in OO diagrams is instead expressed in types, or in "signatures", which you should think of as "module types". Haskell also has "type classes", which is a bit like an interface type.

    Those functional programmers who use types generally think that "once you get the types right; the code practically writes itself."

    Not all functional languages use explicit types, but the How To Design Programs book, an excellent book for learning Scheme/Lisp/Clojure, relies heavily on "data descriptions", which are closely related to types.

So what is the methodology for a systematic (model-based ?) design of a functional application, i.e. in Lisp or Clojure?

Any design method based on data abstraction works well. I happen to think that this is easier when the language has explicit types, but it works even without. A good book about design methods for abstract data types, which is easily adapted to functional programming, is Abstraction and Specification in Program Development by Barbara Liskov and John Guttag, the first edition. Liskov won the Turing award in part for that work.

Another design methodology that is unique to Lisp is to decide what language extensions would be useful in the problem domain in which you are working, and then use hygienic macros to add these constructs to your language. A good place to read about this kind of design is Matthew Flatt's article Creating Languages in Racket. The article may be behind a paywall. You can also find more general material on this kind of design by searching for the term "domain-specific embedded language"; for particular advice and examples beyond what Matthew Flatt covers, I would probably start with Graham's On Lisp or perhaps ANSI Common Lisp.

What are the common steps, what artifacts do I use?

Common steps:

  1. Identify the data in your program and the operations on it, and define an abstract data type representing this data.

  2. Identify common actions or patterns of computation, and express them as higher-order functions or macros. Expect to take this step as part of refactoring.

  3. If you're using a typed functional language, use the type checker early and often. If you're using Lisp or Clojure, the best practice is to write function contracts first including unit tests—it's test-driven development to the max. And you will want to use whatever version of QuickCheck has been ported to your platform, which in your case looks like it's called ClojureCheck. It's an extremely powerful library for constructing random tests of code that uses higher-order functions.

这篇关于有功能编程的软件工程方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆