A Funtoril Query Lnguge Ryn Wisnesky, Dvid Spivk Deprtment of Mthemtis Msshusetts Institute of Tehnology {wisnesky, dspivk}@mth.mit.edu Presented t Boston Hskell April 16, 2014
Outline Introdution to FQL. FQL is dtse query lnguge sed on tegory theory. But, there will e no tegory theory in this tlk. How to progrm FQL using Hskell. FQL provides n lterntive semntis for Hskell progrms. If you n progrm Hskell, you n progrm FQL. Demo of the FQL E. Projet wepge: tegorildt.net/fql.html 2 / 30
Introdution to FQL In FQL, dtse shem is speil kind of entity-reltionship (ER) digrm. mnger Emp worksin seretry Dept first lst nme Emp.mnger.worksIn Emp.worksIn Emp mgr works first lst 101 103 q10 Al Akin 102 102 x02 Bo Bo 103 103 q10 Crl Cork Dept.seretry.worksIn Dept Dept se nme q10 102 CS x02 101 Mth 3 / 30
Introdution to FQL mnger Emp worksin seretry Dept first lst nme Emp.mnger.worksIn Emp.worksIn Dept.seretry.worksIn Dept Eh lk node represents n entity set (of s). Eh direted edge represents foreign key. Eh open irle represent n ttriute. Dt integrity onstrints re pth equlities. Dt is stored s tles in the ovious wy. 4 / 30
Why FQL? FQL is lnguge for mnipulting the shems nd instnes just defined. But you n lso mnipulte suh shems nd instnes using SQL. We ssert tht, euse of its tegoril roots, FQL is etter lnguge for doing so. FQL is dtse t time, not tle t time. FQL opertions neessrily respet onstrints. Unlike SQL, FQL is expressive enough to e used for informtion integrtion (see ppers). Prts of FQL n run on SQL, nd vie vers. 5 / 30
FQL Bsis A shem mpping F : S Ñ T is onstrint-respeting mpping: nodespsq Ñ nodespt q edgespsq Ñ pthspt q nd it indues three dt migrtion opertions: F : T -inst Ñ S-inst (like projetion) ΣF : S-inst Ñ T -inst (like union) ΠF : S-inst Ñ T -inst (like join) 6 / 30
(Projet) Nme Nme Slry N1 N2 F ÝÝÝÑ N Slry Age Age N1 Nme Slry 1 Bo $250 2 Sue $300 3 Alie $100 N2 Age 1 20 2 20 3 30 F ÐÝÝ N Nme Age Slry 1 Bo 20 $250 2 Sue 20 $300 3 Alie 30 $100 7 / 30
Π (Join) Nme Nme Slry N1 N2 F ÝÝÝÑ N Slry Age Age N1 Nme Slry 1 Bo $250 2 Sue $300 3 Alie $100 N2 Age 1 20 2 20 3 30 Π F ÝÝÑ N Nme Age Slry 1 Alie 20 $100 2 Alie 20 $100 3 Alie 30 $100 4 Bo 20 $250 5 Bo 20 $250 6 Bo 30 $250 7 Sue 20 $300 8 Sue 20 $300 9 Sue 30 $300 8 / 30
Σ (Union) Nme Nme Slry N1 N2 F ÝÝÝÑ N Slry Age Age N1 Nme Slry 1 Bo $250 2 Sue $300 3 Alie $100 N2 Age 1 20 2 20 3 30 Σ F ÝÝÑ N Nme Age Slry 1 Alie null $100 2 Bo null $250 3 Sue null $300 4 null 20 null 5 null 20 null 6 null 30 null 9 / 30
Foreign keys Nme Nme Slry N1 f N2 F ÝÝÝÑ N Slry Age Age N1 Nme Slry f 1 Bo $250 1 2 Sue $300 2 3 Alie $100 3 N2 Age 1 20 2 20 3 30 F ÐÝÝ Π F,Σ F ÝÝÝÝÑ N Nme Age Slry 1 Alie 20 $100 2 Bo 20 $250 3 Sue 30 $300 10 / 30
FQL Summry FQL provides dtse t time query lnguge for ertin kinds of reltionl dtses. For the tegorilly inlined, roughly: Shems re finitely-presented tegories. Shem mppings re funtors. Instnes re funtors to the tegory of sets. The instnes on ny shem form tegory. pσf, F q nd p F, Π F q re djoint funtors. 11 / 30
Progrmming FQL Shems nd Mppings using Hskell By Hskell, I men the the simply-typed λ-lulus (STLC): Types t: t :: 0 1 t ` t t ˆ t t Ñ t Expressions e: e :: v λv : t.e ee pq fst e snd e pe, eq K inl e inr e pe ` eq Equtions: fstpe, fq e sndpe, fq f pλv : t.eqf erv ÞÑ fs... Theorem: FQL shems nd mppings re model of the STLC. Given n STLC type t, you get n FQL shem rts. Given n STLC term Γ $ e : t, you get n FQL shem mpping res : rγs Ñ rts 12 / 30
Progrmming FQL Shems using Hskell The empty type, 0, (in Hskell, dt Empty = ), eomes shem with no nodes: The unit type, 1, (in Hskell, dt Unit = TT), eomes shem with one node: TT 13 / 30
Progrmming FQL Shems using Hskell Sum types, t ` t 1, (in Hskell, Either t t ), re given y ddition: inl inl inl ` d e inr d inr e Produt types, t ˆ t 1, (in Hskell, (t,t )), re given y multiplition: p,dq p,dq p,dq ˆ d e p,eq p,eq p,eq 14 / 30
Progrmming FQL Shems using Hskell Funtion types, t Ñ t 1 re given y exponentition: Ñ d e pþñd,þñd,þñdq pþñe,þñd,þñdq pþñd,þñe,þñdq pþñd,þñd,þñeq pþñe,þñe,þñdq pþñd,þñe,þñeq pþñe,þñd,þñeq pþñe,þñe,þñeq 15 / 30
Progrmming FQL Shems using Hskell Constnt types, orresponding to user defined types in Hskell, re simply shems: mnger Emp worksin seretry Dept The opertions ˆ, `, Ñ ehve orretly with respet to foreign keys. Hene, STLC types trnslte to FQL shems. 16 / 30
Progrmming FQL Mppings using Hskell In Hskell, we hve K ::. In FQL, we hve mpping K : 0 Ñ : K ÝÑ mnger Emp worksin seretry Dept In Hskell, we hve pq :: 1. In FQL, we hve mpping pq : Ñ 1: mnger Emp worksin seretry Dept pq ÝÑ TT 17 / 30
Progrmming FQL Mppings using Hskell In Hskell, we hve inl :: Ñ ` nd inr :: Ñ `. ` d e inl,inr ÝÝÝÝÑ inl inl inl inr d inr e In Hskell, we hve fst :: ˆ Ñ nd snd :: ˆ Ñ. ˆ d e fst,snd ÐÝÝÝÝ p,dq p,dq p,dq p,eq p,eq p,eq 18 / 30
Progrmming FQL Mppings using Hskell We n trnslte the other STLC opertions too: If f :: t Ñ nd g :: t Ñ, we need pf, gq :: t Ñ ˆ. This is piring. If f :: Ñ t nd g :: Ñ t, we need pf ` gq :: ` Ñ t. This is se. If f :: ˆ Ñ, we need Λf : Ñ p Ñ q. This is usully lled urry. We need ev :: p Ñ q ˆ Ñ. This is funtion pplition. All FQL opertions oey the required equtions, fstp, q sndp, q... And the FQL opertions work orretly with foreign keys. Hene, FQL mppings re model of the STLC. 19 / 30
Retrospetive STLC types nd terms, FQL shems nd mppings, nd even sets nd funtions etween them, re ll i-rtesin losed tegories. Hskell progrmmers will eventully enounter tegory theory, strting with i-rtesin losed tegories. Tht theory n e put to use in other ples, nmely dtses. In ft, s we will see next, for every FQL shem S, the tegory of S-instnes is lso i-rtesin losed. 20 / 30
Progrmming FQL Instnes nd Morphisms using Hskell By Hskell, I men the the simply-typed λ-lulus (STLC): Types t: t :: 0 1 t ` t t ˆ t t Ñ t Expressions e: e :: v λv : t.e ee pq fst e snd e pe, eq K inl e inr e pe ` eq Equtions: fstpe, fq e sndpe, fq f pλv : t.eqf erv ÞÑ fs... Theorem: For eh shem S, the FQL S-instnes nd S-homomorphisms re model of the STLC. A dtse homomorphism is mp of s to s. Given n STLC type t, you get n FQL S-instne rts. Given n STLC term Γ $ e : t, you get n FQL S-homomorphism res : rγs Ñ rts 21 / 30
Progrmming FQL Instnes using Hskell Let S e the shem f The empty type, 0, (in Hskell, dt Empty = ), eomes n S instne with no dt: f The unit type, 1, (in Hskell, dt Unit = TT), eomes n S instne with one per tle: f 1 1 1 22 / 30
Progrmming FQL Instnes using Hskell Sum types t ` t 1 re given y disjoint union: f 1 3 2 3 3 4 ` f d f inl 1 inl 3 inl 2 inl 3 inr inr inr inr inl 3 inl 4 inr inr d Produt types t ˆ t 1 re given y joining: f 1 3 2 3 3 4 ˆ f d (1,) (1,) (2,) (2,) f (3,) (3,) (3,) (3,) (3,) (3,d) (4,) (4,d) 23 / 30
Progrmming FQL Instnes using Hskell Funtion types t Ñ t 1 re given y finding ll homomorphisms: f 1 3 2 3 3 4 Ñ f d f 1 ÞÑ, 2 ÞÑ, 3 ÞÑ, 4 ÞÑ d 3 ÞÑ, 4 ÞÑ d 1 ÞÑ, 2 ÞÑ, 3 ÞÑ, 4 ÞÑ d 3 ÞÑ, 4 ÞÑ d 1 ÞÑ, 2 ÞÑ, 3 ÞÑ, 4 ÞÑ d 3 ÞÑ, 4 ÞÑ d 1 ÞÑ, 2 ÞÑ, 3 ÞÑ, 4 ÞÑ d 3 ÞÑ, 4 ÞÑ d 1 ÞÑ, 2 ÞÑ, 3 ÞÑ d, 4 ÞÑ 3 ÞÑ d, 4 ÞÑ 1 ÞÑ, 2 ÞÑ, 3 ÞÑ d, 4 ÞÑ 3 ÞÑ d, 4 ÞÑ 1 ÞÑ, 2 ÞÑ, 3 ÞÑ d, 4 ÞÑ 3 ÞÑ d, 4 ÞÑ 1 ÞÑ, 2 ÞÑ, 3 ÞÑ d, 4 ÞÑ 3 ÞÑ d, 4 ÞÑ 3 ÞÑ, 4 ÞÑ 3 ÞÑ, 4 ÞÑ d 3 ÞÑ d, 4 ÞÑ 3 ÞÑ d, 4 ÞÑ d 24 / 30
Progrmming FQL Instnes using Hskell Constnt instnes, orresponding to user defined types in Hskell, re simply instnes: f p q q r t t The opertions ˆ, `, Ñ ehve orretly with respet to foreign keys. Hene, for every shem S, STLC types trnslte to S-instnes. 25 / 30
Progrmming FQL Homomorphisms using Hskell in Hskell, we hve K ::. In FQL, we hve homomorphism K : 0 Ñ : f K ÝÑ p r f q t q t In Hskell, we hve pq :: 1. In FQL, we hve homomorphism pq : Ñ 1: f pq ÝÑ f p q q 1 1 1 r t t 26 / 30
Progrmming FQL Homomorphisms using Hskell As efore, inl : Ñ ` nd inr : Ñ ` f 1 3 2 3 3 4 ` f d inl,inr ÝÝÝÝÑ f inl 1 inl 3 inl 2 inl 3 inr inr inr inr inl 3 inl 4 inr inr d As efore, fst : ˆ Ñ nd snd : ˆ Ñ f 1 3 2 3 3 4 ˆ f d fst,snd ÐÝÝÝÝÝ (1,) (1,) (2,) (2, ) f (3,) (3,) (3,) (3,) (3,) (3,d) (4,) (4,d) 27 / 30
Retrospetive The lnguge of FQL instnes ontins ll opertions required to e model of the STLC. In ft, t the level of instnes, FQL is model of higher-order logi: types t ::... P rop expressions e ::... e e The STLC struture interts with the, Σ, Π dt migrtion opertions in nie wy, e.g,: Σ F pi ` Jq Σ F piq ` Σ F pjq Π F pi ˆ Jq Π F piq ˆ Π F piq 28 / 30
Demo of the FQL E The FQL E is n open-soure jv pplition, downlodle t tegorildt.net{fql.html It supports ll the opertions disussed ove: 0, 1, `, ˆ, Ñ for shems nd instnes, nd the dt migrtion opertions, Σ, Π. To the extent possile, ll opertions re implemented with SQL: 0, 1, `, ˆ,, Π implemented with SQL. Σ F only implementle with SQL if F hs ertin property. Ñ not implementle with SQL. Other fetures: It trnsltes from SQL to FQL. It emits RDF enodings of instnes. It omes with mny uilt-in exmples. It n e used s ommnd-line ompiler. 29 / 30
Conlusion First, we tlked out FQL, funtoril query lnguge sed on tegory theory. Shems re prtiulr ER digrms, nd instnes re reltionl tles. The, Σ, Π opertions migrte dt from one shem to nother. FQL ontins two opies of the STLC: one t the level of shems nd mppings, nd one t the level of instnes nd homomorphisms. Conlusion: Hskell, in the guise of the STLC, ours in mny res of CS outside of progrmming. Finlly, we sw demo of the FQL E. We re looking for ollortors: tegorildt.net/fql.html 30 / 30