Functional Programming & Haskell - Computerphile

Computerphile
30 Nov 201609:19

Summary

TLDRこのスクリプトでは、関数プログラミングの基本理念とその利点を紹介しています。関数プログラミングでは、副作用のない関数を使用し、入力と出力のみで動作するため、バグの検出が容易です。また、Haskellなどの関数型言語は、FacebookのスパムフィルタリングやWhatsAppのバックエンドなど、インターネットサービスの重要な役割を果たしています。さらに、関数プログラミングは並列処理に適しており、テスト自動化ツールQuickCheckの開発にも役立つかが示されています。

Takeaways

  • 📚 函数式编程是一种编程范式,其中函数没有副作用,即函数调用仅依赖于输入并产生输出,不修改输入或产生其他影响。
  • 🔍 函数式编程有助于减少由于函数副作用导致的难以发现的错误,这些错误在传统编程语言中容易被忽视。
  • 🌐 函数式编程语言在互联网应用的服务器端有广泛应用,例如Haskell用于Facebook的垃圾邮件过滤,Erlang用于WhatsApp。
  • 🌟 Haskell语言是由Paul Hudak等人发起,集合了当时世界各地研究者共同的编程语言核心特性而设计出来的。
  • 🎭 Haskell的名字来源于Haskell Curry,一位早期的逻辑学家,对Lambda演算有贡献,命名过程中有一段有趣的小插曲。
  • 🔢 函数式编程与数学类似,遵循特定的法则,允许程序的不同表达方式之间进行替换,而不会改变程序的整体行为。
  • 🚀 现代函数式编程语言通常内置了内存管理等高级特性,使得编写的代码可以与使用传统语言编写的代码在性能上相媲美或更优。
  • 🔒 函数式编程由于其无副作用的特性,使得代码更易于并行化,有助于利用多核处理器提高性能。
  • 🛡️ 函数式编程在安全性方面也有潜力,例如Haskell中的库可以用来强制执行隐私约束,有助于创建防黑客攻击的代码。
  • 🔧 QuickCheck是一种自动化测试工具,它允许开发者编写测试代码来生成大量测试用例,以确保软件质量。
  • 🔬 通过QuickCheck的应用和商业化,开发者和研究者能够解决实际问题,并从中发现新的研究课题。

Q & A

  • 関数型プログラミングとはどのようなプログラミング方式ですか?

    -関数型プログラミングは、関数が副作用を持たないプログラミング方式です。つまり、関数は入力を与えられたら出力を返し、それ以外の何も行いません。

  • 関数型プログラミングにおける副作用とは何を指しますか?

    -副作用とは、関数が実行時に外部の変数を変更したり、他の操作を行ったりすることを指します。関数型プログラミングでは、関数は純粋で副作用を持ちません。

  • 関数型プログラミングでの関数の特性は何ですか?

    -関数型プログラミングでの関数は、入力と出力を返すのみで、それ以外の操作は行いません。そのため、関数の振る舞いは入力と出力を通じて完全に理解できます。

  • 関数型プログラミングはなぜバグを見つけるのが難しい状況を避けられますか?

    -関数型プログラミングでは、関数が副作用を持ちませんので、関数の結果以外に何も起こらないため、バグを見つけるのが容易です。

  • 関数型プログラミング言語はどのような用途で使われていますか?

    -関数型プログラミング言語は、インターネットアプリケーションのサーバーサイド、スペムフィルタリング、通信アプリケーションなど、多岐にわたって使われています。

  • Haskell言語はどのような場面で使われていますか?

    -Haskellは、FacebookのスペムフィルタリングやWhatsAppのバックエンドなど、実際に使われています。

  • 関数型プログラミング言語のパフォーマンスはどのようにですか?

    -関数型プログラミング言語は、より高いレベルでプログラミングを行うため、パフォーマンス面で若干のペナルティがありますが、最新のコンパイラ技術により、従来のプログラミング言語と同等またはそれ以上の速度が得られます。

  • 関数型プログラミング言語の特徴として並列実行の安全性はどのようにして実現されていますか?

    -関数型プログラミング言語では、関数が副作用を持ちないため、独立した複数の式を並列で評価しても問題ありません。これは並列処理が容易であることを意味します。

  • Haskell言語の名前の由来は何ですか?

    -Haskell言語は、ラムダ計算に貢献した初期の論理学者であるHaskell Curryにちなんで名付けられました。

  • 関数型プログラミングにおける数学的な法則とは何ですか?

    -関数型プログラミングでは、数学の法則と同様に、例えば、x + yをy + xに置き換えることができ、プログラムの振る舞いは変わりません。これらの法則はプログラムの理解と最適化に役立ちます。

  • QuickCheckとはどのようなツールですか?

    -QuickCheckは、手動でテストを書くのではなく、プログラムの振る舞いを記述し、自動的に多数のテストケースを生成してプログラムが期待通りに動作するかを検証するツールです。

  • 関数型プログラミングで書かれたコードはハックに強いコードになる可能性があるとありますが、その理由は何ですか?

    -関数型プログラミングで書かれたコードは、関数が副作用を持ちないため、特定のプライバシー制約を強制するライブラリを組み込むことができ、ハックに強いコードを作り出す可能性があります。

Outlines

00:00

📚 関数型プログラミングの基本

第1段落では、関数型プログラミングの核心である副作用のない関数の概念が説明されています。関数が入力をとって出力するだけで、それ以外の影響を与えないという特性が強調されています。このスタイルでのプログラミングは、バグを見つけるのが困難なミスを避けることができます。また、Facebookのスパムフィルタリングで使われているHaskellやWhatsAppのバックエンドで使われているAirline、TwitterのScalaなど、関数型プログラミング言語がウェブサービスのバックボーンとして広く使われている例が紹介されています。

05:00

🔧 関数型プログラミングの利点と研究

第2段落では、関数型プログラミング言語の利点について詳しく説明されています。古いプログラミング言語に比べて、多くの実装の詳細を管理してくれる点や、並列実行の安全性、そして高水準なプログラミング言語であるにも関わらず、コンパイラの最適化により高性能なコードを生成できる点が強調されています。また、Haskellにおけるプライバシーに関する制約を強制するライブラリの開発や、自動テストジェネレーターであるQuickCheckの開発とその応用についても触れられています。

Mindmap

Keywords

💡関数型プログラミング

関数型プログラミングは、関数が副作用を持たないプログラミングスタイルです。これは、関数が入力を受け取り出力するのみで、それ以外の変更は行わないことを意味します。この概念は、ビデオの主題であるソフトウェアの信頼性とバグの排除に関連しています。ビデオでは、関数型プログラミングが、入力と出力のみによって関数の挙動を理解できるようにし、副作用を忘れることによるバグのリスクを減らす方法として紹介されています。

💡副作用

副作用とは、関数が実行時に意図しない変更を加えることを指します。例えば、関数が外部の変数を変更したり、システムの状態を変更したりすることです。ビデオでは、関数型プログラミングが副作用を避けることにより、プログラムの予測可能性を高め、バグを見つけるのを困難にする副作用を排除する利点を強調しています。

💡Haskell

Haskellは、関数型プログラミング言語の1つで、ビデオではFacebookのスパムフィルタリングに使用されている例として挙げられています。Haskellは、関数型プログラミングの主要な言語であり、ビデオのテーマに沿って、高い信頼性と並列処理の利点を提供する能力を持つことを示しています。

💡並列処理

並列処理は、複数の処理を同時に実行するコンピューティング技術です。関数型プログラミング言語は、関数が副作用を持たないため、独立した表現を並列に安全に評価できる利点があります。ビデオでは、この特性がプログラムのパフォーマンス向上につながることを説明しています。

💡Lambda 計算

Lambda 計算は、関数型プログラミングの基礎となる数学モデルです。ビデオでは、Haskell言語の名前の由来として、Lambda 計算と関連する早いロジシスタンの1人であるHaskell Curryが言及されています。Lambda 計算は、関数型プログラミングにおける関数の抽象化と適用の概念を表しています。

💡QuickCheck

QuickCheckは、Haskell言語で使用される自動テスト生成ツールです。ビデオでは、ソフトウェアの複雑性により手動でのテストは限界があるとされており、QuickCheckを使用することで、プログラムの振る舞いを定義したテストコードから多数のテストケースを自動生成し、バグの検出を支援することができると説明されています。

💡メモリ管理

メモリ管理は、プログラムが実行時にメモリを効果的に使用し、解放するプロセスです。ビデオでは、関数型プログラミング言語がメモリ管理を組み込んでおり、これにより開発者がより高いレベルでプログラミングを行うことができると紹介されています。

💡並行性

並行性は、複数の処理を同時に実行することで、システムのパフォーマンスを向上させる概念です。ビデオでは、関数型プログラミングが関数の副作用の欠如により、並列処理を容易に行うことができ、従来のプログラミング言語以上にパフォーマンスを提供できる点に触れています。

💡セキュリティ

セキュリティは、ソフトウェアが不正アクセスやデータ漏洩から守られる能力です。ビデオでは、関数型プログラミング言語のHaskellに組み込まれたライブラリを使用して、特定のプライバシー制約を強制し、セキュリティを高める取り組みが行われていることを説明しています。

💡数学的方程式

数学的方程式は、関数型プログラミングと数学の間の類似性を示す例としてビデオで使用されています。関数型プログラミングでは、数学のように、特定の法則に従ってプログラムを書き換えることができ、その結果、プログラムの全体的な動作は変わりません。これは、プログラムの振る舞いを理解し、パフォーマンスを向上させるためにプログラムを自由に書き換えることができることを意味しています。

💡Yahoo! Pipes

Yahoo! Pipesは、ビデオスクリプトの最後の部分で言及されている、入力から出力を変換するプロセスを表す例です。これは、関数型プログラミングにおける関数の概念を示しており、入力値に基づいて特定の出力を生成するプロセスを表しています。

Highlights

Functional programming emphasizes functions without side effects, where outputs are solely determined by inputs.

Mistakes in conventional programming often stem from overlooked side effects of functions, leading to difficult-to-find bugs.

Functional programming languages have significantly impacted server-side internet applications.

Haskell, a language involved in its design, is utilized for spam filtering on Facebook.

Erlang, another functional language, powers the backend of WhatsApp.

Twitter's code is built using Scala, a language inspired by Haskell and well-integrated with Java, predominantly functional in nature.

Functional programming's higher-level abstraction can lead to performance penalties, but advances in compiler technology have mitigated this.

Functional programs can be safely evaluated in parallel due to the absence of side effects, facilitating multi-core utilization.

Efforts are underway to create hack-proof code using functional programming, particularly in enforcing privacy constraints.

The development of Haskell was a collaborative effort among researchers, aiming to unify the common core of functional languages.

The name 'Haskell' honors Haskell Curry, an early logician who contributed to lambda calculus.

Functional programs resemble mathematical equations, allowing for the application of algebraic laws in program reasoning and optimization.

QuickCheck, a property-based testing tool, automates the generation of tests to ensure software quality.

The speaker founded a company to commercialize QuickCheck, applying it to various real-world software problems.

Functional programming's impact extends to practical applications, with many everyday services running on functional code.

The speaker's work with QuickCheck has provided a wealth of research problems and insights into software testing.

The transcript discusses the potential for functional programming to improve software reliability and security.

Transcripts

play00:00

functional programming is

play00:01

essentially kind of programming where

play00:05

functions don't have side effects

play00:06

So what does it mean when you call a function you give it some inputs?

play00:10

And it will return some output and that's all that it does it doesn't modify the inputs

play00:16

It doesn't do something on the side people often talk about firing the missiles

play00:20

It can't do that

play00:21

the entire behavior of the function you can see by [looking] at what the inputs were and what the apple was that was generated and

play00:27

if you [program] [in] that style, then functions are

play00:30

you know you know a lot [about] their behavior and

play00:33

Many of the mistakes that are easy to make when you program in more conventional programming languages

play00:38

is that [you] forget about some effects that a function has in addition to returning its result and

play00:44

What you forget about those effects then they can lead to very difficult to find bugs and a lot of wasted time?

play00:54

What did I get used for these languages?

play00:58

Quite a variety of things they've made a lot of impact on the server side of internet applications. So for example haskell

play01:05

Which is the language that I played a part in designing. That's now used for all the spam filtering on Facebook

play01:12

now when I heard that first of all I said, I didn't think there was spam on Facebook and

play01:19

Sally Marlowe, the man is now in charge of this said to me exactly

play01:23

That's how good we [are]

play01:25

All of that is running in Haskell nowadays

play01:28

another example would be airline that's another functional [programming] [language] which I'm working with a lot at the moment [and]

play01:34

Airline was used to implement. Whatsapp always everybody uses whatsapp at some point

play01:39

that's all airline code running behind it there are other examples Twitter's code is built using scala which is a

play01:47

programming language that I think was heavily inspired by Haskell, but

play01:51

integrates very well with Java and

play01:54

but it's also mostly functional and

play01:56

So you know many of the services that we use [everyday] are actually running functional code in the background

play02:00

You've just mentioned then you had a hand in the development of haskell. Where did you start?

play02:05

[did] you have a big team that you weren't where tell me about that? Oh?

play02:09

so that was interesting and

play02:12

so when I first got in functional programming there were small number of people working on it at different universities around the world and

play02:19

In those days you couldn't really get hold of a functional programming language from somewhere else you had to write your own compiler

play02:25

So many of us around the [world] were doing that and then after [a] while

play02:30

We realized and it was a man called Paul [hugh] deck yale university took the first initiative to this

play02:36

We realized that we were all working with very much the same kind of programming language

play02:42

But because we each had our own compiler. We couldn't share any code

play02:46

We couldn't share our results

play02:47

And that just seemed [like] a waste of effort so that led to the proposal [tooned] just

play02:54

Take the common core [of] what we were all doing put it together and [designer] a programming language that we all use

play03:00

Which became haskell what where did the name come from then?

play03:04

It's actually named after

play03:06

Haskell Curry who was

play03:08

One of the early logicians who work with with lambda calculus the first time that the committee that was formed designed the language got together?

play03:16

then we decided we had to choose a name and

play03:19

nobody really knew [what] to choose so we

play03:23

Everybody was allowed to write up one proposal on the blackboard

play03:26

and

play03:27

Then everybody was allowed to go round and cross off one proposal now you might think that we would end up with zero names that

play03:33

Way, but no there was one name that

play03:36

Survived and it was curry

play03:40

named after

play03:41

Haskell curry

play03:43

So that was fine. We thought okay the new language will be called curry and

play03:47

This is last thing in the evening. So we all went to bed

play03:51

which meant we had a chance to sleep on it and next morning one of us realized that one of the

play03:58

Abstract machines that was very popular at that time was called the Tim

play04:03

Tim curry

play04:08

Oh dear [that] [wouldn't] be good and so we decided to call it haskell instead, so it's still named after haskell curry

play04:15

But his first name rather than second from what I've [seen] when you look at a functional program

play04:19

It is very much like a mathematical equation am [I] right with that. Yes, that's right. So one of the things is that like mathematics

play04:28

Functional programs will satisfy what we call laws for example in mathematics if you see

play04:33

x plus y you can always replace it by y plus x if that's more convenient and in

play04:40

Functional programs then very often you may be able to replace one program by another so you have different ways of expressing the same thing

play04:47

And you can replace one by the other which you may want to do because one of them may be much more

play04:51

efficient than the other

play04:53

and you know

play04:55

that that replacement will not change the overall behavior of your program and

play05:00

So we find that these laws extremely important for reasoning about programs and they can let you

play05:06

Rewrite your program quite freely in order to try and improve its performance for example and be sure that you're not introducing bugs

play05:12

[I'm] assuming that the same kind of

play05:15

Mechanics are happening somewhere at some level on the computers with any other programming language though is a [repeal] for this is it slow is

play05:22

It faster. What's that? What's the deal? Oh, well, so

play05:25

Functional programming languages take care of a lot [of] the implementation details that an older programming languages you have to do

play05:32

manually for example memory management

play05:35

but nowadays

play05:36

It's very popular to use languages like even Java for example which builds memory management into the programming language functional languages do that too

play05:45

Very often there is some kind of performance penalty for this and that has meant that

play05:51

One of the very active parts of functional programming research has been developing compilers that [can] give good performance

play05:58

Despite the fact that you're programming at a higher level

play06:01

nowadays

play06:02

Code written and functional languages can be as fast as or sometimes faster than code written in conventional programming languages

play06:08

one of the very nice benefits of functional program by the way

play06:12

is that it's always safe to evaluate two independent expressions in Parallel and

play06:19

That's because functions have no side effects so you can all the functions going to do is convert inputs to outputs

play06:25

And you can have two of those computations going on at the same time they cannot interfere

play06:32

Whereas if you use

play06:34

Programming languages in which each function may be modifying data somewhere else at the same time then you can't tell whether or not

play06:40

Two functions will interfere they're very likely to and that means that when you want to make programs run in parallel

play06:47

When you want to make them use more than one of the cores in your laptop

play06:51

Then it's much easier to do that if you start with a functional program and that can give a performance benefit over and above

play06:57

What conventional programming languages have I've heard as well?

play07:01

there is [a] possibility to make hack-proof code using functional programming is that something you could tell me about or

play07:08

Yes, so there is a lot of work in trying to make of the cannot be hacked of for example. Can't leak

play07:14

confidential Data and

play07:16

There are people who are working on programming languages that will provide some guarantees about that

play07:22

one of the quite successful approaches is to build a library into haskell in fact that can be used for

play07:29

enforcing in particular Privacy constraints

play07:32

So yes, there's work [going] on in [that] area

play07:35

We're going to talk about things like your quick check

play07:37

And you know that we've already filmed some stuff on that and what do you up to? What do you do?

play07:41

What's what's next for you?

play07:43

So I'm devoting most of my time to quick check nowadays

play07:46

where the basic idea is not to write tests by hand because

play07:54

Because basically it doesn't work

play07:56

Software is so complex that to make sure there are no bugs in it you have to try all kinds of weird

play08:01

Combinations, and there are just too many combinations to try for people to be able to try them all so really the only way to

play08:09

ensure that you

play08:11

Avoid these bugs is to generate tests instead

play08:13

That's what quick check does it lets you write test

play08:16

Code that says what your program [should] do and then it generates as many tests as you want to check that it does

play08:23

And I've been working with quick check for many years now [10] years ago

play08:28

I founded a company that is commercializing quick check

play08:31

And that's been huge fun because it means I get [to] apply

play08:34

this technology to all of real problems all kinds [of] different kinds of software, and that's been a huge learning experience for me and a

play08:42

Tremendous source of

play08:45

Research problems when we find oh, it's hard to apply it in this case well

play08:48

There's a research [property] to solve so [as] a professor and a researcher

play08:52

It's given me all kinds of new ideas for problems to work on which which has been very very stimulating

play08:59

and

play09:01

the value of y at this position is going to be some polynomial that we have to find which is a

play09:08

x cubed plus [be] x squared

play09:12

Plus C. X plus d. There are four unknowns here x we know because we're going between no [1] 1

Rate This

5.0 / 5 (0 votes)

Related Tags
関数プログラミングコードセキュリティ並列実行HaskellFacebookWhatsAppScalaテスト自動化プログラミング言語QuickCheckメモリ管理
Do you need a summary in English?