“面向语言的编程”是如何实现的?与现实世界中的 OOP/函数式进行比较
我最近开始阅读一些与 F# 相关的文献,例如“真实世界函数式编程”和“专家 F#”。一开始很容易,因为我有一些 Haskell 背景,并且了解 C#。但是当谈到“面向语言的编程”时,我就是不明白。 - 我读了一些解释,就像读一篇学术论文,每句话都变得更加抽象和奇怪。
有没有人有此类内容的简单示例以及它与现有范例的比较如何?这不仅仅是学术幻想,不是吗? ;)
谢谢, 威希
I recently began to read some F# related literature, speaking of "Real World Functional Programming" and "Expert F#" e. g.. At the beginning it's easy, because I have some background in Haskell, and know C#. But when it comes to "Language Oriented Programming" I just don't get it. - I read some explanations and it's like reading an academic paper that gets more abstract and strange with every sentence.
Does anybody have an easy example for that kind of stuff and how it compares to existing paradigms? It's not just academic fantasy, isn't it? ;)
Thanks,
wishi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
面向语言的程序(LOP)可用于描述以下任何内容。
创建外部语言 (DSL)
这可能是 LOP 最常见的用法,并且是您拥有特定域的地方 - 例如 UPS 通过运输类型通过路线运输包裹等。而不是尝试对所有这些特定于域的内容进行编码程序代码内部的实体,您宁愿为该域创建单独的编程语言。因此,您可以用单独的外部语言对您的问题进行编码。
创建内部语言
有时,您希望程序代码看起来不太像“代码”,而是更接近问题域。也就是说,让代码“更自然地阅读”。流畅的界面就是一个例子:流畅的界面。此外,F# 具有很好地支持这一点的活动模式。
我写了一篇博客文章 不久前关于 LOP 的内容提供了一些代码示例。
Language oriented program (LOP) can be used to describe any of the following.
Creating an external language (DSL)
This is perhaps the most common use of LOP, and is where you have a specific domain - such as UPS shipping packages via transit types through routes, etc. Rather than try to encode all of these domain-specific entities inside of program code, you rather create a separate programming language for just that domain. So you can encode your problem in a separate, external language.
Creating an internal language
Sometimes you want your program code to look less like 'code' and map more closely to the problem domain. That is, have the code 'read more naturally'. A fluent interface is an example of this: Fluent Interface. Also, F# has Active Patterns which support this quite well.
I wrote a blog post on LOP a while back that provides some code examples.
F# 有一些机制可以以一种可能称为“面向语言”的风格进行编程。
首先,语法的优点(函数调用不需要括号,可以定义自己的中缀运算符,...)使得许多用户定义的库具有嵌入式 DSL 的外观。
其次,F#“引用”机制可以让您引用代码,然后使用替代语义/评估引擎运行它。
第三,F#“计算表达式”(又名工作流、单子等)还提供了一种为某些代码块提供一种替代语义的方法。
所有这些都属于 EDSL 类别。
F# has a few mechanisms for doing programming in a style one might call "language-oriented".
First, the syntax niceties (function calls don't need parentheses, can define own infix operators, ...) make it so that many user-defined libraries have the appearance of embedded DSLs.
Second, the F# "quotations" mechanism can enable you to quote code and then run it with an alternative semantics/evaluation engine.
Third, F# "computation expressions" (aka workflows, monads, ...) also provide a way to provide a type of alternative semantics for certain blocks of code.
All of these kinda fall into the EDSL category.
在面向对象编程中,您尝试使用对象来建模问题。然后,您可以将这些对象连接在一起以执行功能......并最终解决原始问题。
在面向语言编程中,您不是使用现有的面向对象或函数式编程语言,而是设计一种最适合有效解决问题的新的领域特定语言。
In Object Oriented Programming, you try to model a problem using Objects. You can then connect those Objects together to perform functions...and in the end solve the original problem.
In Language Oriented Programming, rather than use an existing Object Oriented or Functional Programming Language, you design a new Domain Specific Language that is best suited to efficiently solve your problem.
“面向语言编程”一词可能被过度使用,因为它对不同的人可能有不同的含义。
但就我如何使用它而言,这意味着您创建了一个 DSL(http://en .wikipedia.org/wiki/Domain_Specific_Language),然后再开始解决问题。
创建 DSL 后,您就可以根据 DSL 编写程序。
这个想法是,DSL 比通用语言更适合表达问题。
一些示例是 make 文件语法或 Ruby on Rails ActiveRecord 类。
The term language Oriented Programming may be overloaded in that it might have different meanings to different people.
But in terms of how I've used it, it means that you create a DSL(http://en.wikipedia.org/wiki/Domain_Specific_Language) before you start to solve your problem.
Once your DSL is created you would then write your program in terms of the DSL.
The idea being that your DSL is more suited to expressing the problem than a General purpose language would be.
Some examples would be the make file syntax or Ruby on Rails ActiveRecord class.
我还没有在现实世界中直接使用面向语言的编程(创建实际的语言),但是思考并帮助设计更好的领域驱动对象很有用。
从某种意义上说,Lisp 或Scheme 中的任何现实世界开发都可以被视为“面向语言”,因为您在编写代码时正在开发应用程序及其抽象树的“语言”。 Cucumber 是我的另一个真实示例听说过。
请注意,这种方法(以及任何领域驱动方法)在实际开发中存在一些问题。我之前处理过的一个主要问题是在领域中有意义的逻辑与在软件中有意义的逻辑之间的不匹配。领域(业务)逻辑可能极其复杂且毫无意义——并导致领域模型崩溃。
I haven't directly used language oriented programming in real-world situations (creating an actual language), but it is useful to think about and helps design better domain-driven objects.
In a sense, any real-world development in Lisp or Scheme can be considered "language-oriented," since you are developing the "language" of your application and its abstract tree as you code along. Cucumber is another real-world example I've heard about.
Please note that there are some problems to this approach (and any domain-driven approach) in real-world development. One major problem that I've dealt with before is mismatch between the logic that makes sense in the domain and the logic that makes sense in software. Domain (business) logic can be extremely convoluted and senseless - and causes domain models to break down.
此处提到了一个特定于域的语言的简单示例, 是 SQL。另外:UNIX shell 脚本。
当然,如果您正在执行大量基本操作并且与底层语言有很多重叠,则可能是过度设计。
An easy example of a domain-specific language, mentioned here, is SQL. Also: UNIX shell scripts.
Of course, if you are doing a lot of basic ops and have a lot of overlap with the underlying language, it is probably overengineering.