函数式编程范式是否有可视化建模语言或风格?

发布于 2024-08-03 18:46:11 字数 537 浏览 8 评论 0 原文

UML 是一个旨在用 OO 语言编写的软件建模的标准,与 Java 齐头并进。尽管如此,它是否可以用于对以函数式编程范例编写的软件进行建模?考虑到嵌入的视觉元素,哪些图表会变得有用?

是否有一种针对函数式编程的建模语言,更具体地说是 Haskell?您推荐哪些用于组合图表的工具?

由 OP 于 2009 年 9 月 2 日编辑:

我正在寻找的是代码中所发生内容的最直观、最简单的表示。易于理解的图表、可视化模型不一定针对其他程序员。我很快就会在 Haskell 中开发一个游戏,但因为这个项目是我的毕业总结工作,所以我需要引入某种形式化的提议解决方案。我想知道是否有一个相当于 UML+Java 标准的东西,但是对于 Haskell。 我应该只坚持故事板、书面描述、非形式化图表(一些浅层流程图式图像)、非形式化用例描述吗?

由 jcolebrand 于 2012 年 6 月 21 日编辑:

请注意,提问者最初想要一个视觉隐喻,现在我们已经有了三年的时间,我们正在寻找更多/更好的工具。原始答案都没有真正解决“视觉隐喻设计工具”的概念,所以......这就是新赏金所寻求提供的。

UML is a standard aimed at the modeling of software which will be written in OO languages, and goes hand in hand with Java. Still, could it possibly be used to model software meant to be written in the functional programming paradigm? Which diagrams would be rendered useful given the embedded visual elements?

Is there a modeling language aimed at functional programming, more specifically Haskell? What tools for putting together diagrams would you recommend?

Edited by OP Sept 02, 2009:

What I'm looking for is the most visual, lightest representation of what goes on in the code. Easy to follow diagrams, visual models not necessarily aimed at other programmers. I'll be developing a game in Haskell very soon but because this project is for my graduation conclusion work I need to introduce some sort of formalization of the proposed solution. I was wondering if there is an equivalent to the UML+Java standard, but for Haskell.
Should I just stick to storyboards, written descriptions, non-formalized diagrams (some shallow flow-chart-like images), non-formalized use case descriptions?

Edited by jcolebrand June 21, 2012:

Note that the asker originally wanted a visual metphor, and now that we've had three years, we're looking for more/better tools. None of the original answers really addressed the concept of "visual metaphor design tool" so ... that's what the new bounty is looking to provide for.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

梦在深巷 2024-08-10 18:46:12

查看带有外链的存档版本此处

截至 2023 年 5 月 26 日,尚无标准建模语言纯函数式编程,但还有其他选择:

1. UML

UML 传统上与面向对象编程 (OOP) 联系在一起,主要是因为它的历史根源12,但是人们当然可以尝试将它用于不同的编程范例(或者用于除了建模软件之外的东西) ,就此而言,例如 用例图本体工程、制造等系统&流程等)。

话虽如此,这个用例似乎没有很多例子。值得注意的一些:

上面的答案提到除了类图之外还有 UML 图类型,它们的使用将使任何程序受益,而与范例无关。请参阅 1使用 UML 的应用 维基百科页面,以及图表:

UML 2.2 中的图表层次结构
来源

[1]: < a href="https://www.guru99.com/uml-diagrams.html" rel="nofollow noreferrer">UML 图:历史、类型、特征、版本、工具
[2]:https://stackoverflow.com/a/27861475/1498178

1.1 状态图

来自状态图:复杂系统的视觉形式主义论文:

[状态图]扩展了传统的状态转换图,本质上包含三个元素,分别处理层次结构、并发性和通信的概念。这些将状态图的语言转变为高度结构化且经济的
描述语言。

如果程序不处理状态,那么这不是很有用,但它似乎是一个值得注意的工具。

更多信息和讨论:

2. 图表

2.1 接线图

来自组合性的七个草图:应用范畴论的邀请2.2.2 接线图介绍

接线图是从旧关系建立新关系的视觉表示。

示例:

  • 在此处输入图像描述
  • 在此处输入图像描述

2.2 字符串图

组合性中的七个草图: 《应用范畴论邀请函》提到了弦图,但它们似乎与接线图不同。来自论文使用字符串图的范畴论

字符串图是范畴论中的一种图形形式,为描述二类中的态射提供了一种方便的工具[B ́enabou, 1967],有关字符串图的一些背景可以在[Street, 1995]中找到。

“字符串图形式的视觉证明"

2.3 数据流图

本节扩展了这个答案

2.3.1“亨德森图”

这些显示了各个功能如何“插入在一起”” 。来自 3.5.2 无限流 部分“https://sarabander.github.io/sicp/”rel =“nofollow noreferrer”>SICP:

图 3.31:将素筛视为信号处理系统。

2.3.2 使用的图表在 箭头 文档中

“在此处输入图像描述"

class 箭头 a 所在位置
  arr::(b -> c) -> ABC
-- 每个函数都可以被视为
 一个计算。

“在此处输入图像描述"

 (>>>) :: abc -> acd->阿卜杜勒
-- 计算可以由以下组成
-- 连接第一个的输出
-- 到第二个的输入。

“在此处输入图像描述"

 首先 :: abc -> (b,d) (c,d)
-- 计算可以应用于部分
-- 输入的内容,其余部分复制
-- 直到输出。

“在此处输入图像描述"

2.4 哈斯图 (维基百科)

"范畴论就是关于组织和分层结构"3 所以 哈斯图可能有用。 UML 类图是“哈斯图的一种形式 ”,因此可以在更一般的意义上使用它来传达依赖关系。

[3] 组合性的七个草图:应用范畴论的邀请,部分
"1.1.2 排序系统"

2.5 交换图 (wikipedia)

这些可用于描述函数和结构如何相互作用;然而,他们宁愿定义不变量/定律而不是行为。

示例:

3. 形式化方法

3.1 通用系统语言 (USL)(专有?)

通用系统语言(USL)是一种系统建模语言形式化方法,用于软件和其他复杂系统的规范和设计。它是由玛格丽特·汉密尔顿根据她为阿波罗计划编写飞行软件的经验而设计的。该语言通过 Hamilton Technologies, Inc. 的 001 Tool Suite 软件实现。

请参阅 Hamilton Technologies, Inc. 上的文章 .的主页。

3.2 TLA+

TLA+ 是 Leslie Lamport 开发的一种形式化规范语言。它用于程序的设计、建模、文档和验证,特别是并发系统和分布式系统。

在这里提及 TLA+ 也是明智之举。

4. 工具

4.1 ghc-vis

与 Python Tutor 类似,

ghc-vis 是一个在 GHCi 中可视化实时 Haskell 数据结构的工具。评估不是强制的,您可以与可视化数据结构进行交互。这可以让我们看到 Haskell 的惰性求值和共享的实际效果

“Haskell

4.2 Visual Haskell

本文提出并论证了实时信号处理系统编程框架。该框架扩展了现有的“框图”编程模型;它由三个组成部分:高级文本语言、视觉语言和计算的数据流过程网络模型。

来自 4.2 Visual Haskell 简介

Visual Haskell 中的阶乘函数

4.3 Viskell(工具;最后一次提交:2017 年)

Viskell 是一种用于类型化(类似 Haskell)函数式编程语言的实验性可视化编程环境。该项目正在结合函数式语言的优缺点来探索交互式可视化编程的可能性和挑战。

“Viskell

4.4 graphmod Haskell 包

将程序的模块依赖关系呈现为“点”图。

4.5 HOPS:高级对象编程系统

HOPS是一个基于术语图的图形交互程序开发和程序转换系统。

看起来没有维护,但看起来很有趣。

输入图片此处描述
此处查看更多图像/动画。


其他有趣的资源:

See archived version with outlinks here.

As of May 26, 2023, there is no standard modeling language for pure functional programming, but there are alternatives:

1. UML

UML has traditionally been associated with object-oriented programming (OOP), mostly because of its historical roots1, 2, but one can certainly attempt to use it for different programming paradigms (or for something other than modeling software, for that matter, such as use-case diagrams, ontology engineering, manufacture and other systems & processes, etc.).

With that said, there don't seem to be many examples around for this use case. A noteworthy few:

The answers above mention that there are UML diagram types other than class diagrams, whose use would benefit any program, independent of paradigm. See 1 and the Modeling applications of UML using various diagrams section of the Applications of UML wikipedia page, along with a diagram:

Hierarchy of diagrams in UML 2.2
source

[1]: UML Diagrams: History, Types, Characteristics, Versions, Tools
[2]: https://stackoverflow.com/a/27861475/1498178

1.1 Statecharts

From the Statecharts: a visual formalism for complex systems paper:

[statecharts] extend conventional state-transition diagrams with essentially three elements, dealing, respectively, with the notions of hierarchy, concurrency and communication. These transform the language of state diagrams into a highly structured and economical
description language.

If the program doesn't deal with state, then this is not very useful but it seems to be a noteworthy tool otherwise.

More info and discussions:

2. Diagrams

2.1 Wiring diagram

From Seven Sketches in Compositionality: An Invitation to Applied Category Theory, section 2.2.2 Introducing wiring diagrams:

Wiring diagrams are visual representations for building new relationships from old.

Examples:

  • enter image description here
  • enter image description here

2.2 String diagram

Seven Sketches in Compositionality: An Invitation to Applied Category Theory mentions string diagrams, but they seem to be different from wiring diagrams. From the paper Category Theory Using String Diagrams:

String diagrams are a graphical formalism for working in category theory, providing a convenient tool for describing morphisms within bicategories [B ́enabou, 1967], some background on string diagrams can be found in [Street, 1995].

A visual proof as a string diagram

2.3 Data flow diagrams

This section expands on this answer.

2.3.1 "Henderson diagram"

"These show how individual functions are "plugged together"". From section 3.5.2 Infinite Streams of SICP:

Figure 3.31: The prime sieve viewed as a signal-processing system.

2.3.2 Diagrams used in the Arrows documentation

enter image description here

class Arrow a where
  arr :: (b -> c) -> a b c
-- Each function may be treated as
 a computation.

enter image description here

  (>>>) :: a b c -> a c d -> a b d
-- Computations may be composed, by
-- connecting the output of the first
-- to the input of the second.

enter image description here

  first :: a b c -> a (b,d) (c,d)
-- A computation may be applied to part
-- of the input, with the rest copied
-- through to the output.

enter image description here

2.4 Hasse diagram (wikipedia)

"Category theory is all about organizing and layering structures"3 so Hasse diagrams may be useful. A UML class diagram is "a form of Hasse diagram", so it can be used in a more general sense to convey dependency relations.

[3] Seven Sketches in Compositionality: An Invitation to Applied Category Theory, section
"1.1.2 Ordering systems"

2.5 Commutative diagram (wikipedia)

These can be used to describe how functions and structures interact with each other; however, they rather define invariants/laws than behaviour.

Examples:

3. Formal methods

3.1 Universal Systems Language (USL) (proprietary?)

Universal systems language (USL) is a systems modeling language and formal method for the specification and design of software and other complex systems. It was designed by Margaret Hamilton based on her experiences writing flight software for the Apollo program. The language is implemented through the 001 Tool Suite software by Hamilton Technologies, Inc.

See articles on Hamilton Technologies, Inc.'s homepage.

3.2 TLA+

TLA+ is a formal specification language developed by Leslie Lamport. It is used for designing, modeling, documentation, and verification of programs, especially concurrent systems and distributed systems.

It felt prudent to mention TLA+ here as well.

4. Tools

4.1 ghc-vis

Similar to Python Tutor,

ghc-vis is a tool to visualize live Haskell data structures in GHCi. Evaluation is not forced and you can interact with the visualized data structures. This allows seeing Haskell’s lazy evaluation and sharing in action

Visualization of the execution of a Haskell program

4.2 Visual Haskell

This thesis presents and justifies a framework for programming real-time signal processing systems. The framework extends the existing "block-diagram" programming model; it has three components: a very high-level textual language, a visual language, and the dataflow process network model of computation.

From section 4.2 An introduction to Visual Haskell:

The factorial function in Visual Haskell

4.3 Viskell (tool; last commit: 2017)

Viskell is an experimental visual programming environment for a typed (Haskell-like) functional programming language. This project is exploring the possibilities and challenges of interactive visual programming in combination with the strengths and weaknesses of functional languages.

Screenshot of Viskell's user interface

4.4 graphmod Haskell package

Present the module dependencies of a program as a "dot" graph.

4.5 HOPS: The Higher Object Programming System

HOPS is a graphically interactive program development and program transformation system based on term graphs.

Seems unmaintained, but looked interesting.

enter image description here
See more images / animations here.


Other interesting resources:

赠佳期 2024-08-10 18:46:12

是的,Haskell 有广泛使用的建模/规范语言/技术。
它们不是视觉的。

在 Haskell 中,类型给出了部分规范。
有时,该规范完全确定了含义/结果,同时留下了各种实现选择。
超越 Haskell 到具有依赖类型的语言,例如 Agda 和 Agda 。 Coq(以及其他),类型作为完整规范通常更有用。

如果类型不够,请添加正式规范,通常采用简单的函数形式。
(因此,我相信,Haskell 选择的建模语言的答案是 Haskell 本身或“数学”。)
在这种形式中,您给出了一个功能定义,该定义为了清晰和简单而优化,而不是全部为了效率。
该定义甚至可能涉及不可计算的操作,例如无限域上的函数相等。
然后,逐步将规范转换为高效可计算的函数程序的形式。
每个步骤都保留语义(表示),因此最终形式(“实现”)在语义上保证与原始形式(“规范”)等效。
您会看到这个过程有各种名称,包括“程序转换”、“程序推导”和“程序计算”。

典型推导的步骤主要是“方程推理”的应用,并辅以一些数学归纳法(和共归纳法)的应用。
能够执行如此简单而有用的推理是函数式编程语言的主要动机,它们的有效性归功于“真正的函数式编程”的“外延”性质。
(术语“外延”和“真正功能”来自 Peter Landin 的开创性论文 接下来的 700 种编程语言。)
因此,纯函数式编程的口号曾经是“有利于等式推理”,尽管现在我很少听到这种描述。
在 Haskell 中,外延对应于 IO 之外的类型以及依赖于 IO 的类型(例如 STM)。
虽然指示/非 IO 类型有利于正确的等式推理,但 IO/非指示类型被设计为不利于错误的等式推理。

我在 Haskell 工作中尽可能频繁地使用的从规范派生的一个特定版本就是我所说的“语义类型类态射”(TCM)。
这里的想法是给出数据类型的语义/解释,然后使用 TCM 原理通过类型类实例来确定(通常是唯一的)大多数或全部类型功能的含义。
例如,我说 Image 类型的含义是作为 2D 空间的函数。
然后中医原理告诉我Monoid、Functor、Applicative、Monad、Contrafunctor的含义 和 Comonad 实例,与函数的这些实例相对应。
对于具有非常简洁和引人注目的规范的图像来说,这是很多有用的功能!
(规范是语义函数加上语义 TCM 原则必须成立的标准类型类列表。)
然而,我在如何表示图像方面拥有巨大的自由,并且语义 TCM 原则消除了抽象泄漏。
如果您想了解该原理的一些实际示例,请查看论文 具有类型类态射的指称设计

Yes, there are widely used modeling/specification languages/techniques for Haskell.
They're not visual.

In Haskell, types give a partial specification.
Sometimes, this specification fully determines the meaning/outcome while leaving various implementation choices.
Going beyond Haskell to languages with dependent types, as in Agda & Coq (among others), types are much more often useful as a complete specification.

Where types aren't enough, add formal specifications, which often take a simple functional form.
(Hence, I believe, the answers that the modeling language of choice for Haskell is Haskell itself or "math".)
In such a form, you give a functional definition that is optimized for clarity and simplicity and not all for efficiency.
The definition might even involve uncomputable operations such as function equality over infinite domains.
Then, step by step, you transform the specification into the form of an efficiently computable functional program.
Every step preserves the semantics (denotation), and so the final form ("implementation") is guaranteed to be semantically equivalent to the original form ("specification").
You'll see this process referred to by various names, including "program transformation", "program derivation", and "program calculation".

The steps in a typical derivation are mostly applications of "equational reasoning", augmented with a few applications of mathematical induction (and co-induction).
Being able to perform such simple and useful reasoning was a primary motivation for functional programming languages in the first place, and they owe their validity to the "denotative" nature of "genuinely functional programming".
(The terms "denotative" and "genuinely functional" are from Peter Landin's seminal paper The Next 700 Programming languages.)
Thus the rallying cry for pure functional programming used to be "good for equational reasoning", though I don't hear that description nearly as often these days.
In Haskell, denotative corresponds to types other than IO and types that rely on IO (such as STM).
While the denotative/non-IO types are good for correct equational reasoning, the IO/non-denotative types are designed to be bad for incorrect equational reasoning.

A specific version of derivation-from-specification that I use as often as possible in my Haskell work is what I call "semantic type class morphisms" (TCMs).
The idea there is to give a semantics/interpretation for a data type, and then use the TCM principle to determine (often uniquely) the meaning of most or all of the type's functionality via type class instances.
For instance, I say that the meaning of an Image type is as a function from 2D space.
The TCM principle then tells me the meaning of the Monoid, Functor, Applicative, Monad, Contrafunctor, and Comonad instances, as corresponding to those instances for functions.
That's a lot of useful functionality on images with very succinct and compelling specifications!
(The specification is the semantic function plus a list of standard type classes for which the semantic TCM principle must hold.)
And yet I have tremendous freedom of how to represent images, and the semantic TCM principle eliminates abstraction leaks.
If you're curious to see some examples of this principle in action, check out the paper Denotational design with type class morphisms.

红玫瑰 2024-08-10 18:46:12

我们使用定理证明器进行形式化建模(带验证),例如 Isabelle 或 Coq。有时,我们在推导“低级”Haskell 实现之前,使用特定于领域的语言(例如 Cryptol)进行高级设计。

通常我们只是使用 Haskell 作为建模语言,并通过重写得出实际的实现。

QuickCheck 属性以及类型和模块分解也在设计文档中发挥作用。

We use theorem provers to do formal modelling (with verification), such as Isabelle or Coq. Sometimes we use domain specific languages (e.g. Cryptol) to do the high level design, before deriving the "low level" Haskell implementation.

Often we just use Haskell as the modelling language, and derive the actual implementation via rewriting.

QuickCheck properties also play a part in the design document, along with type and module decompositions.

绾颜 2024-08-10 18:46:12

是的,哈斯克尔。

我的印象是,使用函数式语言的程序员在考虑他们的设计时并不觉得需要简化他们选择的语言,这是查看 UML 为您所做的事情的一种(相当圆滑的)方式。

Yes, Haskell.

I get the impression that programmers using functional languages don't feel the need to simplify their language of choice away when thinking about their design, which is one (rather glib) way of viewing what UML does for you.

铁憨憨 2024-08-10 18:46:12

我观看了一些视频采访,并阅读了一些对埃里克·梅耶尔和西蒙·佩顿·琼斯等人的采访。似乎在建模和理解问题域时,他们使用类型签名,尤其是函数签名。

序列图(UML)可以与函数的组合相关。
静态类图 (UML) 可能与类型签名相关。

I have watched a few video interviews, and read some interviews, with the likes of erik meijer and simon peyton-jones. It seems as when it comes to modelling and understanding ones problem domain, they use type signatures, especially function signatures.

Sequence diagrams (UML) could be related to the composition of functions.
A static class diagram (UML) could be related to type signatures.

北城孤痞 2024-08-10 18:46:12

虽然不建议使用(因为它似乎无法下载),但是 HOPS系统可视化术语图,这通常是功能程序的方便表示。

HOPS snapshot

它也可以被视为一种设计工具,因为它支持记录程序以及构建程序;我相信,如果您愿意的话,它也可以逐步重写术语,以便您可以看到它们展开

不幸的是,我相信它不再被积极开发。

Although not a recommendation to use (as it appears to be not available for download), but the HOPS system visualizes term graphs, which are often a convenient representation of functional programs.

HOPS screenshot

It may be also considered a design tool as it supports documenting the programs as well as constructing them; I believe it can also step through the rewriting of the terms if you want it to so you can see them unfold.

Unfortunately, I believe it is no longer actively developed though.

海夕 2024-08-10 18:46:12

在 Haskell 中,您可以按类型建模。

只需从编写函数、类和数据签名开始,无需任何实现,并尝试使类型适合。下一步是快速检查。

例如,对排序进行建模:

class Ord a where
    compare :: a -> a -> Ordering

sort :: Ord a => [a] -> [a]
sort = undefined

然后测试

prop_preservesLength l = (length l) == (length $ sort l)
...

,最后实现......

In Haskell, you model by types.

Just begin with writing your function-, class- and data signatures without any implementation and try to make the types fit. Next step is QuickCheck.

E.g. to model a sort:

class Ord a where
    compare :: a -> a -> Ordering

sort :: Ord a => [a] -> [a]
sort = undefined

then tests

prop_preservesLength l = (length l) == (length $ sort l)
...

and finally the implementation ...

赤濁 2024-08-10 18:46:12

我意识到我参加聚会迟到了,但我仍然会给出我的答案,以防有人发现它有用。

我想我会选择 SADT/IDEF0 等系统方法。

可以使用 Linux 上可用的 Dia 程序制作此类图表, Windows 和 MacOS。

I realize I'm late to the party, but I'll still give my answer in case someone would find it useful.

I think I'd go for systemic methodologies of the likes of SADT/IDEF0.

Such diagrams can be made with the Dia program that is available on both Linux, Windows and MacOS.

软的没边 2024-08-10 18:46:12

您可以按照实时信号处理:数据流中所述创建数据流处理网络模型,视觉和函数式编程 作者:Hideki John Reekie

例如,对于像 (Haskell) 这样的代码:

fact n | n == 0    = 1
       | otherwise = n * fact (n - 1)

视觉表示为:

在此处输入图像描述

You can a data flow process network model as described in Realtime Signal Processing: Dataflow, Visual, and Functional Programming by Hideki John Reekie

For example for code like (Haskell):

fact n | n == 0    = 1
       | otherwise = n * fact (n - 1)

The visual representation would be:

enter image description here

夜未央樱花落 2024-08-10 18:46:12

用数学建模 Haskell 有什么意义?我认为 Haskell 的全部意义在于它与数学密切相关,以至于数学家可以拿起它并运行它。为什么要将一种语言翻译成它本身?

当使用另一种函数式编程语言 (f#) 时,我在白板上使用图表来描述大块,然后使用 UML 和类以 OO 方式对系统进行建模。 f# 中的构建块存在轻微的不匹配(将类分为数据结构和作用于它们的函数)。但从商业角度来看,这是一种享受。我想补充一点,问题是面向业务/网络的,并且不知道该技术对于更财务的东西是否有效。我想我可能会将函数捕获为没有状态的对象,并且它们应该很好地适应。

这完全取决于您工作的领域。

What would be the point in modelling Haskell with Maths? I thought the whole point of Haskell was that it related so closely to Maths that Mathematicians could pick it up and run with it. Why would you translate a language into itself?

When working with another functional programming language (f#) I used diagrams on a white board describing the large blocks and then modelled the system in an OO way using UML, using classes. There was a slight miss match in the building blocks in f# (split the classes up into data structures and functions that acted upon them). But for the understanding from a business perspective it worked a treat. I would add mind that the problem was business/Web oriented and don't know how well the technique would work for something a bit more financial. I think I would probably capture the functions as objects without state and they should fit in nicely.

It all depends on the domain your working in.

夏夜暖风 2024-08-10 18:46:12

我使用 USL - 通用系统语言。我正在学习 Erlang,我认为它非常适合。

可惜文档非常有限,没有人使用它。
更多信息此处

I use USL - Universal Systems Language. I'm learning Erlang and I think it's a perfect fit.

Too bad the documentation is very limited and nobody uses it.
More information here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文