Introduction Background Goal Current Status Results Conclusion . . Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog Spyros Hadjichristodoulou 1 Stony Brook University Computer Science Department ICLP 2012 Doctorate Consortium Spyros Hadjichristodoulou Implementation of a Gradual Polymorphic Type System with Standard Subtyping for Prolog 1 / 23
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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)).
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,