您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 冶金工业 > F # Another ML compiler for NET
F#:AnotherMLcompilerfor.NETDonSyme,24/4/2002OverviewWhy?F#=Caml.NETLanguageChoices(Basic)LanguageChoices(Interop)Usability(Optimization,Packaging)Part1:Motivation&ATasteWhyF#?BackgroundGenericsTypeparametersforMS-IL,C#etc.ILX“Standardize”encodingsforclosuresanddataby“adding”themtoMS-ILAbstractILToolkitformanipulatingILveryeasilyILVerifyVerifier/validatorforIL(writteninCaml)Needaqualitycompilertosetanexample.Isitefficient?Isitsuitableforcross-languageinterop?Needacompilertofindout.SML.NETnotquitesuitable.WritteninCaml,can’tusethisfromC#,oranywhereelse.Needacompilertomakethisaccessible.Couldn’tintegratethisintotheCLRevenifwewantedto.AnMLcompilerwouldhavebeenuseful.WhyF#?BackgroundFuture?ILanalysis?(security?optimization?)ILtransformation?Languagedesign?(systemsprogramming?XML?concurrency?)HenceCaml.NET,nowF#AbsILusefulforallofthese.WhyF#?F#=Caml.NETWhyareMLlanguagessogreat?TypecheckingSimplebagofconstructsTypeinferenceletsyouhackoutcorrectcodequicklyandstillmaintainitWhat’softenwrong?TraditionallyhunguponperformanceSlow,strangecompilers,nodebuggers,noprofilersetc.Nolibraries,nointeropWhyisCamlsogreat?Simple,easy-to-understandcompilerTop-to-bottomdesignchoicesthatmakeseverything“fit”Myhopewouldbeistorepeatthisassuminga.NETworldunderneathUnfortunatelyitwon’tbequitethateasy…F#OverviewLanguage:Simple,extensiblecorelanguageLeaveroomforextensionTools:FastseparatecompilationSimplecompiler(10Klines,excl.ILX)Leverageother.NETtoolsStandards:Notinterestedin100%CamlcompatibilityJusta.NETlanguageWillingtochangeCamlifneeded,butrarelyInterop:Not.NETConsumercan’teasilyimporteverything,mayneedtowritealittleC#Not.NETProducercan’twriteorextendC#-styleframeworksBut.NETAccessibleeverythingthatiswrittencanbeaccessedfrom.NETeverythingin.NETcanbeaccessedsomehowTheaim:NotforwindowsorASP.NETprogrammingButforprimarilyMLprogramming,with.NETinmindAllMLcodeimmediatelyaccessiblefrom.NETSemanticsandoperationalbehaviourofMLcode/objectsmustbeeasilyunderstoodbyC#programmersF#=Caml.NETCoreCamlisjustasimplemixedimperative/functionalprogramminglanguageFunctional:e.g.“TypeInference+Functionsasvalues+Typeparameters+Simpledata”makesiterationabstractioneasyAlso:built-in“deep”equality,lists,controlledrecursionImperative:Mutabledata,while/forloops,arrays“Pointer”equality(i.e.allocationsemanticsisvisibleformutabledata)ExceptionsF#=Caml.NETletiterfarr:('a-unit)-'aarray=letlen=lengtharrinfori=0tolen-1dofarr.(i)doneletrecmapfx=matchxwith|[]-[]|(h::t)-fh::mapfttype(‘a,‘b)tree=|Empty|Nodeof(‘a,’b)nodeand(‘a,’b)node={key:‘a;val:‘b;left:(‘a,‘b)tree;right:(‘a,‘b)tree;height:int}type(‘a,‘b)mtree=|Empty|Nodeof(‘a,’b)mnodeand(‘a,’b)mnode={key:‘a;mutableval:‘b;left:(‘a,‘b)tree;right:(‘a,‘b)tree;height:int}F#=Caml.NETPolymorphichashing,equality,comparisonval(=):‘a-‘a-boolval(==):‘a-‘a-boolvalcompare:‘a-‘a-intval():‘a-‘a-boolvalhash:‘a-intvaloutput_val:out_channel-‘a-unitvalinput_val:in_channel-‘aPolymorphicbinaryI/Oi.e.hashandequality“automaticallyavailable”fornon-cyclicCamltypesi.e.blasttheobjectgraphtodiskSignaturefilespervasives.mli:valabs:int-intvalstring_of_float:float-string...typeout_channelvalopen_out_bin:string-out_channelvaloutput_value:‘a-out_channel-unitMulti-patternmatchingletfindr=matchxwith|A|B|C_-x|D(_,ty1,_)|E(ty1)-combinety1xF#OCaml.NETNoobjects(bettertofitwith.NET)Nolabels/defaults(theremustbebettersolutions…)Nofunctors(muchcomplexity,littleaddedvalue)AlsoNoocamllex/ocamlyaccNoocamlp4(macroprocessor)Noocamldep(dependencyanalyzer)Usingthecompilerbin\fsc.exefoo.ml-cCompileonly(produce.cno/.cni)-aBuildaDLL-gDebug.Canrunagainstsamelibrary(unlessyouwanttodebugthelibrary)-OEnablecross-moduleoptimization--unverifiableFasterclosures,nostupidcasts,differentlibraryneededHighfidelity,Binarycompatibility&VersioningMostMLcompilerscannotcreateDLLsatallOrcanonlycreateDLLswhoseinterfaceisCorCOMHighFidelity:CanIaccessanMLDLLfromanMLEXEinacompletelytransparentway?BinaryCompatibility:CanIchangetheinternalsofaDLLanduseitinplaceofexistingDLLs?OCaml:No.F#:Yes.MS-ILcompiledinterfacesarestableCaveat:cross-moduleinliningmustnotbeusedbyclientDLLs(i.e.donotship.cnxfilestoclients)Versioning:CanyouaddfunctionalitytoaDLLanduseitinplaceofexistingDLLs?OCaml:No.F#:Some,e.g.canadd(visible)bindings,canadd(visible)types.Evenwithcross-moduleinlining.Part2:Interop,LanguageDesignLanguageDesignChoicesImmutableUnicodestringsandwcharsSignaturesarecompilationunitboundariesNOTmodule-valueconstraintsCanhide“generated”MLtypesCan’tconstrainpolymorphismCanandshouldrevealaritiestypemystringtypemyrecdtypedatatypecsdatatypecsdata2valx:intlistvalx2:intlistvalf1:int-int-intvalf2:int-(int-int)valf3:int-int-intvalf4:int-(int-int)typemystring=MyStringofstringlisttypemyrecd={a:int;b:string}typedata=OtherData.datatypecsdata=(#“CSharpProgram.data”)typecsdata2=CSDataof(#“CSharpProgram.data”)letx=([]:intlist)letx2=([]:‘alist)letf1xy=x+yletf2xy=x+yletf3x=print“hello”;(funy-x+y)letf
本文标题:F # Another ML compiler for NET
链接地址:https://www.777doc.com/doc-5473636 .html