"words and expressions in each unit"quot是什么意思思?

continuum_13868
Our content is added by our users. We aim to remove reported files within 1 working day. Please use this link to notify us:文档分类:
下载后只包含 1 个 DOC 格式的文档,没有任何的图纸或源代码,
下载前请先预览,预览内容跟原文是一样的,在线预览图片经过高度压缩,下载原文更清晰。
您的浏览器不支持进度条
淘豆网网友近日为您收集整理了关于福建省厦门市集美区灌口中学高中英语 unit 5 canada “the true north”词汇教学设计 新人教版必修3的文档,希望对您的工作和学习有所帮助。以下是文档介绍:1Unit 5 Canada The True North一、教学内容分析《普通高中课程标准实验教科书英语(3)》人民教育出版社必修三 Unit 5 中心话题是有关加拿大的旅游经历,我在第二课时对本单元考纲重点词汇(tradition, aboard, measure,broad, impress, scenery, baggage, settle down, rather than, chat with, catch sightof, in the distance, have a gift for, be surrounded by, manage to, be pleased 等)教学的处理上根据教材内容、学生的实际情况、接受能力灵活整合。认知理论认为语言学习是一个信息加工过程,在设计这一课时,通过 reading 的语言输入,Speaking (oralpractice)教学活动的设计对信息进行加工,为后面的 writing 做铺垫,最后介绍旅游经历输出迁移运用所学语言。二、学生学习情况分析学生对旅游这一话题较感兴趣,通过第一节课有关加拿大旅游经历文章的阅读,学生初步了解相关旅游经历的词汇,但学生对大部分词汇的掌握还停留在理解阶段,无法识别一词多性,一词多义,一词多搭配等,未能在口头交际或书面表达中正确,灵活运用所学词汇,所以教师结合词块理论进行词汇教学实践,以学生为主体、以情景为依托、以话题为基础、以运用为目的的词汇教学策略,尝试以“游记”为主线,引导学生在语境中学习词块,运用词块进行交流及表达,争取提高词汇学习的积极性,主动性和有效性。三、设计思想1. 运用建构主义教学的基本模式 5P(Preparation, Presentation, Practice,Production, Progress)笔者在设计本课时,力求发挥学生的主体作用,从学生已有的生活经验出发,以在西方国家旅游经历所见所闻——“快闪”的视频来导入话题,吸引学生注意力,进而以语篇的形式呈现本课考纲词汇教学重点,引导学生在真实的语篇,语境中储存和提取有关旅游经历的词块,并在语境中更准确理解词块。接着,通过 Oral practice 设计各种真实情境,引导学生进行机械操练(Mechanical practice)及有意义操练(Meaningful practice)向学生提供真实生活中解决问题所需的知识成分(如用英语表达旅游经历的词块、句子),培养学生把知识运用于情境中的能力,发展交际策略,最后引导学生围绕所学的词块自主编写有关旅游经历的短文,推动学生将对词块的感知上升为运用,使得所学的词块个性化,提高学生使用词块生成语篇的能力,创造性将所学知识进行迁移运用,输出所学语言,培养综合运用语言的能力。在课堂活动中强调学生的自我评价和相互评价,让学生在自我评价和相互评价中不断反思,学会欣赏,感受进步,体验成功。2. 实施任务型教学,倡导体验参与在课堂教学过程中,设计许多任务型教学活动,创设情境,让学生在教师的指导下,积极感知、体验、实践、参与和合作,实现任务的目标,感受成功,进而形成积极的学习态度,促进语言实际运用能力的提高。四、教学目标Lead in ReadingOralpracticeWriting AssessmentHomeworkActivation municativeactivitiesApplication CheckingExperience Knowledge petence Enrich experiencePreparation Presentation Practice Production Progress2 1. Arise the students’ interest in traveling2. Learn to share their traveling experience ,usingthe important words in chunk in real context我为这课确定了语言技能、语言知识、情感态度、学习策略和文化意识等五项目标:五、教学重点难点1. Help students to master the usages of important words and phrases2. Help students to develop the ability of using vocabulary in real context3. Help students to write an article about traveling experience六、教学设计过程步骤内容活动方式活动间的联系设计意图学情预设I Lead-in( Preparation)Watch the video about my travelingexperiences in Spain视频引出话题以在西方国家旅游所见所闻——“快闪族”视频吸引学生兴趣,自然引入本课话题,引导学生外出旅游关注中西文化的差异,同时激活学生原有的知识经验,帮助学生大脑形成一个有关“旅游经历”的信息包,把它和将要出现的新知识有机结合起来。学生在日常生活中对“快闪族”有一定的了解,对旅游话题也较为感兴趣,会较快进入学习状态1.Read the passage “My travelingexperience in Spain& and try todiscover the new words and phrases in讨论提问( 师生互填补学生间的信息差,为活动的开正如马林诺夫斯基所指出的那样,“没有语Kachro(1962)研究发现,要成功地记住一个Traveling experienceLanguage SkillsLanguage knowledgeCultural awarenessLearning strategiesEmotion and attitudeLanguage in putLearningwordsanduseful expressionsabouttravelingexperienceReading for information1. Ss can master the characteristicsof active words in .real context2. Ss can deal with information bydiscovering important words orphrases in real context3. Ss can develop the ability ofusing vocabulary in real context4. Ss can assess each other andbuild up confidenceKnowledge abouttravelingLanguage out put1. Speaking2. Writing an articleabout travelingexperience3II.1播放器加载中,请稍候...
该用户其他文档
下载所得到的文件列表福建省厦门市集美区灌口中学高中英语 unit 5 canada “the true north”词汇教学设计 新人教版必修3.doc
文档介绍:
1Unit 5 Canada The True North一、教学内容分析《普通高中课程标准实验教科书英语(3)》人民教育出版社必修三 Unit 5 中心话题是有关加拿大的旅游经历,我在第二课时对本单元考纲重点词汇(tradition, aboard, measure,broad, impress, scenery, baggage, settle down, rather than, chat with, catch sightof, in the distance, have a gift for, be surrounded by, manage to, be pleased 等)教学的处理上根据教材内容、学生的实际情况、接受能力灵活整合。认知理论认为语言学习是一个信息加工过程,在设计这一课时,通过 reading 的语言输入,Speaking (oralpractice)教学活动的设计对信息进行加工,为后面的 writing 做铺垫,最后介绍旅游经历输出迁移运用所学语言。二、学生学习情况分析学生对旅游这一话题较感兴趣,通过第一节课有关加拿大旅游经历文章的阅读,学生初步了解相关旅游经历的词汇,但学生对大部分词汇的掌握还停留在理解阶段,无法识别一词多性,一词多义,一词多搭配等,未能在口头交际或书面表达中正确,灵活运用所学词汇,所以教师结合词块理论进行词汇教学实践,以学生为主体、以情景为依托、以话题为基础、以运用为目的的词汇教学策略,尝试以“游记”为主线,引导学生在...
内容来自淘豆网转载请标明出处.functional programming - Monad in plain English? (For the OOP programmer with no FP background) - Stack Overflow
to customize your list.
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
J it only takes a minute:
Join the Stack Overflow community to:
Ask programming questions
Answer and help your peers
Get recognized for your expertise
In terms that an OOP programmer would understand (without any functional programming background), what is a monad?
What problem does it solve and what are the most common places it's used?
To clarify the kind of understanding I was looking for, let's say you were converting an FP application that had monads into an OOP application. What would you do to port the responsibilities of the monads to the OOP app?
7,13243657
UPDATE: This question was the subject of an immensely long blog series, which you can read at
-- thanks for the great question!
In terms that an OOP programmer would understand (without any functional programming background), what is a monad?
A monad is an "amplifier" of types that obeys certain rules and which has certain operations provided.
First, what is an "amplifier of types"?
By that I mean some system which lets you take a type and turn it into a more special type. For example, in C# consider Nullable&T&. This is an amplifier of types. It lets you take a type, say int, and add a new capability to that type, namely, that now it can be null when it couldn't before.
As a second example, consider IEnumerable&T&. It is an amplifier of types. It lets you take a type, say, string, and add a new capability to that type, namely, that you can now make a sequence of strings out of any number of single strings.
What are the "certain rules"? Briefly, that there is a sensible way for functions on the underlying type to work on the amplified type such that they follow the normal rules of functional composition. For example, if you have a function on integers, say
int M(int x) { return x + N(x * 2); }
then the corresponding function on Nullable&int& can make all the operators and calls in there work together "in the same way" that they did before.
(That is incredibly you asked for an explanation that didn't assume anything about knowledge of functional composition.)
What are the "operations"?
That there is a way to take a value of an unamplified type and turn it into a value of the amplified type.
That there is a way to transform operations on the unamplified type into operations on the amplified type that obeys the rules of functional composition mentioned before
That there is usually a way to get the unamplified type back out of the amplified type. (This last point isn't strictly necessary for a monad but it is frequently the case that such an operation exists.)
Again, take Nullable&T& as an example. You can turn an int into a Nullable&int& with the constructor. The C# compiler takes care of most nullable "lifting" for you, but if it didn't, the lifting transformation is straightforward: an operation, say,
int M(int x) { whatever }
is transformed into
Nullable&int& M(Nullable&int& x)
if (x == null)
return new Nullable&int&(whatever);
And turning a nullable int back into an int is done with the Value property.
It's the function transformation that is the key bit. Notice how the actual semantics of the nullable operation -- that an operation on a null propagates the null -- is captured in the transformation. We can generalize this. Suppose you have a function from int to int, like our original M.
You can easily make that into a function that takes an int and returns a Nullable&int& because you can just run the result through the nullable constructor. Now suppose you have this higher-order method:
Nullable&T& Bind&T&(Nullable&T& amplified, Func&T, Nullable&T&& func)
if (amplified == null)
return func(amplified.Value);
See what you can do with that? Any method that takes an int and returns an int, or takes an int and returns a nullable int can now have the nullable semantics applied to it.
Furthermore: suppose you have two methods
Nullable&int& X(int q) { ... }
Nullable&int& Y(int r) { ... }
and you want to compose them:
Nullable&int& Z(int s) { return X(Y(s)); }
That is, Z is the composition of X and Y. But you cannot do that because X takes an int, and Y returns a nullable int. But since you have the "bind" operation, you can make this work:
Nullable&int& Z(int s) { return Bind(Y(s), X); }
The bind operation on a monad is what makes composition of functions on amplified types work. The "rules" I handwaved about above are that the monad preserves the rules of normal
that composing with identity functions results in the original function, that composition is associative, and so on.
In C#, "Bind" is called "SelectMany". Take a look at how it works on the sequence monad. We need to have three things: turn a value into a sequence, turn a sequence into a value, and bind operations on sequences. Those operations are:
IEnumerable&T& MakeSequence&T&(T item)
T Single&T&(IEnumerable&T& sequence)
// let's just take the first one
foreach(T item in sequence)
IEnumerable&T& SelectMany&T&(IEnumerable&T& seq, Func&T, IEnumerable&T&& func)
foreach(T item in seq)
foreach(T result in func(item))
The nullable monad rule was "to combine two functions that produce nullables together, check to see if the inner if it does, produce null, if it does not, then call the outer one with the result". That's the desired semantics of nullable.
The sequence monad rule is "to combine two functions that produce sequences together, apply the outer function to every element produced by the inner function, and then concatenate all the resulting sequences together".
The fundamental semantics of the monads are captured in the Bind/SelectM this is the method that tells you what the monad really means.
We can do even better. Suppose you have a sequences of ints, and a method that takes ints and results in sequences of strings. We could generalize the binding operation to allow composition of functions that take and return different amplified types, so long as the inputs of one match the outputs of the other:
IEnumerable&U& SelectMany&T,U&(IEnumerable&T& seq, Func&T, IEnumerable&U&& func)
foreach(T item in seq)
foreach(U result in func(item))
So now we can say "amplify this bunch of individual integers into a sequence of integers. Transform this particular integer into a bunch of strings, amplified to a sequence of strings. Now put both operations together: amplify this bunch of integers into the concatenation of all the sequences of strings." Monads allow you to compose your amplifications.
What problem does it solve and what are the most common places it's used?
That's rather like asking "what problems does the singleton pattern solve?", but I'll give it a shot.
Monads are typically used to solve problems like:
I need to make new capabilities for this type and still combine old functions on this type to use the new capabilities.
I need to capture a bunch of operations on types and represent those operations as composable objects, building up larger and larger compositions until I have just the right series of operations represented, and then I need to start getting results out of the thing
I need to represent side-effecting operations cleanly in a language that hates side effects
(Note that these are basically three ways of saying the same thing.)
C# uses monads in its design. As already mentioned, the nullable pattern is highly akin to the "maybe monad". LINQ is entirely the "SelectMany" method is what does the semantic work of composition of operations. (Erik Meijer is fond of pointing out that every LINQ function could actually be implemented by SelectM everything else is just a convenience.)
To clarify the kind of understanding I was looking for, let's say you were converting an FP application that had monads into an OOP application. What would you do to port the responsibilities of the monads into the OOP app?
Most OOP languages do not have a rich enough type system to represent the monad pat you need a type system that supports types that are higher types than generic types. So I wouldn't try to do that. Rather, I would implement generic types that represent each monad, and implement methods that represent the three operations you need: turning a value into an amplified value, turning an amplified value into a value, and transforming a function on unamplified values into a function on amplified values.
A good place to start is how we implemented LINQ in C#. Study the SelectM it is the key to understanding how the sequence monad works in C#. It is a very simple method, but very powerful!
For a more in-depth and theoretically sound explanation of monads in C#, I highly recommend my colleague Wes Dyer's article on the subject. This article is what explained monads to me when they finally "clicked" for me.
In terms that an OOP programmer would
understand (without any functional
programming background), what is a
What problem does it solve and what
are the most common places it's used?are the most common places it's used?
In terms of OO programming, a monad is an interface (or more likely a mixin), parameterized by a type, with two methods, return and bind that describe:
How to inject a value to get a
monadic value of that injected value
How to use a function that
makes a monadic value from a
non-monadic one, on a monadic value.
The problem it solves is the same type of problem you'd expect from any interface, namely,
"I have a bunch of different classes that do different things, but seem to do those different things in a way that has an underlying similarity. How can I describe that similarity between them, even if the classes themselves aren't really subtypes of anything closer than 'the Object' class itself?"
More specifically, the Monad "interface" is similar to IEnumerator or IIterator in that it takes a type that itself takes a type. The main "point" of Monad though is being able to connect operations based on the interior type, even to the point of having a new "internal type", while keeping - or even enhancing - the information structure of the main class.
4,49532041
I would say the closest OO analogy to monads is the "".
In the command pattern you wrap an ordinary statement or expression in a command object. The command object expose an execute method which executes the wrapped statement. So statement are turned into first class objects which can passed around and executed at will. Commands can be composed so you can create a program-object by chaining and nesting command-objects.
The commands are executed by a separate object, the invoker. The benefit of using the command pattern (rather than just execute a series of ordinary statements) is that different invokers can apply different logic to how the commands should be executed.
The command pattern could be used to add (or remove) language features which is not supported by the host language. For example, in a hypothetical OO language without exceptions, you could add exception semantics by exposing "try" and "throw" methods to the commands. When a command calls throw, the invoker backtracks through the list (or tree) of commands until the last "try" call. Conversely, you could remove exception semantic from a language (if you believe ) by catching all exceptions thrown by each individual commands, and turning them into error codes which are then passed to the next command.
Even more fancy execution semantics like transactions, non-deterministic execution or continuations can be implemented like this in a language which doesn't support it natively. It is a pretty powerful pattern if you think about it.
Now in reality the command-patterns is not used as a general language feature like this. The overhead of turning each statement into a separate class would lead to an unbearable amount of boilerplate code. But in principle it can be used to solve the same problems as monads are used to solve in fp.
27.8k84666
Why do we need monads?
We want to program only using functions. ("functional programming" after all -FP).
Then, we have a first big problem. This is a program:
f(x) = 2 * x
g(x,y) = x / y
How can we say
what is to be executed first? How can we form an ordered sequence of functions (i.e. a program) using no more than functions?
Solution: compose functions. If you want first g and then f, just write f(g(x,y)). OK, but ...
More problems: some functions might fail (i.e. g(2,0), divide by 0). We have no "exceptions" in FP. How do we solve it?
Solution: Let's allow functions to return two kind of things: instead of having g : Real,Real -& Real (function from two reals into a real), let's allow g : Real,Real -& Real | Nothing (function from two reals into (real or nothing)).
But functions should (to be simpler) return only one thing.
Solution: let's create a new type of data to be returned, a "boxing type" that encloses maybe a real or be simply nothing. Hence, we can have g : Real,Real -& Maybe Real. OK, but ...
What happens now to f(g(x,y))? f is not ready to consume a Maybe Real. And, we don't want to change every function we could connect with g to consume a Maybe Real.
Solution: let's have a special function to "connect"/"compose"/"link" functions. That way, we can, behind the scenes, adapt the output of one function to feed the following one.
In our case:
g &&= f (connect/compose g to f). We want &&= to get g's output, inspect it and, in case it is Nothing just don't call f and return Nothing; or on the contrary, extract the boxed Real and feed f with it. (This algorithm is just the implementation of &&= for the Maybe type).
Many other problems arise which can be solved using this same pattern: 1. Use a "box" to codify/store different meanings/values, and have functions like g that return those "boxed values". 2. Have composers/linkers g &&= f to help connecting g's output to f's input, so we don't have to change f at all.
Remarkable problems that can be solved using this technique are:
having a global state that every function in the sequence of functions ("the program") can share: solution StateMonad.
We don't like "impure functions": functions that yield different output for same input. Therefore, let's mark those functions, making them to return a tagged/boxed value: IO monad.
Total happiness !!!!
11.3k31837
7,34983971
You have a recent presentation "" by Christopher League (July 12th, 2010), which is quite interesting on topics of continuation and monad.
The video going with this (slideshare) presentation is actually .
The Monad part start around 37 minutes in, on this one hour video, and starts with slide 42 of its 58 slide presentation.
It is presented as "the leading design pattern for functional programming", but the language used in the examples is Scala, which is both OOP and functional.
You can read more on Monad in Scala in the blog post "", from
(March 27, 2008).
A type constructor M is a monad if it supports these operations:
# the return function
def unit[A] (x: A): M[A]
# called "bind" in Haskell
def flatMap[A,B] (m: M[A]) (f: A =& M[B]): M[B]
# Other two can be written in term of the first two:
def map[A,B] (m: M[A]) (f: A =& B): M[B] =
flatMap(m){ x =& unit(f(x)) }
def andThen[A,B] (ma: M[A]) (mb: M[B]): M[B] =
flatMap(ma){ x =& mb }
So for instance (in Scala):
Option is a monad
def unit[A] (x: A): Option[A] = Some(x)
def flatMap[A,B](m:Option[A])(f:A =>Option[B]): Option[B] =
case None => None
case Some(x) => f(x)
List is Monad
def unit[A] (x: A): List[A] = List(x)
def flatMap[A,B](m:List[A])(f:A =>List[B]): List[B] =
case Nil => Nil
case x::xs => f(x) ::: flatMap(xs)(f)
Monad are a big deal in Scala because of convenient syntax built to take advantage of Monad structures:
for comprehension in Scala:
i &- 1 to 4
j &- 1 to i
k &- 1 to j
} yield i*j*k
is translated by the compiler to:
(1 to 4).flatMap { i =&
(1 to i).flatMap { j =&
(1 to j).map { k =&
The key abstraction is the flatMap, which binds the computation through chaining.
Each invocation of flatMap returns the same data structure type (but of different value), that serves as the input to the next command in chain.
In the above snippet, flatMap takes as input a closure (SomeType) =& List[AnotherType] and returns a List[AnotherType]. The important point to note is that all flatMaps take the same closure type as input and return the same type as output.
This is what "binds" the computation thread - every item of the sequence in the for-comprehension has to honor this same type constraint.
If you take two operations (that may fail) and pass the result to the third, like:
lookupVenue: String =& Option[Venue]
getLoggedInUser: SessionID =& Option[User]
reserveTable: (Venue, User) =& Option[ConfNo]
but without taking advantage of Monad, you get convoluted OOP-code like:
val user = getLoggedInUser(session)
val confirm =
if(!user.isDefined) None
else lookupVenue(name) match {
case None =& None
case Some(venue) =&
val confno = reserveTable(venue, user.get)
if(confno.isDefined)
mailTo(confno.get, user.get)
whereas with Monad, you can work with the actual types (Venue, User) like all the operations work, and keep the Option verification stuff hidden, all because of the flatmaps of the for syntax:
val confirm = for {
venue &- lookupVenue(name)
user &- getLoggedInUser(session)
confno &- reserveTable(venue, user)
mailTo(confno, user)
The yield part will only be executed if all three functions have Some[X]; any None would directly be returned to confirm.
Monads allow ordered computation within Functional Programing, that allows us to model sequencing of actions in a nice structured form, somewhat like a DSL.
And the greatest power comes with the ability to compose monads that serve different purposes, into extensible abstractions within an application.
This sequencing and threading of actions by a monad is done by the language compiler that does the transformation through the magic of closures.
By the way, Monad is not only model of computation used in FP: see this .
Category theory proposes many models of computation. Among them
the Arrow model of computations
the Monad model of computations
the Applicative model of computations
30.8k1273107
604k18216981782
In functional programming, a monad
a kind of abstract data type used to
represent computations
(instead of
data in the domain model). Monads
allow the programmer to chain actions
together to build a pipeline, in which
each action is decorated with
additional processing rules provided
by the monad. Programs written in
functional style can make use of
monads to structure procedures that
include sequenced operations,[2]
or to define arbitrary control flows
(like handling concurrency,
continuations, or exceptions).
Formally, a monad is constructed by
defining two operations (bind and
return) and a type constructor M that
must fulfill several properties to
allow the correct composition of
monadic functions (i.e. functions that
use values from the monad as their
arguments). The return operation takes
a value from a plain type and puts it
into a monadic container of type M.
The bind operation performs the
reverse process, extracting the
original value from the container and
passing it to the associated next
function in the pipeline.
A programmer will compose monadic
functions to define a data-processing
pipeline. The monad acts as a
framework, as it's a reusable behavior
that decides the order in which the
specific monadic functions in the
pipeline are called, and manages all
the undercover work required by the
computation.[3] The bind and return
operators interleaved in the pipeline
will be executed after each monadic
function returns control, and will
take care of the particular aspects
handled by the monad.
I believe it explains it very well.
7,1861181152
A monad is a data type that encapsulates a value and to which essentially two operations can be applied:
return x creates a value of the monad type that encapsulates x
m &&= f (read it as "the bind operator") applies the function f to the value in the monad m
That's what a monad is. There are , but basically those two operations define a monad. The real question is what a monad does, and that depends on the monad — lists are monads, Maybes are monads, IO operations are monads. All that it means when we say those things are monads is that they have the monad interface of return and &&=.
171k19226336
I've written a short article comparing standard OOP python code to monadic python code demonstrating the underlying computational process with diagrams. It assumes no previous knowledge of FP. Hope you find it useful -
I'll try to make the shortest definition I can manage using OOP terms:
A generic class CMonadic&T& is a monad if it defines at least the following methods:
class CMonadic&T& {
static CMonadic&T& create(T t);
// a.k.a., "return" in Haskell
public CMonadic&U& flatMap&U&(Func&T, CMonadic&U&& f); // a.k.a. "bind" in Haskell
and if the following laws apply for all types T and their possible values t
left identity:
CMonadic&T&.create(t).flatMap(f) == f(t)
right identity
instance.flatMap(CMonadic&T&.create) == instance
associativity:
instance.flatMap(f).flatMap(g) == instance.flatMap(t =& f(t).flatMap(g))
A List monad may have:
List&int&.create(1) --& [1]
And flatMap on the list [1,2,3] could work like so:
intList.flatMap(x =& List&int&.makeFromTwoItems(x, x*10)) --& [1,10,2,20,3,30]
Iterables and Observables can also be made monadic, as well as Promises and Tasks.
Commentary:
Monads are not that complicated. The flatMap function is a lot like the more commonly encountered map. It receives a function argument (also known as delegate), which it may call (immediately or later, zero or more times) with a value coming from the generic class. It expects that passed function to also wrap its return value in the same kind of generic class. To help with that, it provides create, a constructor that can create an instance of that generic class from a value. The return result of flatMap is also a generic class of the same type, often packing the same values that were contained in the return results of one or more applications of flatMap to the previously contained values. This allows you to chain flatMap as much as you want:
intList.flatMap(x =& List&int&.makeFromTwo(x, x*10))
.flatMap(x =& x % 3 == 0
? List&string&.create("x = " + x.toString())
: List&string&.empty())
It just so happens that this kind of generic class is useful as a base model for a huge number of things. This (together with the category theory jargonisms) is the reason why Monads seem so hard to understand or explain. They're a very abstract thing and only become obviously useful once they're specialized.
For example, you can model exceptions using monadic containers. Each container will either contain the result of the operation or the error that has occured. The next function (delegate) in the chain of flatMap callbacks will only be called if the previous one packed a value in the container. Otherwise if an error was packed, the error will continue to propagate through the chained containers until a container is found that has an error handler function attached via a method called .orElse() (such a method would be an allowed extension)
Notes: Functional languages allow you to write functions that can operate on any kind of a monadic generic class. For this to work, one would have to write a generic interface for monads. I don't know if its possible to write such an interface in C#, but as far as I know it isn't:
interface IMonad&T& {
static IMonad&T& create(T t); // not allowed
public IMonad&U& flatMap&U&(Func&T, IMonad&U&& f); // not specific enough,
// because the function must return the same kind of monad, not just any monad
A monad is an array of functions
(Pst: an array of functions is just a computation).
Actually, instead of a true array (one function in one cell array) you have those functions chained by another function >>=. The >>= allows to adapt the results from function i to feed function i+1, perform calculations between them
or, even, not to call function i+1.
The types used here are "types with context". This is, a value with a "tag".
The functions being chained must take a "naked value" and return a tagged result.
One of the duties of >>= is to extract a naked value out of its context.
There is also the function "return", that takes a naked value and puts it with a tag.
An example with Maybe. Let's use it to store a simple integer on which make calculations.
multiply :: Int -& Int -& Maybe Int
multiply a b = return
-- divideBy 5 100 = 100 / 5
divideBy :: Int -& Int -& Maybe Int
divideBy 0 _ = Nothing -- dividing by 0 gives NOTHING
divideBy denom num = return (quot num denom) -- quotient of num / denom
-- tagged value
val1 = Just 160
-- array of functions feeded with val1
array1 = val1 &&= divideBy 2
&&= multiply 3 &&= divideBy
4 &&= multiply 3
-- array of funcionts created with the do notation
-- equals array1 but for the feeded val1
array2 :: Int -& Maybe Int
array2 n = do
v &- divideBy 2
v &- multiply 3 v
v &- divideBy 4 v
v &- multiply 3 v
-- array of functions,
-- the first &&= performs 160 / 0, returning Nothing
-- the second &&= has to perform Nothing &&= multiply 3 ....
-- and simply returns Nothing without calling multiply 3 ....
array3 = val1 &&= divideBy 0
&&= multiply 3 &&= divideBy
4 &&= multiply 3
print array1
print (array2 160)
print array3
Just to show that monads are array of functions with helper operations, consider
the equivalent to the above example, just using a real array of functions
type MyMonad = [Int -& Maybe Int] -- my monad as a real array of functions
myArray1 = [divideBy 2, multiply 3, divideBy 4, multiply 3]
-- function for the machinery of executing each function i with the result provided by function i-1
runMyMonad :: Maybe Int -& MyMonad -& Maybe Int
runMyMonad val [] = val
runMyMonad Nothing _ = Nothing
runMyMonad (Just val) (f:fs) = runMyMonad (f val) fs
And it would be used like this:
print (runMyMonad (Just 160) myArray1)
7,34983971
To respect fast readers, I start with precise definition first,
continue with quick more "plain English" explanation, and then move to examples.
slightly reworded:
A monad (in computer science) is formally a map that:
sends every type X of some given programming language to a new type T(X) (called the "type of T-computations with values in X");
equipped with a rule for composing two functions of the form
f:X-&T(Y) and g:Y-&T(Z) to a function g?f:X-&T(Z);
in a way that is associative in the evident sense and unital with respect to a given unit function called pure_X:X-&T(X), to be thought of as taking a value to the pure computation that simply returns that value.
So in simple words, a monad is a rule to pass from any type X to another type T(X), and a rule to pass from two functions f:X-&T(Y) and g:Y-&T(Z) (that you would like to compose but can't) to a new function h:X-&T(Z). Which, however, is not the composition in strict mathematical sense. We are basically "bending" function's composition or re-defining how functions are composed.
Plus, we require the monad's rule of composing to satisfy the "obvious" mathematical axioms:
Associativity: Composing f with g and then with h (from outside) should be the same as composing g with h and then with f (from inside).
Unital property: Composing f with the identity function on either side should yield f.
Again, in simple words, we can't just go crazy re-defining our function composition as we like:
We first need the associativity to be able to compose several functions in a row e.g. f(g(h(k(x))), and not to worry about specifying the order composing function pairs. As the monad rule only prescribes how to compose a pair of functions, without that axiom, we would need to know which pair is composed first and so on. (Note that is different from the commutativity property that f composed with g were the same as g composed with f, which is not required).
And second, we need the unital property, which is simply to say that identities compose trivially the way we expect them. So we can safely refactor functions whenever those identities can be extracted.
So again in brief: A monad is the rule of type extension and composing functions satisfying the two axioms -- associativity and unital property.
In practical terms, you want the monad to be implemented for you by the language, compiler or framework that would take care of composing functions for you. So you can focus on writing your function's logic rather than worrying how their execution is implemented.
That is essentially it, in a nutshell.
Being professional mathematician, I prefer to avoid calling h the "composition" of f and g. Because mathematically, it isn't. Calling it the "composition" incorrectly presumes that h is the true mathematical composition, which it isn't. It is not even uniquely determined by f and g. Instead, it is the result of our monad's new "rule of
composing" the functions. Which can be totally different from the actual mathematical composition even if the latter exists!
Monad is not a functor! A functor is a rule to go from types to types and functions between them to other functions (sending objects to objects and their morphisms to morphisms in category theory). Instead a monad sends a pair of functions f and g to a new one h.
To make it less dry, let me try to illustrate it by example
that I am annotating with small sections, so you can skip right to the point.
Exception throwing as Monad examples
Suppose we want to compose two functions:
f: x -& 1 / x
g: y -& 2 * y
But f(0) is not defined, so an exception e is thrown. Then how can you define the compositional value g(f(0))? Throw an exception again, of course! Maybe the same e. Maybe a new updated exception e1.
What precisely happens here? First, we need new exception value(s)
(different or same). You can call them nothing or null or whatever but the essence remains the same -- they should be new values, e.g. it should not be a number in our example here. I prefer not to call them null to avoid confusion with how null can be implemented in any specific language. Equally I prefer to avoid nothing because it is often associated with null, which, in principle, is what null should do, however, that principle often gets bended for whatever practical reasons.
What is exception exactly?
This is a trivial matter for any experienced programmer but I'd like to drop few words just to extinguish any worm of confusion:
Exception is an object encapsulating information about how the invalid result of execution occurred.
This can range from throwing away any details and returning a single global value (like NaN or null) or generating a long log list or what exactly happened, send it to a database and replicating all over the distributed)
The important difference between these two extreme examples of exception is that in the first case there are no side-effects. In the second there are. Which brings us to the (thousand-dollar) question:
Are exceptions allowed in pure functions?
Shorter answer: Yes, but only when they don't lead to side-effects.
Longer answer. To be pure, your function's output must be uniquely determined by its input. So we amend our function f by sending 0 to the new abstract value e that we call exception. We make sure that value e contains no outside information that is not uniquely determined by our input, which is x. So here is an example of exception without side-effect:
type: error,
message: 'I got error trying to divide 1 by 0'
And here is one with side-effect:
type: error,
message: 'Our committee to decide what is 1/0 is currently away'
Actually, it only has side-effects if that message can possibly change in the future. But if it is guaranteed to never change, that value becomes uniquely predictable, and so there is no side-effect.
To make it even sillier. A function returning 42 ever is clearly pure. But if someone crazy decides to make 42 a variable that value might change, the very same function stops being pure under the new conditions.
Note that I am using the object literal notation for simplicity to demonstrate the essence. Unfortunately things are messed-up in languages like JavaScript, where error is not a type that behaves the way we want here with respect to function composition, whereas actual types like null or NaN do not behave this way but rather go through the some artificial and not always intuitive type conversions.
Type extension
As we want to vary the message inside our exception, we are really declaring a new type E for the whole exception object and then
That is what the maybe number does, apart from its confusing name, which is to be either of type number or of the new exception type E, so it is really the union number | E of number and E. In particular, it depends on how we want to construct E, which is neither suggested nor reflected in the name maybe number.
What is functional composition?
It is the mathematical operation taking functions
f: X -& Y and g: Y -& Z and constructing
their composition as function h: X -& Z satisfying h(x) = g(f(x)).
The problem with this definition occurs when the result f(x) is not allowed as argument of g.
In mathematics those functions cannot be composed without extra work.
The strictly mathematical solution for our above example of f and g is to remove 0 from the set of definition of f. With that new set of definition (new more restrictive type of x), f becomes composable with g.
However, it is not very practical in programming to restrict the set of definition of f like that. Instead, exceptions can be used.
Or as another approach, artificial values are created like NaN, undefined, null, Infinity etc. So you evaluate 1/0 to Infinity and 1/-0 to -Infinity. And then force the new value back into your expression instead of throwing exception. Leading to results you may or may not find predictable:
// =& Infinity
parseInt(Infinity) // =& NaN
// =& false
And we are back to regular numbe)
JavaScript allows us to keep executing numerical expressions at any costs without throwing errors as in the above example. That means, it also allows to compose functions. Which is exactly what monad is about - it is a rule to compose functions satisfying the axioms as defined at the beginning of this answer.
But is the rule of composing function, arising from JavaScript's implementation for dealing with numerical errors, a monad?
To answer this question, all you need is to check the axioms (left as exercise as not part ).
Can throwing exception be used to construct a monad?
Indeed, a more useful monad would instead be the rule prescribing
that if f throws exception for some x, so does its composition with any g. Plus make the exception E globally unique with only one possible value ever ( in category theory). Now the two axioms are instantly checkable and we get a very useful monad. And the result is what is well-known as the .
3,89632350
Whether a monad has a "natural" interpretation in OO depends on the monad.
In a language like Java, you can translate the maybe monad to the language of checking for null pointers, so that computations that fail (i.e., produce Nothing in Haskell) emit null pointers as results.
You can translate the state monad into the language generated by creating a mutable variable and methods to change its state.
A monad is a monoid in the category of endofunctors.
The information that sentence puts together is very deep.
And you work in a monad with any imperative language.
A monad is a "sequenced" domain specific language.
It satisfies certain interesting properties, which taken together make a monad a mathematical model of "imperative programming".
Haskell makes it easy to define small (or large) imperative languages, which can be combined in a variety of ways.
As an OO programmer, you use your language's class hierarchy to organize the kinds of functions or procedures that can be called in a context, what you call an object.
A monad is also an abstraction on this idea, insofar as different monads can be combined in arbitrary ways, effectively "importing" all of the sub-monad's methods into the scope.
Architecturally, one then uses type signatures to explicitly express which contexts may be used for computing a value.
One can use monad transformers for this purpose, and there is a high quality collection of all of the "standard" monads:
(non-deterministic computations, by treating a list as a domain)
(computations that can fail, but for which reporting is unimportant)
(computations that can fail and require exception handling
Reader (computations that can be represented by compositions of plain Haskell functions)
Writer (computations with sequential "rendering"/"logging" (to strings, html etc)
(continuations)
(computations that depend on the underlying computer system)
(computations whose context contains a modifiable value)
with corresponding monad transformers and type classes.
Type classes allow a complementary approach to combining monads by unifying their interfaces, so that concrete monads can implement a standard interface for the monad "kind".
For example, the module Control.Monad.State contains a class MonadState s m, and (State s) is an instance of the form
instance MonadState s (State s) where
The long story is that a monad is a functor which attaches "context" to a value, which has a way to inject a value into the monad, and which has a way to evaluate values with respect to the context attached to it, at least in a restricted way.
return :: a -& m a
is a function which injects a value of type a into a monad "action" of type m a.
(&&=) :: m a -& (a -& m b) -& m b
is a function which takes a monad action, evaluates its result, and applies a function to the result.
The neat thing about (>>=) is that the result is in the same monad.
In other words, in m >>= f, (>>=) pulls the result out of m, and binds it to f, so that the result is in the monad.
(Alternatively, we can say that (>>=) pulls f into m and applies it to the result.)
As a consequence, if we have f :: a -> m b, and g :: b -> m c, we can "sequence" actions:
m &&= f &&= g
Or, using "do notation"
The type for (>>) might be illuminating.
(&&) :: m a -& m b -& m b
It corresponds to the (;) operator in procedural languages like C.
It allows do notation like:
m = do x &- someQuery
someAction x
theNextAction
In mathematical and philosopical logic, we have frames and models, which are "naturally" modelled with monadism.
An interpretation is a function which looks into the model's domain and computes the truth value (or generalizations) of a proposition (or formula, under generalizations).
In a modal logic for necessity, we might say that a proposition is necessary if it is true in "every possible world" -- if it is true with respect to every admissible domain.
This means that a model in a language for a proposition can be reified as a model whose domain consists of collection of distinct models (one corresponding to each possible world).
Every monad has a method named "join" which flattens layers, which implies that every monad action whose result is a monad action can be embedded in the monad.
join :: m (m a) -& m a
More importantly, it means that the monad is closed under the "layer stacking" operation.
This is how monad transformers work:
they combine monads by providing "join-like" methods for types like
newtype MaybeT m a = MaybeT { runMaybeT :: m (Maybe a) }
so that we can transform an action in (MaybeT m) into an action in m, effectively collapsing layers.
In this case, runMaybeT :: MaybeT m a -> m (Maybe a) is our join-like method.
(MaybeT m) is a monad, and MaybeT :: m (Maybe a) -> MaybeT m a is effectively a constructor for a new type of monad action in m.
A free monad for a functor is the monad generated by stacking f, with the implication that every sequence of constructors for f is an element of the free monad (or, more exactly, something with the same shape as the tree of sequences of constructors for f).
Free monads are a useful technique for constructing flexible monads with a minimal amount of boiler-plate.
In a Haskell program, I might use free monads to define simple monads for "high level system programming" to help maintain type safety (I'm just using types and their declarations.
Implementations are straight-forward with the use of combinators):
data RandomF r a = GetRandom (r -& a) deriving Functor
type Random r a = Free (RandomF r) a
type RandomT m a = Random (m a) (m a) -- model randomness in a monad by computing random monad elements.
:: Random r r
runRandomIO
:: Random r a -& IO a (use some kind of IO-based backend to run)
runRandomIO'
:: Random r a -& IO a (use some other kind of IO-based backend)
runRandomList :: Random r a -& [a]
(some kind of list-based backend (for pseudo-randoms))
Monadism is the underlying architecture for what you might call the "interpreter" or "command" pattern, abstracted to its clearest form, since every monadic computation must be "run", at least trivially. (The runtime system runs the IO monad for us, and is the entry point to any Haskell program.
IO "drives" the rest of the computations, by running IO actions in order).
The type for join is also where we get the statement that a monad is a monoid in the category of endofunctors.
Join is typically more important for theoretical purposes, in virtue of its type.
But understanding the type means understanding monads.
Join and monad transformer's join-like types are effectively compositions of endofunctors, in the sense of function composition.
To put it in a Haskell-like pseudo-language,
Foo :: m (m a) &-> (m . m) a
1,70211022
Monads in typical usage are the functional equivalent of procedural programming's exception handling mechanisms.
In modern procedural languages, you put an exception handler around a sequence of statements, any of which may throw an exception. If any of the statements throws an exception, normal execution of the sequence of statements halts and transfers to an exception handler.
Functional programming languages, however, philosophically avoid exception handling features due to the "goto" like nature of them. The functional programming perspective is that functions should not have "side-effects" like exceptions that disrupt program flow.
In reality, side-effects cannot be ruled out in the real world due primarily to I/O. Monads in functional programming are used to handle this by taking a set of chained function calls (any of which might produce an unexpected result) and turning any unexpected result into encapsulated data that can still flow safely through the remaining function calls.
The flow of control is preserved but the unexpected event is safely encapsulated and handled.
From a practical point of view (summarizing what has been said in many previous answers and related articles), it seems to me that one of the fundamental "purposes" (or usefulness) of the monad is to leverage the dependencies implicit in recursive method invocations aka function composition (i.e. when f1 calls f2 calls f3, f3 needs to be evaluated before f2 before f1) to represent sequential composition in a natural way, especially in the context of a lazy evaluation model (that is, sequential composition as a plain sequence, e.g. "f3(); f2(); f1();" in C - the trick is especially obvious if you think of a case where f3, f2 and f1 actually return nothing [their chaining as f1(f2(f3)) is artificial, purely intended to create sequence]).
This is especially relevant when side-effects are involved, i.e. when some state is altered (if f1, f2, f3 had no side-effects, it wouldn't matter in what order they' which is a great property of pure functional languages, to be able to parallelize those computations for example). The more pure functions, the better.
I think from that narrow point of view, monads could be seen as syntactic sugar for languages that favor lazy evaluation (that evaluate things only when absolutely necessary, following an order that does not rely on the presentation of the code), and that have no other means of representing sequential composition. The net result is that sections of code that are "impure" (i.e. that do have side-effects) can be presented naturally, in an imperative manner, yet are cleanly separated from pure functions (with no side-effects), which can be evaluated lazily.
This is only one aspect though, as warned .
to "What is a monad?"
It begins with a motivating example, works through the example, derives an example of a monad, and formally defines "monad".
It assumes no knowledge of functional programming and it uses pseudocode with function(argument) := expression syntax with the simplest possible expressions.
This C++ program is an implementation of the pseudocode monad. (For reference: M is the type constructor, feed is the "bind" operation, and wrap is the "return" operation.)
#include &iostream&
#include &string&
template &class A& class M
template &class A, class B&
M&B& feed(M&B& (*f)(A), M&A& x)
M&B& m = f(x.val);
m.messages = x.messages + m.
template &class A&
M&A& wrap(A x)
m.messages = "";
class T {};
class U {};
class V {};
M&U& g(V x)
m.messages = "called g.\n";
M&T& f(U x)
m.messages = "called f.\n";
int main()
M&T& m = feed(f, feed(g, wrap(x)));
std::cout && m.
If you've ever used Powershell, the patterns Eric described should sound familiar. functional composition is represented by .
goes into more detail.
16.7k24676
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
The week's top questions and answers
Important community announcements
Questions that need answers
By subscribing, you agree to the
Stack Overflow works best with JavaScript enabled}

我要回帖

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信