We've gained a good understanding of assignment as a tool in modeling,
as well as an appreciation of the complex problems that assignment
raises. It is time to ask whether we could have gone about things in a
different way, so as to avoid some of these problems. In this
section, we explore an alternative approach to modeling state, based
on data structures called streams. As we shall see, streams can
mitigate some of the complexity of modeling state.
これまでに、モデリングの道具としての代入についてよく理解するとともに、代入が引き起こす複雑な問題についても認識してきました。ここで、こうした問題のいくつかを避けるために、別のやり方で進められなかったかを問い直してみましょう。このセクションでは、ストリームと呼ばれるデータ構造に基づく、状態をモデル化するための別のアプローチを探っていきます。これから見ていくように、ストリームは状態をモデル化する際の複雑さをある程度軽減してくれます。
Let's step back and review where this complexity comes from. In an
attempt to model real-world phenomena, we made some apparently
reasonable decisions: We modeled real-world objects with local state
by computational objects with local variables. We identified time
variation in the real world with time variation in the computer. We
implemented the time variation of the states of the model objects in
the computer with assignments to the local variables of the model
objects.
ここで一歩引いて、この複雑さがどこから来ているのかを振り返ってみましょう。現実世界の現象をモデル化しようとする中で、私たちは一見もっともらしい決断をいくつか下してきました。ローカル状態をもつ現実世界のオブジェクトを、ローカル変数をもつ計算上のオブジェクトでモデル化しました。現実世界における時間変動を、コンピュータ内部での時間変動と同一視しました。そしてモデルオブジェクトの状態がコンピュータ内で時間とともに変化していく様子を、モデルオブジェクトのローカル変数への代入によって実装しました。
Is there another approach? Can we avoid identifying time in the
computer with time in the modeled world? Must we make the model
change with time in order to model phenomena in a changing world?
Think about the issue in terms of mathematical functions. We can
describe the time-varying behavior of a quantity
$x$ as a function of time
$x(t)$.
If we concentrate on $x$ instant by instant,
we think of it as a changing quantity. Yet if we concentrate on the entire
time history of values, we do not emphasize change—the function
itself does not change.
別のアプローチはないのでしょうか。コンピュータ内の時間と、モデル化される世界の時間とを同一視しなくて済む方法はあるのでしょうか。変化する世界の現象をモデル化するために、本当にモデル自体を時間とともに変化させなければならないのでしょうか。この問題を数学的な関数の観点から考えてみましょう。ある量$x$の時間とともに変化する振る舞いは、時間の関数$x(t)$として記述できます。$x$を瞬間ごとに見ていると、それは変化する量であるように思えます。しかし、値の時間的な履歴全体に注目すれば、変化を強調する必要はありません—関数そのものは変化しないのです。
If time is measured in discrete steps, then we can model a time function as
a (possibly infinite) sequence. In this section, we will see how to
model change in terms of sequences that represent the time histories
of the systems being modeled. To accomplish this, we introduce new
data structures called
streams. From an abstract point of view,
a stream is simply a sequence. However, we will find that the
straightforward implementation of streams as lists (as in
section
[2.2.1]) doesn't fully reveal
the power of stream processing. As an alternative, we introduce the
technique of
delayed evaluation, which enables us to represent
very large (even infinite) sequences as streams.
時間を離散的なステップで測るのであれば、時間関数を(場合によっては無限の)シーケンスとしてモデル化できます。このセクションでは、モデル化対象のシステムの時間的履歴を表すシーケンスという形で変化をモデル化する方法を見ていきます。これを実現するために、
ストリームと呼ばれる新しいデータ構造を導入します。抽象的な観点から見れば、ストリームは単なるシーケンスにすぎません。しかし、ストリームを(セクション
[2.2.1]でのように)リストとしてそのまま実装しただけでは、ストリーム処理の力を十分に発揮できないことがわかるでしょう。その代替として、
遅延評価という技法を導入します。これにより、非常に大きな(無限でさえある)シーケンスをストリームとして表現できるようになります。
Stream processing lets us model systems that have state without ever
using assignment or mutable data. This has important implications,
both theoretical and practical, because we can build models that avoid
the drawbacks inherent in introducing assignment. On the other hand,
the stream framework raises difficulties of its own, and the question
of which modeling technique leads to more modular and more easily
maintained systems remains open.
ストリーム処理を使えば、代入やミュータブルなデータを一切使わずに、状態をもつシステムをモデル化できます。これは理論的にも実用的にも重要な意味をもちます。というのも、代入を導入することに伴う欠点を避けたモデルを構築できるからです。その一方で、ストリームという枠組みにはそれ自身の難しさもあり、どちらのモデリング手法がよりモジュール的で保守しやすいシステムにつながるのかという問いには、まだ決着がついていません。