第7章高水準プログラミング言語
7.3 Alternative Programming Models7.3 ほかのプログラミングモデル
Learning Objectives学習目標
By the end of this section, you will be able to:
この節を終えると、次のことができるようになる。
• Discuss characteristics of functional programming
• 関数型プログラミングの特徴を論じる。
• Explain characteristics of declarative programming
• 宣言型プログラミングの特徴を説明する。
• Distinguish the characteristics of object-oriented programming
• オブジェクト指向プログラミングの特徴を見分ける。
• Explain HLL constructs used to support concurrency and parallelism
• 並行性と並列性を支える高水準言語の構成要素を説明する。
• Summarize when to use scripting languages
• スクリプト言語を使う場面をまとめる。
As we have learned, HLLs can be classified into various paradigms, two of which are imperative and declarative. There are other paradigms that fall within these major categories. Some worth investigating are functional programming, declarative programming, object-oriented programming, and parallel programming utilizing concurrency.
英語のヒント
これまで学んだように、高水準言語はさまざまなパラダイムに分類でき、そのうちの二つが命令型と宣言型である。これらの大きな分類の中に入る、ほかのパラダイムもある。検討する価値のあるものとして、関数型プログラミング、宣言型プログラミング、オブジェクト指向プログラミング、並行性を利用した並列プログラミングがある。
Functional Programming関数型プログラミング
In 1930, Alonzo Church developed the lambda calculus model of computing, which got its name from the Greek letter lambda (λ). In this model, each parameter was introduced by the lambda symbol and computation was performed by placing parameters into expressions in the same way that high-level programs transfer arguments to functions. Figure 7.20 highlights various HLLs.
英語のヒント
1930年、アロンゾ・チャーチはラムダ計算という計算モデルを開発した。この名前は、ギリシャ文字のラムダ(λ)に由来する。このモデルでは、各引数をラムダ記号で導入し、式へ引数を入れることで計算を行った。これは、高水準プログラムが関数へ実引数を渡すのと同じような方法である。図7.20に、さまざまな高水準言語を示す。

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 7.20 Functional programming languages can be differentiated by their paradigms. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図7.20 関数型プログラミング言語は、そのパラダイムによって区別できる。(出典:著作権 Rice University、OpenStax、CC BY 4.0ライセンス)
Functional Programming Concepts関数型プログラミングの概念
The functional programming model’s key concept is that programs are constructed by composing functions and applying them. A pure function is one that will return the same result every time given the same arguments, and there is no reading of shared mutable state. In this context, shared data means that the data is available to multiple program locations or scopes, state represents data that are remembered over time, and mutable means it is changeable. If the data used by the function is shared mutable, it is possible to return different results for the same arguments. A pure function may not have any side effects, meaning it cannot modify any state that is not local to it. For this reason, functional programs do not have assignment statements, which is also known as referential transparency.
英語のヒント
関数型プログラミングモデルの中心となる考え方は、関数を合成し、適用することでプログラムを構築するというものである。純粋関数とは、同じ引数を与えれば毎回同じ結果を返し、共有された可変状態を読み取らない関数である。ここで共有データとは、プログラムの複数の場所やスコープから利用できるデータをいう。状態は時間が経過しても保持されるデータを表し、可変とは変更できることを意味する。関数が使うデータが共有され、かつ可変であれば、同じ引数に対して異なる結果を返す可能性がある。純粋関数は副作用を一切持ってはならない。つまり、自分の局所的な範囲にない状態を変更できない。このため、関数型プログラムには代入文がない。これは参照透過性とも呼ばれる。
Some other necessary features and characteristics that may be missing in some imperative languages include first-class functions and high-order functions. A first-class function is one that can be assigned as a value to a variable. A high-order function is one that can take one or more first-class functions as arguments. It can also return functions as results, which gives a function the ability to apply functions to its arguments one at a time. This is very useful in making code both more flexible and simpler. It encourages the creation of smaller functions that take care of just one piece of a larger job, which makes for great maintainability as well.
英語のヒント
必要な機能や特徴のうち、命令型言語の一部にないことがあるものとして、第一級関数と高階関数も挙げられる。第一級関数とは、変数へ値として代入できる関数である。高階関数とは、一つ以上の第一級関数を引数に取れる関数である。また、結果として関数を返すこともできる。これによって、関数が自分の引数に一つずつ関数を適用できる。これは、コードの柔軟性を高めると同時に、単純にするうえでとても役立つ。大きな仕事の一部分だけを受け持つ小さな関数を作ることを促し、保守性も高くなる。
Functional Programming Assessment関数型プログラミングの評価
In the same way that you perform tasks over and over each day, functional programming treats functions used to execute these tasks as prime citizens. Some benefits of repetition include making programs easier to understand for the programmer as well as shortening programs. Some downsides of functional programming are having to move data through the functions, effective implementation is difficult (but not impossible), and new arrays must be created when data in an element are changed.
英語のヒント
毎日同じ作業を何度も行うのと同じように、関数型プログラミングでは、それらの作業を実行する関数を中心的な存在として扱う。繰り返しを利用する利点には、プログラマーにとってプログラムを理解しやすくすることや、プログラムを短くすることがある。関数型プログラミングの欠点には、関数を通じてデータを移動させる必要があること、効果的な実装が難しいこと(ただし不可能ではない)、要素内のデータが変わると新しい配列を作る必要があることがある。
Object-Oriented Programmingオブジェクト指向プログラミング
A number of imperative languages are considered object oriented, and most modern HLLs support this paradigm to some degree. Its basic characteristic is to design software around the concept of a class, which is the blueprint from which objects are constructed. Constructing an object is known as instantiation or building an instance of a class. Objects are made up of attributes (characteristics) and behaviors (methods or functions).
英語のヒント
命令型言語の中にはオブジェクト指向とされるものが多く、現代の高水準言語の大半は、このパラダイムをある程度支えている。その基本的な特徴は、クラスという概念を中心にソフトウェアを設計することである。クラスは、オブジェクトを作るための設計図である。オブジェクトを作ることをインスタンス化、またはクラスのインスタンスを作るという。オブジェクトは、属性(特徴)と振る舞い(メソッドまたは関数)で構成される。
The three main principles of OOP are encapsulation, inheritance, and polymorphism, which provide a very high capability of abstraction. With encapsulation, the attributes and behaviors of classes and objects are self-contained and go along with them throughout their lifetime. It also means that internal implementation is hidden from outside the class or object. In inheritance, objects take on specified attributes from their ancestors. An example is that a Dog object inherits from an Animal parent object. Polymorphism means that behaviors that are inherited may perform in different ways that depend upon their context. A Dog is an Animal object and makes noise. A Snake is also an Animal object that makes noise—a different noise.
英語のヒント
オブジェクト指向プログラミング(OOP)の三つの主要原則は、カプセル化、継承、ポリモーフィズムである。これらにより、非常に高い抽象化能力が得られる。カプセル化では、クラスやオブジェクトの属性と振る舞いがその内部にまとまり、生存期間を通じて一緒に保たれる。また、内部の実装が、クラスやオブジェクトの外から隠されることも意味する。継承では、オブジェクトは祖先から指定された属性を引き継ぐ。たとえば、Dogオブジェクトは、親であるAnimalオブジェクトから継承する。ポリモーフィズムは、継承した振る舞いが、文脈に応じて異なる方法で実行されうることを意味する。DogはAnimalオブジェクトで、音を出す。Snakeも音を出すAnimalオブジェクトだが、別の音を出す。
The degree to which an HLL conforms to these principles measures how object oriented it is. Java and C# are fully object oriented. C/C++ is a hybrid of procedural characteristics and OOP characteristics. Languages such as Python, JavaScript, and PHP partially ascribe to these characteristics. Therefore, they are sometimes referred to as object based.
英語のヒント
高水準言語がどれほどオブジェクト指向であるかは、これらの原則にどれほど従っているかで測られる。JavaとC#は完全なオブジェクト指向である。C/C++は、手続き型の特徴とOOPの特徴を混ぜたものである。Python、JavaScript、PHPなどの言語は、これらの特徴を部分的に取り入れている。そのため、オブジェクトベースと呼ばれることもある。