OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and...

99
CIS 500 Software Foundations Fall 2005 Programming with OCaml CIS 500, Programming with OCaml 1

Transcript of OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and...

Page 1: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

CIS500

SoftwareFoundations

Fall2005

ProgrammingwithOCaml

CIS500,ProgrammingwithOCaml1

Page 2: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

FunctionalprogrammingwithOCaml

CIS500,ProgrammingwithOCaml2

Page 3: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

OCamlandthiscourse

Thematerialinthiscourseismostlyconceptualandmathematical.However,

experimentingwithsmallimplementationsisanexcellentwaytodeepen

intuitionsaboutmanyoftheconceptswewillencounter.Forthispurpose,we

willusetheOCamllanguage.

OCamlisalargeandpowerfullanguage.Forourpresentpurposes,though,we

canconcentratejustonthe“core”ofthelanguage,ignoringmostofits

features.

CIS500,ProgrammingwithOCaml3

Page 4: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

FunctionalProgramming

OCamlisafunctionalprogramminglanguage—i.e.,alanguageinwhichthe

functionalprogrammingstyleisthedominantidiom.Otherwell-known

functionallanguagesincludeLisp,Scheme,Haskell,andStandardML.

Thefunctionalstylecanbedescribedasacombinationof...

�persistentdatastructures(which,oncebuilt,areneverchanged)

�recursionasaprimarycontrolstructure

�heavyuseofhigher-orderfunctions(functionsthattakefunctionsas

argumentsand/orreturnfunctionsasresults)

Imperativelanguages,bycontrast,emphasize

�mutabledatastructures

�loopingratherthanrecursion

�first-orderratherthanhigher-orderprogramming(thoughmany

object-oriented“designpatterns”involvehigher-orderidioms—e.g.,

Subscribe/Notify,Visitor,etc.)

CIS500,ProgrammingwithOCaml4

Page 5: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

ComputingwithExpressions

OCamlisanexpressionlanguage.Aprogramisanexpression.The“meaning”

oftheprogramisthevalueoftheexpression.

#16+18;;

-:int=34

#2*8+3*6;;

-:int=34

CIS500,ProgrammingwithOCaml5

Page 6: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Thetoplevel

OCamlprovidesbothaninteractivetoplevelandacompilerthatproduces

standardexecutablebinaries.Thetoplevelprovidesaconvenientwayof

experimentingwithsmallprograms.

Themodeofinteractingwiththetoplevelistypinginaseriesofexpressions;

OCamlevaluatesthemastheyaretypedanddisplaystheresults(andtheir

types).Intheinteractionabove,linesbeginningwith#areinputsandlines

beginningwith-arethesystem’sresponses.Notethatinputsarealways

terminatedbyadoublesemicolon.

CIS500,ProgrammingwithOCaml6

Page 7: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Givingthingsnames

Theletconstructgivesanametotheresultofanexpressionsothatitcanbe

usedlater.

#letinchesPerMile=12*3*1760;;

valinchesPerMile:int=63360

#letx=1000000/inchesPerMile;;

valx:int=15

CIS500,ProgrammingwithOCaml7

Page 8: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Functions

#letcube(x:int)=x*x*x;;

valcube:int->int=<fun>

#cube9;;

-:int=729

Wecallxtheparameterofthefunctioncube;theexpressionx*x*xisitsbody.

Theexpressioncube9isanapplicationofcubetotheargument9.

ThetypeprintedbyOCaml,int->int(pronounced“intarrowint”)

indicatesthatcubeisafunctionthatshouldbeappliedtoasingle,integer

argumentandthatreturnsaninteger.

Thetypeannotationontheparameter(x:int)isoptional.OCamlcanfigure

itout.However,yourlifewillbemuchsimplerifyouputiton.

NotethatOCamlrespondstoafunctiondeclarationbyprintingjust<fun>as

thefunction’s“value.”

CIS500,ProgrammingwithOCaml8

Page 9: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Hereisafunctionwithtwoparameters:

#letsumsq(x:int)(y:int)=x*x+y*y;;

valsumsq:int->int->int=<fun>

#sumsq34;;

-:int=25

Thetypeprintedforsumsqisint->int->int,indicatingthatitshouldbe

appliedtotwointegerargumentsandyieldsanintegerasitsresult.

NotethatthesyntaxforinvokingfunctiondeclarationsinOCamlisslightly

differentfromlanguagesintheC/C++/Javafamily:wewritecube3and

sumsq34ratherthancube(3)andsumsq(3,4).

CIS500,ProgrammingwithOCaml9

Page 10: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Thetypeboolean

Thereareonlytwovaluesoftypeboolean:trueandfalse.

Comparisonoperationsreturnbooleanvalues.

#1=2;;

-:bool=false

#4>=3;;

-:bool=true

notisaunaryoperationonbooleans.

#not(5<=10);;

-:bool=false

#not(2=2);;

-:bool=false

CIS500,ProgrammingwithOCaml10

Page 11: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Conditionalexpressions

TheresultoftheconditionalexpressionifBthenE1elseE2iseitherthe

resultofE1orthatofE2,dependingonwhethertheresultofBistrueor

false.

#if3<4then7else100;;

-:int=7

#if3<4then(3+3)else(10*10);;

-:int=6

#iffalsethen(3+3)else(10*10);;

-:int=100

#iffalsethenfalseelsetrue;;

-:bool=true

CIS500,ProgrammingwithOCaml11

Page 12: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Lists

Onehandystructureforstoringacollectionofdatavaluesisalist.Listsare

providedasabuilt-intypeinOCamlandanumberofotherpopularlanguages.

WecanbuildalistinOCamlbywritingoutitselements,enclosedinsquare

bracketsandseparatedbysemicolons.

#[1;3;2;5];;

-:intlist=[1;3;2;5]

ThetypethatOCamlprintsforthislistispronouncedeither“integerlist”or

“listofintegers”.

Theemptylist,written[],issometimescalled“nil.”

CIS500,ProgrammingwithOCaml12

Page 13: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Thetypesoflists

Wecanbuildlistswhoseelementsaredrawnfromanyofthebasictypes(int,

bool,etc.).

#["cat";"dog";"gnu"];;

-:stringlist=["cat";"dog";"gnu"]

#[true;true;false];;

-:boollist=[true;true;false]

Wecanalsobuildlistsoflists:

#[[1;2];[2;3;4];[5]];;

-:intlistlist=[[1;2];[2;3;4];[5]]

Infact,foreverytypet,wecanbuildlistsoftypetlist.

CIS500,ProgrammingwithOCaml13

Page 14: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Listsarehomogeneous

OCamldoesnotallowdifferenttypesofelementstobemixedwithinthesame

list:

#[1;2;"dog"];;

Characters7-13:

Thisexpressionhastypestringlistbutishereused

withtypeintlist

CIS500,ProgrammingwithOCaml14

Page 15: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

ConstructingLists

OCamlprovidesanumberofbuilt-inoperationsthatreturnlists.Themost

basiconecreatesanewlistbyaddinganelementtothefrontofanexisting

list.Itiswritten::andpronounced“cons”(becauseitconstructslists).

#1::[2;3];;

-:intlist=[1;2;3]

#letadd123(l:intlist)=1::2::3::l;;

valadd123:intlist->intlist=<fun>

#add123[5;6;7];;

-:intlist=[1;2;3;5;6;7]

#add123[];;

-:intlist=[1;2;3]

CIS500,ProgrammingwithOCaml15

Page 16: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Somerecursivefunctionsthatgeneratelists

#letrecrepeat(k:int)(n:int)=(*Alistofncopiesofk*)

ifn=0then[]

elsek::repeatk(n-1);;

#repeat712;;

-:intlist=[7;7;7;7;7;7;7;7;7;7;7;7]

#letrecfromTo(m:int)(n:int)=(*Thenumbersfrommton*)

ifn<mthen[]

elsem::fromTo(m+1)n;;

#fromTo918;;

-:intlist=[9;10;11;12;13;14;15;16;17;18]

CIS500,ProgrammingwithOCaml16

Page 17: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

ConstructingLists

Anylistcanbebuiltby“consing”itselementstogether:

-#1::2::3::2::1::[];;;

-:intlist=[1;2;3;2;1]

Infact,

[x1;x2;...;xn]

issimplyashorthandfor

x1::x2::...::xn::[]

Notethat,whenweomitparenthesesfromanexpressioninvolvingseveraluses

of::,weassociatetotheright—i.e.,1::2::3::[]meansthesamethingas

1::(2::(3::[])).Bycontrast,arithmeticoperatorslike+and-associateto

theleft:1-2-3-4means((1-2)-3)-4.

CIS500,ProgrammingwithOCaml17

Page 18: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

TakingListsApart

OCamlprovidestwobasicoperationsforextractingthepartsofalist.

�List.hd(pronounced“head”)returnsthefirstelementofalist.

#List.hd[1;2;3];;

-:int=1

�List.tl(pronounced“tail”)returnseverythingbutthefirstelement.

#List.tl[1;2;3];;

-:intlist=[2;3]

CIS500,ProgrammingwithOCaml18

Page 19: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

#List.tl(List.tl[1;2;3]);;

-:intlist=[3]

#List.tl(List.tl(List.tl[1;2;3]));;

-:intlist=[]

#List.hd(List.tl(List.tl[1;2;3]));;

-:int=3

#List.hd[[5;4];[3;2]];;

-:intlist=[5;4]

#List.hd(List.hd[[5;4];[3;2]]);;

-:int=5

#List.tl(List.hd[[5;4];[3;2]]);;

-:intlist=[4]

CIS500,ProgrammingwithOCaml19

Page 20: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Modules–abriefdigression

Likemostprogramminglanguages,OCamlincludesamechanismforgrouping

collectionsofdefinitionsintomodules.

Forexample,thebuilt-inmoduleListprovidestheList.hdandList.tl

functions(andmanyothers).Thatis,thenameList.hdreallymeans“the

functionhdfromthemoduleList.”

CIS500,ProgrammingwithOCaml20

Page 21: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Recursiononlists

Lotsofusefulfunctionsonlistscanbewrittenusingrecursion.Here’sonethat

sumstheelementsofalistofnumbers:

#letreclistSum(l:intlist)=

ifl=[]then0

elseList.hdl+listSum(List.tll);;

#listSum[5;4;3;2;1];;

-:int=15

CIS500,ProgrammingwithOCaml21

Page 22: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Consingontheright

#letrecsnoc(l:intlist)(x:int)=

ifl=[]thenx::[]

elseList.hdl::snoc(List.tll)x;;

valsnoc:intlist->int->intlist=<fun>

#snoc[5;4;3;2]1;;

-:intlist=[5;4;3;2;1]

CIS500,ProgrammingwithOCaml22

Page 23: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

BasicPatternMatching

Listscaneitherbeemptyornon-empty.OCamlprovidesaconvenient

pattern-matchingconstructthatdetermineswhetherthislistisempty,andifit

isnot,allowaccesstothefirstelement.

#letreclistSum(l:intlist)=

matchlwith

[]->0

|x::y->x+listSumy;;

#listSum[5;4;3;2;1];;

-:int=15

CIS500,ProgrammingwithOCaml23

Page 24: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Patternmatchingcanbeusedwithtypesotherthanlists.Forexample,hereit

isusedonintegers:

#letrecfact(n:int)=

matchnwith

0->1

|_->n*fact(n-1);;

The_patternhereisawildcardthatmatchesanyvalue.

CIS500,ProgrammingwithOCaml24

Page 25: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

ComplexPatterns

Thebasicelements(constants,variablebinders,wildcards,[],::,etc.)may

becombinedinarbitrarilycomplexwaysinmatchexpressions:

#letsilly(l:intlist)=

matchlwith

[_;_;_]->"threeelementslong"

|_::x::y::_::_::rest->ifx>ythen"foo"else"bar"

|_->"dunno";;

valsilly:intlist->string=<fun>

#silly[1;2;3];;

-:string="threeelementslong"

#silly[1;2;3;4];;

-:string="dunno"

#silly[1;2;3;4;5];;

-:string="bar"

CIS500,ProgrammingwithOCaml25

Page 26: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Example:Findingwords

Supposewewanttotakealistofcharactersandreturnalistoflistsof

characters,whereeachelementofthefinallistisa“word”fromtheoriginal

list.

#split[’t’;’h’;’e’;’’;’b’;’r’;’o’;’w’;’n’;’’;’d’;’o’;’g’];;

-:charlistlist=

[[’t’;’h’;’e’];[’b’;’r’;’o’;’w’;’n’];[’d’;’o’;’g’]]

(Notethatcharacterconstantsarewrittenwithsinglequotes.)

CIS500,ProgrammingwithOCaml26

Page 27: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Animplementationofsplit

#letrecloop(w:charlist)(l:charlist)=

matchlwith

[]->[w]

|(’’::ls)->w::(loop[]ls)

|(c::ls)->loop(w@[c])ls;;

valloop:charlist->charlist->charlistlist=<fun>

#letsplit(l:charlist)=loop[]l;;

valsplit:charlist->charlistlist=<fun>

CIS500,ProgrammingwithOCaml27

Page 28: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Aside:Localfunctiondefinitions

Theloopfunctioniscompletelylocaltosplit:thereisnoreasonforanybody

elsetouseit—oreven,foranybodyelsetobeabletoseeit!Itisgoodstyle

inOCamltowritesuchdefinitionsaslocalbindings:

#letsplit(l:charlist)=

letrecloop(w:charlist)(l:charlist)=

matchlwith

[]->[w]

|(’’::ls)->w::(loop[]ls)

|(c::ls)->loop(w@[c])ls

in

loop[]l;;

CIS500,ProgrammingwithOCaml28

Page 29: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Ingeneral,anyletdefinitionthatcanappearatthetoplevel

#let...;;

#e;;;

canalsoappearinalet...in...form.

#let...ine;;;

CIS500,ProgrammingwithOCaml29

Page 30: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Tuples

Itemsconnectedbycommasare“tuples”

#"age",44;;

-:string*int="age",44

#("professor",("age",33));;

-:string*(string*int)="professor",("age",33)

#("children",["bob";"ted";"alice"]);;

-:string*stringlist="children",["bob";"ted";"alice"]

#letg((x:int),(y:int))=x*y;;

valg:int*int->int=<fun>

Howmanyargumentsdoesgtake?

CIS500,ProgrammingwithOCaml30

Page 31: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Tuplesarenotlists

Pleasedonotconfusethem!

#lettuple="cow","dog","sheep";;

valtuple:string*string*string="cow","dog","sheep"

#letlist=["cow";"dog";"sheep"];;

vallist:stringlist=["cow";"dog";"sheep"]

#List.hdtuple;;

Thisexpressionhastypestring*string*stringbutishereused

withtype’alist

#List.hdlist;;

-:string="cow"

#lettup2=1,"cow";;

valtup2:int*string=1,"cow"

#letl2=[1;"cow"];;

Thisexpressionhastypestringbutishereusedwithtypeint

CIS500,ProgrammingwithOCaml31

Page 32: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

TuplesandPatternMatching

Tuplescanbe“deconstructed”bypatternmatching:

#letlastNamename=

matchnamewith

(n1,_,_)->n1;;

#lastName("Weirich","Stephanie","Penn");;

-:string="Weirich"

CIS500,ProgrammingwithOCaml32

Page 33: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

BasicExceptions

OCaml’sexceptionmechanismisroughlysimilartothatfoundin,forexample,

Java.

Webeginbydefininganexception:

#exceptionBad;;

Now,encounteringraiseBadwillimmediatelyterminateevaluationand

returncontroltothetoplevel:

#letrecfact(n:int)=

ifn<0thenraiseBad

elseifn=0then1

elsen*fact(n-1);;

#fact(-3);;

Exception:Bad.

CIS500,ProgrammingwithOCaml33

Page 34: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Naturally,exceptionscanalsobecaughtwithinaprogram(usingthe

try...with...form),butlet’sleavethatforanotherday.

CIS500,ProgrammingwithOCaml34

Page 35: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

DataTypes

Wehaveseenanumberofdatatypes:

int

bool

string

char

lists

tuples

OCamlhasafewotherbuilt-indatatypes—inparticular,float,with

operationslike+.,*.,etc.

Onecanalsocreatecompletelynewdatatypes.

CIS500,ProgrammingwithOCaml35

Page 36: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Theneedfornewtypes

Theabilitytoconstructnewtypesisanessentialpartofmostprogramming

languages.

Supposewearebuildinga(verysimple)graphicsprogramthatdisplayscircles

andsquares.Wecanrepresenteachofthesewiththreerealnumbers.

CIS500,ProgrammingwithOCaml36

Page 37: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Acircleisrepresentedbytheco-ordinatesofitscenteranditsradius.Asquare

isrepresentedbytheco-ordinatesofitsbottomleftcorneranditswidth.So

wecanrepresentbothshapesaselementsofthetype:

float*float*float

However,therearetwoproblemswithusingthistypetorepresentcirclesand

squares.First,itisabitlongandunwieldy,bothtowriteandtoread.Second,

becausetheirtypesareidentical,thereisnothingtopreventusfrommixing

circlesandsquares.Forexample,ifwewrite

#letareaOfSquare((_,_,d):float*float*float)=d*.d;;

wemightaccidentallyapplytheareaOfSquarefunctiontoacircleandgeta

nonsensicalresult.

(Numericaloperationsonthefloattypearewrittendifferentlyfromthe

correspondingoperationsonint—e.g.,+.insteadof+.SeetheOCaml

manualformoreinformation.)

CIS500,ProgrammingwithOCaml37

Page 38: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

DataTypes

Wecanimprovemattersbydefiningsquareasanewtype:

#typesquare=Squareoffloat*float*float;;

Thisdoestwothings:

�Itcreatesanewtypecalledsquarethatisdifferentfromanyothertypein

thesystem.

�ItcreatesaconstructorcalledSquare(withacapitalS)thatcanbeused

tocreateasquarefromthreefloats.Forexample:

#Square(1.1,2.2,3.3);;

-:square=Square(1.1,2.2,3.3)

CIS500,ProgrammingwithOCaml38

Page 39: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Takingdatatypesapart

Wetaketypesapartwith(surprise,surprise...)patternmatching.

#letareaOfSquare(s:square)=

matchswith

Square(_,_,d)->d*.d;;

valareaOfSquare:square->float=<fun>

#letbottomLeftCoords(s:square)=

matchswith

Square(x,y,_)->(x,y);;

valbottomLeftCoords:square->float*float=<fun>

SowecanuseconstructorslikeSquarebothasfunctionsandaspatterns.

Constructorsarerecognizedbybeingcapitalized(thefirstletterisuppercase).

CIS500,ProgrammingwithOCaml39

Page 40: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Thesefunctionscanbewrittenalittlemoreconciselybycombiningthe

patternmatchingwiththefunctionheader:

#letareaOfSquare(Square(_,_,d):square)=d*.d;;

#letbottomLeftCoords(Square(x,y,_):square)=(x,y);;

CIS500,ProgrammingwithOCaml40

Page 41: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Continuing,wecandefineadatatypeforcirclesinthesameway.

#typecircle=Circleoffloat*float*float;;

#letc=Circle(1.0,2.0,2.0);;

#letareaOfCircle(Circle(_,_,r):circle)=3.14159*.r*.r;;

#letcenterCoords(Circle(x,y,_):circle)=(x,y);;

#areaOfCirclec;;

-:float=12.56636

Wecannotnowapplyafunctionintendedfortypesquaretoavalueoftype

circle:

#areaOfSquare(c);;

Thisexpressionhastypecirclebutishereusedwithtypesquare.

CIS500,ProgrammingwithOCaml41

Page 42: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Varianttypes

Goingbacktotheideaofagraphicsprogram,weobviouslywanttohave

severalshapesonthescreenatonce.Forthiswe’dprobablywanttokeepalist

ofcirclesandsquares,butsuchalistwouldbeheterogenous.Howdowemake

suchalist?

Thesolutionistobuildatypethatcanbeeitheracircleorasquare.

#typeshape=Circleoffloat*float*float

|Squareoffloat*float*float;;

NowbothconstructorsCircleandSquarecreatevaluesoftypeshape.For

example:

#Square(1.0,2.0,3.0);;

-:shape=Square(1.000000,2.000000,3.000000)

Atypethatcanhavemorethanoneformisoftencalledavarianttype.

CIS500,ProgrammingwithOCaml42

Page 43: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Wecanalsowritefunctionsthatdotherightthingonallformsofavariant

type.Againweusepatternmatching:

#letarea(s:shape)=

matchswith

Circle(_,_,r)->3.14159*.r*.r

|Square(_,_,d)->d*.d;;

#area(Circle(0.0,0.0,1.5));;

-:float=7.0685775

CIS500,ProgrammingwithOCaml43

Page 44: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

A“heterogeneous”list:

#letl=[Circle(0.0,0.0,1.5);Square(1.0,2.0,1.0);

Circle(2.0,0.0,1.5);Circle(5.0,0.0,2.5)];;

CIS500,ProgrammingwithOCaml44

Page 45: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Mixed-modeArithmetic

Manyprogramminglanguages(Lisp,Basic,Perl,databasequerylanguages)

usevarianttypesinternallytorepresentnumbersthatcanbeeitherintegersor

floats.Thisamountsto“tagging”eachnumericvaluewithanindicatorthat

sayswhatkindofnumberitis.

#typenum=Intofint|Floatoffloat;;

#letadd(r1:num)(r2:num)=

match(r1,r2)with

(Inti1,Inti2)->Int(i1+i2)

|(Floatr1,Inti2)->Float(r1+.floati2)

|(Inti1,Floatr2)->Float(floati1+.r2)

|(Floatr1,Floatr2)->Float(r1+.r2);;

#add(Int3)(Float4.5);;

-:num=Float7.5

CIS500,ProgrammingwithOCaml45

Page 46: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Multiplication,multfollowsexactlythesamepattern:

#letmult(r1:num)(r2:num)=

match(r1,r2)with

(Inti1,Inti2)->Int(i1*i2)

|(Floatr1,Inti2)->Float(r1*.floati2)

|(Inti1,Floatr2)->Float(floati1*.r2)

|(Floatr1,Floatr2)->Float(r1*.r2);;

CIS500,ProgrammingwithOCaml46

Page 47: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

ADataTypeforOptionalValues

Supposeweareimplementingasimplelookupfunctionforatelephone

directory.Wewanttogiveitastringandgetbackanumber(sayaninteger).

Weexpecttohaveafunctionlookupwhosetypeis

lookup:string->directory->int

wheredirectoryisa(yettobedecided)typethatwe’llusetorepresentthe

directory.

However,thisisn’tquiteenough.Whathappensifagivenstringisn’tinthe

directory?Whatshouldlookupreturn?

Thereareseveralwaystodealwiththisissue.Oneistoraiseanexception.

Anotherisbasedonthefollowingdatatype:

#typemaybe=Absent|Presentofint;;

CIS500,ProgrammingwithOCaml47

Page 48: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Toseehowthistypeisused,let’srepresentourdirectoryasalistofpairs:

#typedirectory=(string*int)list;;

#letdirectory=[("Joe",1234);("Martha",5672);

("Jane",3456);("Ed",7623)];;

#letreclookup(s:string)(l:directory)=

matchlwith

[]->Absent

|(k,i)::t->ifk=sthenPresent(i)

elselookupst;;

#lookup"Jane"directory;;

-:maybe=Present3456

#lookup"Karen"directory;;

-:maybe=Absent

CIS500,ProgrammingwithOCaml48

Page 49: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Built-inoptions

Becauseoptionsareoftenusefulinfunctionalprogramming,OCamlprovidesa

built-intypetoptionforeachtypet.ItsconstructorsareNone

(correspondingtoAbsent)andSome(forPresent).

#letreclookup(s:string)(l:directory)=

matchlwith

[]->None

|(k,i)::t->ifk=sthenSome(i)

elselookupst;;

#lookup"Jane"directory;;

-:intoption=Some3456

CIS500,ProgrammingwithOCaml49

Page 50: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Enumerations

Ourmaybedatatypehasonevariant,Absent,thatisa“constant”constructor

carryingnodatavalueswithit.Datatypesinwhichallthevariantsare

constantscanactuallybequiteuseful...

#typecolor=Red|Yellow|Green;;

#letnext(c:color)=

matchcwithGreen->Yellow|Yellow->Red|Red->Green;;

#typeday=Sunday|Monday|Tuesday|Wednesday

|Thursday|Friday|Saturday;;

#letweekend(d:day)=

matchdwith

Saturday->true

|Sunday->true

|_->false;;

CIS500,ProgrammingwithOCaml50

Page 51: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

ABooleanDataType

Asimpledatatypecanbeusedtoreplacethebuilt-inbooleans.

WeusetheconstantconstructorsTrueandFalsetorepresenttrueandfalse.

We’llusedifferentnamesasneededtoavoidconfusionbetweenourbooleans

andthebuilt-inones:

#typemyBool=False|True;;

#letmyNot(b:myBool)=matchbwithFalse->True|True->False;;

#letmyAnd(b1:myBool)(b2:myBool)=

match(b1,b2)with

(True,True)->True

|(True,False)->False

|(False,True)->False

|(False,False)->False;;

NotethatthebehaviorofmyAndisnotquitethesameasthebuilt-in&&!

CIS500,ProgrammingwithOCaml51

Page 52: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

RecursiveTypes

Considerthetinylanguageofarithmeticexpressionsdefinedbythefollowing

grammar:

exp::=number

exp+exp

exp-exp

exp*exp

CIS500,ProgrammingwithOCaml52

Page 53: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Wecantranslatethisgrammardirectlyintoadatatypedefinition:

typeast=

ANumofint

|APlusofast*ast

|AMinusofast*ast

|ATimesofast*ast;;

Notes:

�Thisdatatype(liketheoriginalgrammar)isrecursive.

�Thetypeastrepresentsabstractsyntaxtrees,whichcapturethe

underlyingtreestructureofexpressions,suppressingsurfacedetailssuchas

parentheses

CIS500,ProgrammingwithOCaml53

Page 54: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Anevaluatorforexpressions

Goal:writeanevaluatorfortheseexpressions.

valeval:ast->int=<fun>

#eval(ATimes(APlus(ANum12,ANum340),ANum5));;

-:int=1760

CIS500,ProgrammingwithOCaml54

Page 55: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Thesolutionusesarecursivefunctionplusapatternmatch.

letreceval(e:ast)=

matchewith

ANumi->i

|APlus(e1,e2)->evale1+evale2

|AMinus(e1,e2)->evale1-evale2

|ATimes(e1,e2)->evale1*evale2;;

Thepatternofrecursionfollowsthedefinitionofthedatatype.

typeast=

ANumofint

|APlusofast*ast

|AMinusofast*ast

|ATimesofast*ast;;

CIS500,ProgrammingwithOCaml55

Page 56: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Thesolutionusesarecursivefunctionplusapatternmatch.

letreceval(e:ast)=

matchewith

ANumi->i

|APlus(e1,e2)->evale1+evale2

|AMinus(e1,e2)->evale1-evale2

|ATimes(e1,e2)->evale1*evale2;;

Thepatternofrecursionfollowsthedefinitionofthedatatype.

typeast=

ANumofint

|APlusofast*ast

|AMinusofast*ast

|ATimesofast*ast;;

CIS500,ProgrammingwithOCaml55-a

Page 57: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Polymorphism

#letreclastl=

matchlwith

[]->raiseBad

|[x]->x

|_::y->lasty

Whattypeshouldwegivetotheparameterl?

Itdoesn’tmatterwhattypeofobjectsarestoredinthelist,wecouldmakeit

intlistorboollistandOCamlwouldnotcomplain.However,ifwechose

oneofthesetypes,wouldnotbeabletoapplylasttotheother.

OCamlletsususeatypevariabletoabstractpartofatypeifitisnot

important.Wecangivelthetype’alist(pronounced“alpha”),standing

foranarbitrarytype.Whenweusethefunction,OCamlwillfigureoutwhat

typeweneed.

CIS500,ProgrammingwithOCaml56

Page 58: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Polymorphism

#letreclastl=

matchlwith

[]->raiseBad

|[x]->x

|_::y->lasty

Whattypeshouldwegivetotheparameterl?

Itdoesn’tmatterwhattypeofobjectsarestoredinthelist,wecouldmakeit

intlistorboollistandOCamlwouldnotcomplain.However,ifwechose

oneofthesetypes,wouldnotbeabletoapplylasttotheother.

OCamlletsususeatypevariabletoabstractpartofatypeifitisnot

important.Wecangivelthetype’alist(pronounced“alpha”),standing

foranarbitrarytype.Whenweusethefunction,OCamlwillfigureoutwhat

typeweneed.

CIS500,ProgrammingwithOCaml56-a

Page 59: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Polymorphism

#letreclastl=

matchlwith

[]->raiseBad

|[x]->x

|_::y->lasty

Whattypeshouldwegivetotheparameterl?

Itdoesn’tmatterwhattypeofobjectsarestoredinthelist,wecouldmakeit

intlistorboollistandOCamlwouldnotcomplain.However,ifwechose

oneofthesetypes,wouldnotbeabletoapplylasttotheother.

OCamlletsususeatypevariabletoabstractpartofatypeifitisnot

important.Wecangivelthetype’alist(pronounced“alpha”),standing

foranarbitrarytype.Whenweusethefunction,OCamlwillfigureoutwhat

typeweneed.

CIS500,ProgrammingwithOCaml56-b

Page 60: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Polymorphism

Thisversionoflastissaidtobepolymorphic,becauseitcanbeappliedto

manydifferenttypesofarguments.(“Poly”=many,“morph”=shape.)

Notethatthetypeoftheelementsoflis’a(pronounced“alpha”).Thisisa

typevariable,whichcaninstantiated,eachtimeweapplylast,byreplacing’a

withanytypethatwelike.Theinstancesofthetype’alist->’ainclude

intlist->int

stringlist->string

intlistlist->intlist

etc.

Inotherwords,

last:’alist->’a

canberead,“lastisafunctionthattakesalistofelementsofanytypealpha

andreturnsanelementofalpha.”

CIS500,ProgrammingwithOCaml57

Page 61: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Apolymorphicappend

#letrecappend(l1:’alist)(l2:’alist)=

matchl1with

[]->l2

|(hd::tl)->hd::appendtll2;;

valappend:’alist->’alist->’alist=<fun>

#append[4;3;2][6;6;7];;

-:intlist=[4;3;2;6;6;7]

#append["cat";"in"]["the";"hat"];;

-:stringlist=["cat";"in";"the";"hat"]

CIS500,ProgrammingwithOCaml58

Page 62: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Apolymorphicrev

#letrecrevaux(l:’alist)(res:’alist)=

matchlwith

[]->res

|(hd::tl)->revauxtl(hd::res);;

valrevaux:’alist->’alist->’alist=<fun>

#letrev(l:’alist)=revauxl[];;

valrev:’alist->’alist=<fun>

#rev["cat";"in";"the";"hat"];;

-:stringlist=["hat";"the";"in";"cat"]

#rev[false;true];;

-:boollist=[true;false]

CIS500,ProgrammingwithOCaml59

Page 63: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Polymorphicrepeat

#letrecrepeat(k:’a)(n:int)=(*Alistofncopiesofk*)

ifn=0then[]

elsek::repeatk(n-1);;

#repeat712;;

-:intlist=[7;7;7;7;7;7;7;7;7;7;7;7]

#repeattrue3;;

-:boollist=[true;true;true]

#repeat[6;7]4;;

-:intlistlist=[[6;7];[6;7];[6;7];[6;7]]

Whatisthetypeofrepeat?

CIS500,ProgrammingwithOCaml60

Page 64: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

map:“apply-to-each”

OCamlhasapredefinedfunctionList.mapthattakesafunctionfandalistl

andproducesanotherlistbyapplyingftoeachelementofl.We’llsoonsee

howtodefineList.map,butfirstlet’slookatsomeexamples.

#List.mapsquare[1;3;5;9;2;21];;

-:intlist=[1;9;25;81;4;441]

#List.mapnot[false;false;true];;

-:boollist=[true;true;false]

NotethatList.mapispolymorphic:itworksforlistsofintegers,strings,

booleans,etc.

CIS500,ProgrammingwithOCaml61

Page 65: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Moreonmap

AninterestingfeatureofList.mapisitsfirstargumentisitselfafunction.For

thisreason,wecallList.mapahigher-orderfunction.

Naturalusesforhigher-orderfunctionsarisefrequentlyinprogramming.One

ofOCaml’sstrengthsisthatitmakeshigher-orderfunctionsveryeasytowork

with.

InotherlanguagessuchasJava,higher-orderfunctionscanbe(andoftenare)

simulatedusingobjects.

CIS500,ProgrammingwithOCaml62

Page 66: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

filter

Anotherusefulhigher-orderfunctionisList.filter.Whenappliedtoalistl

andabooleanfunctionp,itextractsfromlthelistofthoseelementsforwhich

preturnstrue.

#letreceven(n:int)=

ifn=0thentrue

elseifn=1thenfalse

elseifn<0theneven(-n)

elseeven(n-2);;

valeven:int->bool=<fun>

#List.filtereven[1;2;3;4;5;6;7;8;9];;

-:intlist=[2;4;6;8]

#List.filterpalindrome[[1];[1;2;3];[1;2;1];[]];;

-:intlistlist=[[1];[1;2;1];[]]

CIS500,ProgrammingwithOCaml63

Page 67: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Notethat,likemap,List.filterispolymorphic—itworksonlistsofany

type.

CIS500,ProgrammingwithOCaml64

Page 68: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Definingmap

List.mapcomespredefinedintheOCamlsystem,butthereisnothingmagic

aboutit—wecaneasilydefineourownmapfunctionwiththesamebehavior.

letrecmap(f:’a->’b)(l:’alist)=

matchlwith

[]->[]

|(hd::tl)->fhd::mapftl;;

valmap:(’a->’b)->’alist->’blist=<fun>

Thetypeofmapisprobablyevenmorepolymorphicthanyouexpected!The

listthatitreturnscanactuallybeofadifferenttypefromitsargument:

#mapString.length["The";"quick";"brown";"fox"];;

-:intlist=[3;5;5;3]

CIS500,ProgrammingwithOCaml65

Page 69: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Definingfilter

Similarly,wecandefineourownfilterthatbehavesthesameas

List.filter.

letrecfilter(p:’a->bool)(l:’alist)=

matchlwith

[]->[]

|(hd::tl)->ifphdthenhd::filterptl

elsefilterptl;;

valfilter:(’a->bool)->’alist->’alist=<fun>

CIS500,ProgrammingwithOCaml66

Page 70: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

GenericProgramming

ThepolymorphisminMLthatarisesfromtypeparametersisanexampleof

genericprogramming.(mapl,filter,etc.)aregoodexamplesofgeneric

functions.Differentlanguagessupportgenericprogrammingindifferentways...

�parametricpolymorphismallowsfunctionstoworkuniformlyover

argumentsofdifferenttypes.E.g.,last:’alist->’a

�adhocpolymporphism(oroverloading)allowsanoperationtobehavein

differentwayswhenappliedtoargumentsofdifferenttypes.Thereisno

suchpolymorphisminOCaml,butmostlanguagesallowsomeoverloading

(e.g.2+3and2.4+3.6).

�subtypepolymporphismallowsoperationstobedefinedforcollectionsof

typessharingsomecommonstructure

e.g.,afeedoperationmightmakesenseforvaluesofanimalandallits

“refinements”—cow,tiger,moose,etc.

CIS500,ProgrammingwithOCaml67

Page 71: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

OCamlsupportsparametricpolymorphisminaverygeneralway,andalso

supportssubtyping(ThoughweshallnotgettoseethisaspectofOCaml,its

supportforsubtypingiswhatdistinguishesitfromotherdialectsofML.)It

doesnotallowoverloading.

Javaprovidesasubtypingaswellasmoderatelypowerfuloverloading,butno

parametricpolymorphism.(Java1.5betaversionhasparametric

polymorphism,called“Generics”.)

Confusingly,thebareterm“polymorphism”isusedtorefertoparametric

polymorphismintheMLcommunityandforsubtypepolymorphisminthe

Javacommunity!

CIS500,ProgrammingwithOCaml68

Page 72: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

ApproachestoTyping

�Astronglytypedlanguagepreventsprogramsfromaccessingprivatedata,

corruptingmemory,crashingthemachine,etc.

�Aweaklytypedlanguagedoesnot.

�Astaticallytypedlanguageperformstype-consistencychecksatwhen

programsarefirstentered.

�Adynamicallytypedlanguagedelaysthesechecksuntilprogramsare

executed.

WeakStrong

DynamicLisp,Scheme,Perl,Python,Smalltalk

StaticC,C++ML,ADA,Java?

?Strictlyspeaking,Javashouldbecalled“mostlystatic”

CIS500,ProgrammingwithOCaml69

Page 73: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

PracticewithTypes

Whatarethetypesofthefollowingfunctions?

�letf(x:int)=x+1

�letfx=x+1

�letf(x:int)=[x]

�letfx=[x]

�letfx=x

�letfx=hd(tlx)::[1.0]

�letfx=hd(tlx)::[]

�letfx=1::x

�letfxy=x::y

CIS500,ProgrammingwithOCaml70

Page 74: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

�letfxy=x::[]

�letfx=x@x

�letfx=x::x

�letfxyz=ifx>3thenyelsez

�letfxyz=ifx>3thenyelse[z]

Andonemore:

letrecfx=

if(tlx)=[]thenx

elsef(tlx)

CIS500,ProgrammingwithOCaml71

Page 75: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

ProgrammingwithFunctionsinOCaml

CIS500,ProgrammingwithOCaml72

Page 76: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

FunctionsasData

FunctionsinOCamlarefirstclass—theyhavethesamerightsandprivileges

asvaluesofanyothertypes.E.g.,theycanbe

�passedasargumentstootherfunctions

�returnedasresultsfromotherfunctions

�storedindatastructuressuchastuplesandlists

�etc.

CIS500,ProgrammingwithOCaml73

Page 77: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Multi-parameterfunctions

Wehaveseentwowaysofwritingfunctionswithmultipleparameters:

#letfooxy=x+y;;

valfoo:int->int->int=<fun>

#letbar(x,y)=x+y;;

valbar:int*int->int=<fun>

Thefirsttakesitstwoargumentsseparately;thesecondtakesatupleanduses

apatterntoextractitsfirstandsecondcomponents.

CIS500,ProgrammingwithOCaml74

Page 78: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Thesyntaxforapplyingthesetwoformsoffunctiontotheirargumentsdiffers

correspondingly:

#foo23;;

-:int=5

#bar(4,5);;

-:int=9

#foo(2,3);;

Thisexpressionhastypeint*int

butishereusedwithtypeint

#bar45;;

Thisfunctionisappliedtotoomanyarguments

CIS500,ProgrammingwithOCaml75

Page 79: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

PartialApplication

Oneadvantageofthefirstformofmultiple-argumentfunctionisthatsuch

functionsmaybepartiallyapplied.

#letfoo2=foo2;;

valfoo2:int->int=<fun>

#foo23;;

-:int=5

#foo25;;

-:int=7

#List.mapfoo2[3;6;10;100];;

-:intlist=[5;8;12;102]

CIS500,ProgrammingwithOCaml76

Page 80: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Currying

Obviously,thesetwoformsarecloselyrelated—givenone,wecaneasily

definetheother.

#letfoo’xy=bar(x,y);;

valfoo’:int->int->int=<fun>

#letbar’(x,y)=fooxy;;

valbar’:int*int->int=<fun>

CIS500,ProgrammingwithOCaml77

Page 81: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Currying

Indeed,thesetransformationscanthemselvesbeexpressedas(higher-order)

functions:

#letcurryfxy=f(x,y);;

valcurry:(’a*’b->’c)->’a->’b->’c=<fun>

#letfoo’’=currybar;;

valfoo’’:int->int->int=<fun>

#letuncurryf(x,y)=fxy;;

valuncurry:(’a->’b->’c)->’a*’b->’c=<fun>

#letbar’’=uncurryfoo;;

valbar’’:int*int->int=<fun>

CIS500,ProgrammingwithOCaml78

Page 82: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

ACloserLook

Thetypeint->int->intcanequivalentlybewritten

int->(int->int).

Thatis,afunctionoftypeint->int->intisactuallyafunctionthat,

whenappliedtoaninteger,yieldsafunctionthat,whenappliedtoaninteger,

yieldsaninteger.

Similarly,anapplicationlikefoo23isactuallyshorthandfor(foo2)3.

Formally:->isright-associativeandapplicationisleft-associative.

CIS500,ProgrammingwithOCaml79

Page 83: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

AnonymousFunctions

ItisfairlycommoninOCamlthatweneedtodefineafunctionanduseitjust

once.

#lettimesthreeplustwox=x*3+2;;

valtimesthreeplustwo:int->int=<fun>

#List.maptimesthreeplustwo[4;3;77;12];;

-:intlist=[14;11;233;38]

Tosavemakingupnamesforsuchfunctions,OCamloffersamechanismfor

writingthemin-line:

#List.map(funx->x*3+2)[4;3;77;12];;

-:intlist=[14;11;233;38]

CIS500,ProgrammingwithOCaml80

Page 84: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

AnonymousFunctions

Anonymousfunctionsmayappear,syntactically,inthesameplacesasvalues

ofanyothertypes.

Forexample,thefollowinglet-bindingsarecompletelyequivalent:

#letdoublex=x*2;;

valdouble:int->int=<fun>

#letdouble’=(funx->x*2);;

valdouble’:int->int=<fun>

#double5;;

-:int=10

#double’5;;

-:int=10

CIS500,ProgrammingwithOCaml81

Page 85: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

AnonymousFunctions

Wecanevenwrite:

#(funx->x*2)5;;

-:int=10

Or(slightlymoreusefully):

#(if5*5>20then(funx->x*2)else(funx->x+3))5;;

-:int=10

Theconditionalyieldsafunctiononthebasisofsomebooleantest,andits

resultisthenappliedto5.

CIS500,ProgrammingwithOCaml82

Page 86: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

QuickCheck

Whatisthetypeofl?

#letl=[(funx->x+2);

(funx->x*3);

(funx->ifx>4then0else1)];;

CIS500,ProgrammingwithOCaml83

Page 87: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Applyingalistoffunctions

#letl=[(funx->x+2);

(funx->x*3);

(funx->ifx>4then0else1)];;

vall:(int->int)list=[<fun>;<fun>;<fun>]

#letapplytoxf=fx;;

valapplyto:’a->(’a->’b)->’b=<fun>

#List.map(applyto10)l;;

-:intlist=[12;30;0]

#List.map(applyto2)l;;

-:intlist=[4;6;1]

CIS500,ProgrammingwithOCaml84

Page 88: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Anotherusefulhigher-orderfunction:fold

#letrecfoldflacc=

matchlwith

[]->acc

|a::l->fa(foldflacc);;

valfold:(’a->’b->’b)->’alist->’b->’b

Forexample:

#fold(funab->a+b)[1;3;5;100]0;;

-:int=109

Ingeneral:

f[a1;...;an]b

is

fa1(fa2(...(fanb)...)).

CIS500,ProgrammingwithOCaml85

Page 89: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Usingfold

Mostofthelist-processingfunctionswehaveseencanbedefinedcompactlyin

termsoffold:

#letlistSuml=

fold(funab->a+b)l0;;

vallistSum:intlist->int=<fun>

#letlengthl=

fold(funab->b+1)l0;;

vallength:’alist->int=<fun>

CIS500,ProgrammingwithOCaml86

Page 90: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Usingfold

#letmapfl=

fold(funab->(fa)::b)l[];;

valmap:(’a->’b)->’alist->’blist=<fun>

#letfilterpl=

fold(funab->ifpathen(a::b)elseb)l[];;

CIS500,ProgrammingwithOCaml87

Page 91: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Usingfold

Andeven:

#(*Listofnumbersfrommton,asbefore*)

letrecfromTomn=

ifn<mthen[]

elsem::fromTo(m+1)n;;

valfromTo:int->int->intlist=<fun>

#letfactn=

fold(funab->a*b)(fromTo1n)1;;

valfact:int->int=<fun>

CIS500,ProgrammingwithOCaml88

Page 92: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

QuickCheck

Whatisthetypeofthisfunction?

#letfool=

fold(funab->List.appendb[a])l[];;

Whatdoesitdo?

CIS500,ProgrammingwithOCaml89

Page 93: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Formsoffold

TheOCamlListmoduleactuallyprovidestwofoldingfunctions:

List.fold_left:(’a->’b->’a)->’a->’blist->’a

List.fold_right:(’a->’b->’b)->’alist->’b->’b

Theonewe’recallingfold(hereandinthehomeworkassignment)is

List.fold_right.

List.fold_leftperformsthesamebasicoperationbuttakesitsargumentsin

adifferentorder.

CIS500,ProgrammingwithOCaml90

Page 94: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Theunittype

OCamlprovidesanotherbuilt-intypecalledunit,withjustoneinhabitant,

written().

#letx=();;

valx:unit=()

#letf()=23+34;;

valf:unit->int=<fun>

#f();;

-:int=57

Whyisthisuseful?

CIS500,ProgrammingwithOCaml91

Page 95: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Usesofunit

Afunctionfromunitto’aisadelayedcomputationoftype’a.

Whenwedefinethefunction...

#letf()=<longandcomplexcalculation>;;

valf:unit->int=<fun>

...thelongandcomplexcalculationisjustboxedupinaclosurethatwe

cansaveforlater(bybindingittoavariable,e.g.).

Whenweactuallyneedtheresult,weapplyfto()andthecalculation

actuallyhappens:

#f();;

-:int=57

CIS500,ProgrammingwithOCaml92

Page 96: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Thunks

Afunctionacceptingaunitargumentisoftencalledathunk.

Thunksarewidelyusedinfunctionalprogramming.

Atypicalexample...

CIS500,ProgrammingwithOCaml93

Page 97: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Supposewearewritingafunctionwhereweneedtomakesurethatsome

“finalizationcode”getsexecuted,evenifanexceptionisraised.

#letreadfile=

letchan=open_infilein

try

letnbytes=in_channel_lengthchanin

letstring=String.createnbytesin

really_inputchanstring0nbytes;

close_inchan;

string

withexn->

(*finalizechannel*)

close_inchan;

(*re-raiseexception*)

raiseexn;;

CIS500,ProgrammingwithOCaml94

Page 98: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Wecanavoidduplicatingthefinalizationcodebywrappingitinathunk:

#letreadfile=

letchan=open_infilein

letfinalize()=close_inchanin

try

letnbytes=in_channel_lengthchanin

letstring=String.createnbytesin

really_inputchanstring0nbytes;

finalize();

string

withexn->

(*finalizechannel*)

finalize();

(*re-raiseexception*)

raiseexn;;

CIS500,ProgrammingwithOCaml95

Page 99: OCaml with Programming F Soft CIS - Penn Engineeringcis500/cis500-f05/lectures/0921.pdfOCaml and this course The material in this course is mostly conceptual and mathematical. Ho w

'

&

$

%

Infact,wecangofurther...

#letunwind_protectbodyfinalize=

try

letres=body()in

finalize();

res

withexn->

finalize();

raiseexn;;

#letreadfile=

letchan=open_infilein

unwind_protect

(fun()->

letnbytes=in_channel_lengthchanin

letstring=String.createnbytesin

really_inputchanstring0nbytes;

string)

(fun()->close_inchan);;

CIS500,ProgrammingwithOCaml96