Title: From Haskell via C
1From Haskell via C? to LINQ
A Personal PerspectiveErik Meijer
2Data
Presentation
Business logic
3Presentation (HaskellScript)
Business logic(HSP,XM?)
Data(HaskellDB)
4The Haskell Days
Swartz, SETL 1970 Burstall and Darlington, NPL
1977 Turner, KRC 1981 . Haskell, Python,
Scala, Write programs using mathematical ZF
set comprehensions syntax. Wadler 1989 List
comprehensions are related to the relational
calculus. Wadler 1992 List comprehensions are a
special case of monad comprehensions.
5List ComprehensionsExample
factors n x Select x lt-
1..n From , n mod x 0 Where
isPrime n factors n 1,n primes n
p p lt- 2..n, isPrime p
6List ComprehensionsSimplified translation
e True e e q e
q, True e b, Q filter b e Q
e x lt- l, Q concatMap (\x -gt e Q
) l e let decls, Q let decls in e
Q
Syntactic sugar over standard list operations
7Monad ComprehensionsExample
words IO Stringwords do putStr enter
a value x lt- getLine return (words
x) class Monad m where (gtgt) m a -gt
(a -gt m b) -gt m b return a -gt m a
Parametrized over type constructor
Syntactic sugar over standard monad operations
8HaskellDb Query Monad
SELECT X.FirstName, X.LastName FROM Authors AS
X WHERE X.City 'OakLand' oaklands do x
lt- table authors restrict (x!city ..
constant "Oakland") project ( au_fname
x!au_fname , au_lname x!au_lname
)
Query monad
intentional representation for expressions
9Haskell Server Pages XHTML Literals
table TABLE table ltTABLE border"1"gt
lt mkRows cells gt lt/TABLEgt cells
(Int,Int) cells (x,y) x lt- 1..16
y lt- 1..16 mkRows (Int,Int) -gt
TR mkRows map \cs -gt ltTRgtlt mkColums cs
gtlt/TRgt mkColumns (Int,Int) -gt
TD mkColums map \c -gt ltTD bgcolor(color
c)gtlt c gtlt/TDgt
Translated to universal DOM representation
ASP-style embedding
10Growing C?
streams
tuples
Unions
Content classes
XML object literals
Generalized member access SQL comprehensions
11Type System Extensions
(structural)
closures
arrays
T N T T T(,T,)
TT TT T! T? T T struct
, T m,
intersection, union
streams
tuples (rows)
12C?Query Comprehensions
String n "Wolfram" structString? Subject
subjects select it.Subject from it in inbox
where it.From n
Compiler plugins
foreach(r in select CustomerID, ContactName
from dbo.Customers where City mycity order
by ContactName)
Type inference
13Incoherence?
4711
4711
4711
T
T?
object
4711
When up-casting from T? to object we need to
unwrap (? in Whidbey)
14LINQ Project
VB 9
C 3.0
StandardQueryOperators
DLinq(relational)
XLinq(xml)
LINQ Framework
15Query Comprensions In C 3.0
var contacts from c in customers where
c.State "WA" select new c.Name, c.Phone
var contacts customers .Where(c
gt c.State "WA") .Select(c gt newc.Name,
c.Phone)
Type inference
Syntactic sugar over standard query operations
Lambda expression
16Extension Methods
IEnumerableltCustomergt
var contacts customers .Where(c gt c.State
"WA") .Select(c gt newc.Name,
c.Phone) Static class System.Query
public static IEnumerableltTgt WhereltTgt(this
IEnumerableltTgt src, FuncltT, boolgtgt
p)
Extension method for IEnumerablltTgt
17Expression Trees
TableltCustomergt
var contacts customers .Where(c gt c.State
"WA") .Select(c gt newc.Name,
c.Phone) class TableltTgt IEnumerableltTgt
public TableltTgt Where(Expression
ltFuncltT, boolgtgt p)
Intensional representation of delegate
18Anonymous types,Object initializers
var contacts from c in customers where
c.State "WA" select new c.Name, c.Phone
var Joe new Person Name Joe, Age
42, Address Street 1th St,
City Seattle
Anonymous types
Object Initializers
Constructor inference
19Query Comprensions In Visual Basic 9
SQL-style Select-From-Wherevs Yoda-style
from-where-select
Dim contacts Select c.Name, c.Phone From c
In customers Where c.State "WA"
Parity wrt to other C 3.0 features
20Application
from c in db.Customerswhere c.City
"London"select new c.Name, c.Phone
Objects
LINQ Query
SubmitChanges()
DLinq
SQL Query
SQLServer
select Name, Phonefrom customerswhere city
'London'
21DLinqO/R Mapping
Mapping through attributes Manually authored or
tool generated DataContext allows access to
relational data as objects Automatic change
trackingAuto-generated updates using optimistic
concurrency Works with existing infrastructure
(ADO.Net) Integrates with System.Transactions,
SQL pass-through, returning objects from SQL
queries.
22XML DOM
Dim PO As New XmlDocument Dim purchaseOrder As
XmlElement _ PO.CreateElement("purchaseOrder"
) PO.AppendChild(purchaseOrder) Dim orderDate As
XmlAttribute PO.CreateAttribute("orderDate"
) orderDate.Value "1999-10-20" purchaseOrder.Att
ributes.Append(orderDate) Dim shipTo As
XmlElement PO.CreateElement("shipTo") purchaseOr
der.AppendChild(shipTo) Dim country As
XmlAttribute PO.CreateAttribute("country") count
ry.Value "US" shipTo.Attributes.Append(country)
What does this program do?
23XLinq API
Functional (expression-based) construction
Dim Item _New XElement("item", _ New
XAttribute("partNum", "926-AA"), _ New
XElement("productName", "), _ New
XElement("quantity", 1), _ New
XElement("price", 39.98), _ New
XElement("shipDate", "1999-05-21"))))
Context-free (no document scope)
Simple
24Dim PO ltpurchaseOrder orderDate(System.DateTime
.Today)gt ltshipTo country"US"gt
ltnamegtAlice Smithlt/namegt ltstreetgt123 Maple
Streetlt/streetgt ltcitygtMill Valleylt/citygt
ltstategtCAlt/stategt ltzipgt90952lt/zipgt
lt/shipTogt lt BillTo gt ltitemsgt
lt Select ltitem partNum(O.PartID)gt
ltproductNamegt lt O.Product gt
lt/productNamegt ltquantitygt lt
O.Quantity gt lt/quantitygt
ltpricegt lt O.Price gt lt/pricegt
lt/itemgt From O In Orders Where
O.Name "Robert Smith" gt lt/itemsgt
lt/purchaseOrdergt
Translated to XLinq constructor calls
ASP-style embedding
25Late binding over XML
Child axis BillTo.street ?
BillTo.Elements(street) Attribute axis
BillTo._at_country ? BillTo.Attribut(country)
Descendants axis PO...item ?
PO.Descendants(item)
26Static Duck Typing(typeclasses--)
Extension Customer For XElement Property Street
As String Get Return CStr(Me.Element(st
reet)) End Get End Property Property
country As String Get Return
CStr(Me.Attribute(street)) End Get End
Property Property Street As IEnumerable(Of
Item) Get Return CType(Me.Descendants(i
tem), Item) End Get End PropertyEnd
Extension
Named extension
Compile-time cast
27(No Transcript)
28(No Transcript)