480 likes | 500 Views
This talk explores the challenges of information-richness for programming languages and introduces F# and its Type Providers as a solution. Discover how F# can help simplify complex problems and handle information-rich applications.
E N D
Reconsidering Strongly Typed Programming in the Information Rich World Don Syme, Principal Researcher, Microsoft Research, UK
F# and Open Source F# 2.0 compiler+library open source drop Apache 2.0 license Runs on Linux, Mac, Windows, Browser
F# for the Browser & Web www.tryfsharp.org (F# Console + Tutorials) pitfw.posterous.com (F# to JS/HTML5, Community) websharper.com (F# to JS/HTML5, Product)
Proposition 4This is a problem(especially for strongly typed programming)
With F# we want to help fix this…The mechanism we’re adding to F# is called Type Providers
LINQ + Type Providers= Language Integrated Data and Services
Two aims todayDemonstrate what we’re doing in F# 3.0Explore the ramifications of information-richness for languages, engineering and tooling
F# is… ...a practical,strongly-typed ,functional-first ,supportedlanguagethat allows you to write simplecodeto solve complexproblems.
Simplicity: Functions as Values OO F# abstract class Command { public virtual void Execute(); } abstract class RoverCommand: Command { protected Rover Rover { get; private set; } public RoverCommand(MarsRoverrover) { this.Rover = rover; } } class BreakCommand: RoverCommand { public BreakCommand(Rover rover) : base(rover) { } public override void Execute() { Rover.Rotate(-5.0); } } class TurnLeftCommand : RoverCommand { public TurnLeftCommand(Rover rover) : base(rover) { } public override void Execute() { Rover.Rotate(-5.0); } } type Command = Command of (Rover -> unit) let BreakCommand= Command(fun rover -> rover.Accelerate(-1.0)) let TurnLeftCommand= Command(fun rover -> rover.Rotate(-5.0<degs>))
Simplicity: Functional Data C# Tuple<U,T> Swap<T,U>(Tuple<T,U> t) { return new Tuple<U,T>(t.Item2, t.Item1) } ReadOnlyCollection<Tuple<T,T,T>> Rotations<T>(Tuple<T,T,T> t) { new ReadOnlyCollection<int> (new Tuple<T,T,T>[] { new Tuple<T,T,T>(t.Item1,t.Item2,t.Item3); new Tuple<T,T,T>(t.Item3,t.Item1,t.Item2); new Tuple<T,T,T>(t.Item2,t.Item3,t.Item1); }); } int Reduce<T>(Func<T,int> f,Tuple<T,T,T> t) { return f(t.Item1) + f(t.Item2) + f (t.Item3); } F# let swap (x, y) = (y, x) let rotations (x, y, z) = [ (x, y, z); (z, x, y); (y, z, x) ] let reduce f (x, y, z) = f x + f y + f z
The Big Trends DATA WEB MULTICORE
A ChallengeTask #1: A Chemistry Elements Class LibraryTask #2: Biology…Task #3: Repeat for all Sciences, Businesses, …
The Data Deluge Organized Data Enormous number of “types” Very interlinked, internally and externally Includes semantic webs, ontologies, web data services, traditional DBs…
A Type Provider is….“A compile-time component that provides a computed space of types and methods on-demand …”“A compiler plug-in…”“An adaptor between data/services and the .NET type system…”
// Freebase.fsx // Example of reading from freebase.com in F# // by Jomo Fisher #r "System.Runtime.Serialization" #r "System.ServiceModel.Web" #r "System.Web" #r "System.Xml" open System open System.IO open System.Net open System.Text open System.Web open System.Security.Authentication open System.Runtime.Serialization [<DataContract>] type Result<'TResult> = { [<field: DataMember(Name="code") >] Code:string [<field: DataMember(Name="result") >] Result:'TResult [<field: DataMember(Name="message") >] Message:string } [<DataContract>] type ChemicalElement = { [<field: DataMember(Name="name") >] Name:string [<field: DataMember(Name="boiling_point") >] BoilingPoint:string [<field: DataMember(Name="atomic_mass") >] AtomicMass:string } let Query<'T>(query:string) : 'T = let query = query.Replace("'","\"") let queryUrl = sprintf "http://api.freebase.com/api/service/mqlread?query=%s" "{\"query\":"+query+"}" let request : HttpWebRequest = downcast WebRequest.Create(queryUrl) request.Method <- "GET" request.ContentType <- "application/x-www-form-urlencoded" let response = request.GetResponse() let result = try use reader = new StreamReader(response.GetResponseStream()) reader.ReadToEnd(); finally response.Close() let data = Encoding.Unicode.GetBytes(result); let stream = new MemoryStream() stream.Write(data, 0, data.Length); stream.Position <- 0L let ser = Json.DataContractJsonSerializer(typeof<Result<'T>>) let result = ser.ReadObject(stream) :?> Result<'T> if result.Code<>"/api/status/ok" then raise (InvalidOperationException(result.Message)) else result.Result let elements = Query<ChemicalElement array>("[{'type':'/chemistry/chemical_element','name':null,'boiling_point':null,'atomic_mass':null}]") elements |> Array.iter(fun element->printfn "%A" element) How would we do this previously?
Note: F# itself still contains no dataOpen architectureYou can write your own type provider
How do we mediate today? Untyped, non-navigable UI designs WCF services WebRequest XmlReader ReadJSON SqlReader ... objects strings bytes Resources program Web services Databases
How do we mediate today? Codegen, Codegen, Codegen!! Does not scale, very clumsy UI designs xaml designer WCF services svcutil.exe types + code Resources resgen.exe program OData datasvcutil.exe Databases edmgen.exe yacg.exe ...
OData demo
Part 3How F# Type Providers have changed my view of programming languages
Observation 1Huge Information Spaces can be Software Components
Observation 2Type-provider architectures allow languages to avoid l.c.d. effectsRich type systems, importing rich metadata where it exists
Observation 3Languages should also be measured by their effectiveness for working with external information spaces“Programs manipulate in-memory objects” “Programs manipulate external information spaces”Example: queries, JSON, XML, type providers, async…
Observation 4Programming Type Systemsv.Information Space MetadataExamples: Types, Schema, Constraints, Units of Measure, Security Information, Documentation, Definition Locations, Help , Provenance, Privacy, Ratings, Rankings, Search…
An invitation to partner with Microsoft Research • MSRwant to partner with experts to fuse functional programming + databases + semantic web + … • We’re looking for people who want to be creative in using and extending languages like F# • Help bring data-rich functional programming to '000s of programmers • Experiments can be browser-hosted and delivered, including code editing
SummaryThe world is information richOur programming needs to be information-rich tooInformation-richness changes how we think about programming languages
Thank You!Questions? Contacts: dsyme@microsoft.comwww.fsharp.net, http://blogs.msdn.com/b/fsharpteamTwitter: @dsyme, #fsharp
What Changes? • Traditionally: ├ e : 0 = M1 : 1, … M n : n • The “initial environment” for a compilation includes a set of providers. Typically each of these are related to one or more external data/service/library stores of the same kind, e.g. 0 = (D0,…,Dn, D ), …
SQL type SQL = SqlDataConnection<"Server='.\\SQLEXPRESS'.."> Fluent, Typed Access To SQL
SharePoint type EmeaSite = SharePointSite<"http://myemea/"> Fluent, Typed Access To SharePoint Lists
Type Providers: Applications • …web data • …data markets • …network management • …a spreadsheet • …web services • …CRM data • …social data • …SQL data • …XML data • ... strongly typed without explicitcodegen extensible, open
Whitespace Matters letcomputeDerivative f x = letp1 = f (x - 0.05) letp2 = f (x + 0.05) (p2 – p1) / 0.1
Objects + Functional type Vector2D (dx:double, dy:double) = let d2 = dx*dx+dy*dy member v.DX = dx member v.DY = dy member v.Length = sqrt d2 memberv.Scale(k) = Vector2D (dx*k,dy*k)
Example #1 (Power Company) I have written an application to balance the national power generation schedule… for an energy company. ...the calculation engine was written in F#. The use of F# to address the complexity at the heart of this applicationclearly demonstrates a sweet spot for the language … algorithmic analysis of large data sets. Simon Cousins (Eon Powergen)
What Changes? • Type soundness becomes relative to • the stability of schema type mappings as data sources change • the soundness of the erasure functions of the providers • A program that checks today may not check tomorrow • Depending on the data store, access rights, … • This is true today anyway, because libraries change • It is also true for any external interop based on code generation • Practically speaking, its also true for external interop based on dynamic techniques • Formally, very reminiscent of binary and source compatibility analyses • Memory safety and IL-type safety are unaffected
Ramification 0Software Component v. Information Space Component
Ramification 1Software Engineering v. Information Space EngineeringExamples: Soundness, Stability, Performance, Reliability
Ramification 3Software Component Markets v.Information Space MarketsExample: Windows Azure Marketplace