Upload
spyros-hadjichristodoulou
View
179
Download
1
Tags:
Embed Size (px)
Citation preview
Introduction Background Goal Current Status Results Conclusion
.
.
Implementation of a Gradual
Polymorphic Type System with
Standard Subtyping for Prolog
Spyros Hadjichristodoulou1
Stony Brook UniversityComputer Science Department
ICLP 2012 Doctorate Consortium
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 1 / 23
Introduction Background Goal Current Status Results Conclusion
Types in Prolog?
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 2 / 23
Introduction Background Goal Current Status Results Conclusion
Types in Prolog?
.Theorem (Most Prolog Books)...Prolog is an untyped language...
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 2 / 23
Introduction Background Goal Current Status Results Conclusion
Types in Prolog?
.Theorem (Most Prolog Books)...Prolog is an untyped language...
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 2 / 23
Introduction Background Goal Current Status Results Conclusion
Types in Prolog?
.Theorem (Most Prolog Books)...Prolog is an untyped language...
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 2 / 23
Introduction Background Goal Current Status Results Conclusion
Types in Prolog?
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 3 / 23
Introduction Background Goal Current Status Results Conclusion
Types in Prolog?
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 3 / 23
Introduction Background Goal Current Status Results Conclusion
Types in Prolog?
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 3 / 23
Introduction Background Goal Current Status Results Conclusion
Our vision
A Gradual Polymorphic type system with Subtyping
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 4 / 23
Introduction Background Goal Current Status Results Conclusion
Our vision
A Gradual Polymorphic type system with Subtyping
..Untyped.Program
.Possibly.Unsafe
.Type.Inference
.More.Types.Added
.Type.Checking.(user)
.Type.Safe
.Gradual,.Incremental
.Process
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 4 / 23
Introduction Background Goal Current Status Results Conclusion
The Mycroft-O’Keefe Type System
.Example 1..
.
:- type list(A) ---> [] ; [A|list(A)].:- pred append(list(A),list(A),list(A)).
append([],L,L).append([X|L1],L2,[X|L3]) :- append(L1,L2,L3).
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 5 / 23
Introduction Background Goal Current Status Results Conclusion
The Mycroft-O’Keefe Type System
.Example 1..
.
:- type list(A) ---> [] ; [A|list(A)].:- pred append(list(A),list(A),list(A)).
append([],L,L).append([X|L1],L2,[X|L3]) :- append(L1,L2,L3).
Prolog code
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 5 / 23
Introduction Background Goal Current Status Results Conclusion
The Mycroft-O’Keefe Type System
.Example 1..
.
:- type list(A) ---> [] ; [A|list(A)].:- pred append(list(A),list(A),list(A)).
append([],L,L).append([X|L1],L2,[X|L3]) :- append(L1,L2,L3).
Prolog code Type constructor
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 5 / 23
Introduction Background Goal Current Status Results Conclusion
The Mycroft-O’Keefe Type System
.Example 1..
.
:- type list(A) ---> [] ; [A|list(A)].:- pred append(list(A),list(A),list(A)).
append([],L,L).append([X|L1],L2,[X|L3]) :- append(L1,L2,L3).
Prolog code Type constructor Type signature
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 5 / 23
Introduction Background Goal Current Status Results Conclusion
Other works
Towards Typed Prolog (Schrijvers, Santos Costa, Wielemaker,Demoen - 2009)
Type inference by abstract interpretation (Barbuti,Giacobazzi1990s)
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 6 / 23
Introduction Background Goal Current Status Results Conclusion
Other works
Towards Typed Prolog (Schrijvers, Santos Costa, Wielemaker,Demoen - 2009)
Type inference by abstract interpretation (Barbuti,Giacobazzi1990s)
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 6 / 23
Introduction Background Goal Current Status Results Conclusion
Other works
Towards Typed Prolog (Schrijvers, Santos Costa, Wielemaker,Demoen - 2009)
Type inference by abstract interpretation (Barbuti,Giacobazzi1990s)
.
.Predicate.Abstract
.Representation.Abstraction
.fix
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 6 / 23
Introduction Background Goal Current Status Results Conclusion
Goal
XSB
Compile Load Execute
Disk
.P
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 7 / 23
Introduction Background Goal Current Status Results Conclusion
Goal
User1
?
XSB
Compile Load Execute
Disk
.P
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 7 / 23
Introduction Background Goal Current Status Results Conclusion
Goal
User1
?
XSB
Compile Load Execute
TypeAnalysis
XSB
Disk
.P2�
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 7 / 23
Introduction Background Goal Current Status Results Conclusion
Goal
User1
?
XSB
Compile Load Execute
TypeAnalysis
3
6
XSB
Disk
.P2�
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 7 / 23
Introduction Background Goal Current Status Results Conclusion
Goal
User1
?
XSB
Compile
4
-
Load Execute
TypeAnalysis
3
6
XSB
Disk
.P2�
.xwam
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 7 / 23
Introduction Background Goal Current Status Results Conclusion
Goal
User1
?
XSB
Compile
4
-
Load Execute
TypeAnalysis
3
6
XSB
Disk
.P2�
.xwam5
6
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 7 / 23
Introduction Background Goal Current Status Results Conclusion
Goal
User1
?
XSB
Compile
4
-
Load 6 - Execute
TypeAnalysis
3
6
XSB
Disk
.P2�
.xwam5
6
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 7 / 23
Introduction Background Goal Current Status Results Conclusion
Extensions (1)
⊤
integer float
⊥
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 8 / 23
Introduction Background Goal Current Status Results Conclusion
Extensions (1)
⊤
?
number
�
integer
JJ
JJ
float
JJ
JJ
�
⊥
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 8 / 23
Introduction Background Goal Current Status Results Conclusion
Extensions (1)
⊤
?
number
�
integer
JJ
JJ
float
JJ
JJ
�
⊥
.Example 2..
.
foo(42).foo(4.2).
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 8 / 23
Introduction Background Goal Current Status Results Conclusion
Extensions (1)
⊤
?
number
�
integer
JJ
JJ
float
JJ
JJ
�
⊥
.Example 2..
.
foo(integer).foo(float).
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 8 / 23
Introduction Background Goal Current Status Results Conclusion
Extensions (1)
⊤
?
number
�
integer
JJ
JJ
float
JJ
JJ
�
⊥
.Example 2..
.foo(number).
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 8 / 23
Introduction Background Goal Current Status Results Conclusion
Extensions (2)
⊤
number atom
�
integer
JJJfloat
JJ
JJ ?
��������
⊥
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 9 / 23
Introduction Background Goal Current Status Results Conclusion
Extensions (2)
⊤
?atomic
�
number
JJJatom
�
integer
JJJfloat
JJ
JJ ?
��������
⊥
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 9 / 23
Introduction Background Goal Current Status Results Conclusion
Extensions (2)
⊤
?atomic
�
number
JJJatom
�
integer
JJJfloat
JJ
JJ ?
��������
⊥
.Example 3..
.
foo(42).foo(4.2).foo(bar).
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 9 / 23
Introduction Background Goal Current Status Results Conclusion
Extensions (2)
⊤
?atomic
�
number
JJJatom
�
integer
JJJfloat
JJ
JJ ?
��������
⊥
.Example 3..
.
foo(integer).foo(float).foo(atom).
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 9 / 23
Introduction Background Goal Current Status Results Conclusion
Extensions (2)
⊤
?atomic
�
number
JJJatom
�
integer
JJJfloat
JJ
JJ ?
��������
⊥
.Example 3..
.
foo(number).
foo(atom).
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 9 / 23
Introduction Background Goal Current Status Results Conclusion
Extensions (2)
⊤
?atomic
�
number
JJJatom
�
integer
JJJfloat
JJ
JJ ?
��������
⊥
.Example 3..
.
foo(atomic).
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 9 / 23
Introduction Background Goal Current Status Results Conclusion
Implementation
.General Idea..
.
1: repeat2: For a predicate H, let {H1, . . . , Hn} be the heads of the
clauses that define it3: for all Hi ∈ {H1, . . . , Hn} do4: Let Hi : −Bi be the respective clause of H5: for all B ∈ Bi do6: Find the type of B and accumulate variable-type
bindings for the entire clause7: end for8: The new type for H is the intersection of the types found
for each Hi
9: end for10: until No change is made to the inferred type for H
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 10 / 23
Introduction Background Goal Current Status Results Conclusion
Remark
..A1
.A2 ..T1
.T2
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 11 / 23
Introduction Background Goal Current Status Results Conclusion
Attempt #1
.Find type for a single clause..
.
type inference(Head,AssumList,Type) :-clause(Head,Body),get type(Head,Body,AssumList,PredType),
if unify with occurs check(Type,PredType) thensucceed
elsethrow error
end if
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 12 / 23
Introduction Background Goal Current Status Results Conclusion
Attempt #1
.Inner fixed-point..
.
type inference batch(Head,AssumList,Type) :-findall(TypeInf,type inference(Head,AssumList,TypeInf),TList),
Unify all elements of TList wich each other to get Type
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 13 / 23
Introduction Background Goal Current Status Results Conclusion
Attempt #1
.Outer fixed-point..
.
do type inference batch(PList,AListIn,AListOut):-findall(TypeInf,
(member(Head,PList),type inference batch(Pred,AListIn,TypeInf)),AListTemp),
if AListIn != AListTemp thendo type inference(PList,AListTemp,AListOut)
elseAListOut = AListIn
end if
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 14 / 23
Introduction Background Goal Current Status Results Conclusion
Attempt #2
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 15 / 23
Introduction Background Goal Current Status Results Conclusion
Attempt #2
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 15 / 23
Introduction Background Goal Current Status Results Conclusion
Attempt #2
.|?- f(X)...
.
f(21).f(42).f(84).
.Global Table.. .
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 15 / 23
Introduction Background Goal Current Status Results Conclusion
Attempt #2
.|?- f(X)...
.
f(21).f(42).f(84).
.Global Table...f(21).
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 15 / 23
Introduction Background Goal Current Status Results Conclusion
Attempt #2
.|?- f(X)...
.
f(21).f(42).f(84).
.Global Table..
.
f(21).f(42).
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 15 / 23
Introduction Background Goal Current Status Results Conclusion
Attempt #2
.|?- f(X)...
.
f(21).f(42).f(84).
.Global Table..
.
f(21).f(42).f(84).
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 15 / 23
Introduction Background Goal Current Status Results Conclusion
Attempt #3
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 16 / 23
Introduction Background Goal Current Status Results Conclusion
Attempt #3
Answer Subsumption
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 16 / 23
Introduction Background Goal Current Status Results Conclusion
Attempt #3
.|?- f(X)...
.
f(21).f(42).f(84).
.Global Table.. .
Answer SubsumptionLattice operation: max/3
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 16 / 23
Introduction Background Goal Current Status Results Conclusion
Attempt #3
.|?- f(X)...
.
f(21).f(42).f(84).
.Global Table...f(21). Answer Subsumption
Lattice operation: max/3
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 16 / 23
Introduction Background Goal Current Status Results Conclusion
Attempt #3
.|?- f(X)...
.
f(21).f(42).f(84).
.Global Table...f(42). Answer Subsumption
Lattice operation: max/3
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 16 / 23
Introduction Background Goal Current Status Results Conclusion
Attempt #3
.|?- f(X)...
.
f(21).f(42).f(84).
.Global Table...f(84). Answer Subsumption
Lattice operation: max/3
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 16 / 23
Introduction Background Goal Current Status Results Conclusion
Attempt #3
.Type inference: tabling with answer subsumption..
.
type inference n(Head,Type) :-copy term1(Head,Type),numbervars(Type,0, ).
type inference n(Head,Type) :-copy term(Head,Head1),clause(Head1,Body),get env(Body,Env),compute type(Env,Head1,Type1),copy term(Type1,Type),numbervars(Type,0, ).
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 17 / 23
Introduction Background Goal Current Status Results Conclusion
Attempt #3
.Lattice operation: unification..
.
type unify(X,Y,X1) :-unnumbervars(X,0,X1),unnumbervars(Y,0,Y1),X1 = Y1,numbervars(X1,0, ).
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 18 / 23
Introduction Background Goal Current Status Results Conclusion
Results (1)
Name LOC # Def # Inf T(s)lib/pairlist.P 125 10 10 0.006lib/lists.P 515 59 59 0.022
lib/ugraphs.P 850 90 90 0.102lib/assoc xsb.P 566 49 48 0.029lib/ordsets.P 422 39 39 0.023
examples/tree1k.P 2046 1 1 0.105
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 19 / 23
Introduction Background Goal Current Status Results Conclusion
Results (2)
.test.P..
.
:- type natural ---> 0 ; s(natural).
formula(0).formula(s(0)).formula(s(s(0))).formula(0 + s(0)).formula(s(s(0)) - s(0)).
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 20 / 23
Introduction Background Goal Current Status Results Conclusion
Results (2)
.Result..
.
|?- infer constructors(’test.P’,formula( )).
formula(natural).formula(+(natural,natural)).formula(-(natural,natural)).
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 21 / 23
Introduction Background Goal Current Status Results Conclusion
What’s next?
..Test/Debug
.Modules
.Metapredicates
.Declaration Free
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 22 / 23
Introduction Background Goal Current Status Results Conclusion
Thanks for your attention :-)
Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 23 / 23