Top Banner
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
59
Welcome message from author
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
Page 1: ICLP-DC12

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

Page 2: ICLP-DC12

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

Page 3: ICLP-DC12

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

Page 4: ICLP-DC12

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

Page 5: ICLP-DC12

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

Page 6: ICLP-DC12

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

Page 7: ICLP-DC12

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

Page 8: ICLP-DC12

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

Page 9: ICLP-DC12

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

Page 10: ICLP-DC12

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

Page 11: ICLP-DC12

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

Page 12: ICLP-DC12

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

Page 13: ICLP-DC12

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

Page 14: ICLP-DC12

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

Page 15: ICLP-DC12

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

Page 16: ICLP-DC12

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

Page 17: ICLP-DC12

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

Page 18: ICLP-DC12

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

Page 19: ICLP-DC12

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

Page 20: ICLP-DC12

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

Page 21: ICLP-DC12

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

Page 22: ICLP-DC12

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

Page 23: ICLP-DC12

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

Page 24: ICLP-DC12

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

Page 25: ICLP-DC12

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

Page 26: ICLP-DC12

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

Page 27: ICLP-DC12

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

Page 28: ICLP-DC12

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

Page 29: ICLP-DC12

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

Page 30: ICLP-DC12

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

Page 31: ICLP-DC12

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

Page 32: ICLP-DC12

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

Page 33: ICLP-DC12

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

Page 34: ICLP-DC12

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

Page 35: ICLP-DC12

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

Page 36: ICLP-DC12

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

Page 37: ICLP-DC12

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

Page 38: ICLP-DC12

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

Page 39: ICLP-DC12

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

Page 40: ICLP-DC12

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

Page 41: ICLP-DC12

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

Page 42: ICLP-DC12

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

Page 43: ICLP-DC12

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

Page 44: ICLP-DC12

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

Page 45: ICLP-DC12

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

Page 46: ICLP-DC12

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

Page 47: ICLP-DC12

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

Page 48: ICLP-DC12

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

Page 49: ICLP-DC12

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

Page 50: ICLP-DC12

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

Page 51: ICLP-DC12

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

Page 52: ICLP-DC12

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

Page 53: ICLP-DC12

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

Page 54: ICLP-DC12

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

Page 55: ICLP-DC12

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

Page 56: ICLP-DC12

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

Page 57: ICLP-DC12

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

Page 58: ICLP-DC12

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

Page 59: ICLP-DC12

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