avoiding dynamic type checking in a polymorphic logic programming language

15

Upload: francesco-magliocca

Post on 02-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

7/27/2019 Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

http://slidepdf.com/reader/full/avoiding-dynamic-type-checking-in-a-polymorphic-logic-programming-language 1/14

A v o i d i n g d y n a m i c t y p e c h e c k i n g i n a p o l y -  

m o r p h i c l o g i c p r o g r a m m i n g l a n g u a g e  

P a s c a l B r i s s e t

I R I S A  

C a m p u s U n i v e r s i t a i r e d e B e a u l i e u , F - 3 5 0 4 2 R e n n e s C e d e x , F r a n c e  

b r i s s e t @ i r i s a . f r  

A b s t r a c t  

F o r o b v i o u s e c i e n c y r e a s o n s , i t i s g o o d t o a v o i d d y n a m i c t y p e - c h e c k i n g i n  

a p r o g r a m m i n g l a n g u a g e . T h e P r o l o g t y p e s y s t e m p r o p o s e d b y M y c r o f t a n d  

O ' k e e f e h a s s o m e g o o d p r o p e r t i e s w h i c h a v o i d s u c h a r u n t i m e o v e r h e a d . O n e

o f t h i s p r o p e r t y c a l l e d   t y p e p r e s e r v i n g   o r t r a n s p a r e n c y   i s a r e s t r i c t i o n o f t h e

p o l y m o r p h i s m . T h i s r e s t r i c t i o n i s n o t c o m p a t i b l e w i t h s o m e p r o g r a m m i n g  

t e c h n i q u e s , f o r e x a m p l e w i t h h i g h e r - o r d e r p r e d i c a t e s .

C o n s i d e r i n g a t y p e s y s t e m w i t h o u t t h e t y p e p r e s e r v i n g p r o p e r t y , H a n u s

h a s i d e n t i e d s o m e c a s e s w h e r e d y n a m i c t y p e - c h e c k i n g c a n b e s t r o n g l y l i m -

i t e d . W e s h o w i n t h i s p a p e r t h a t i n H a n u s ' s o l u t i o n , t o o m a n y t y p e s a r e

s t i l l d y n a m i c a l l y r e p r e s e n t e d a n d c h e c k e d . W e p r o p o s e a n i m p l e m e n t a t i o n  

w h e r e , w i t h o u t t h e t y p e p r e s e r v i n g r e s t r i c t i o n , d y n a m i c t y p e c h e c k i n g i s

d o n e e x a c t l y f o r t y p e s w h i c h a r e n o t p r e s e r v e d   . T h i s a p p r o a c h i s u s e d i n a n  

i m p l e m e n t a t i o n o f t h e h i g h e r - o r d e r l a n g u a g e   P r o l o g .

K e y w o r d s T y p e s , p o l y m o r p h i s m , h i g h e r - o r d e r u n i c a t i o n ,   P r o l o g , d y -

n a m i c t y p e - c h e c k i n g , i m p l e m e n t a t i o n .

1 I n t r o d u c t i o n  

I t i s n o l o n g e r n e c e s s a r y t o s h o w t h e u s e f u l n e s s o f t y p e s i n a l o g i c p r o g r a m -

m i n g l a n g u a g e . A s m e n t i o n e d b y P f e n n i n g 1 8 ] , o n e o f t h e m a i n m o t i v a t i o n  

i s t h a t \ t y p e s h e l p t o d e t e c t p r o g r a m m i n g e r r o r s a t c o m p i l e - t i m e "  . F u r t h e r -

m o r e t y p e s c a n a l s o h e l p t h e c o m p i l e r t o d o s o m e o p t i m i z a t i o n s .

T y p e s y s t e m s c a n b e r o u g h l y d i v i d e d i n t o t w o c a t e g o r i e s 2 0 ] : I n a   p r e -

s c r i p t i v e   t y p e s y s t e m , t e r m s a r e i n t r i n s i c a l l y t y p e d ; I n a   d e s c r i p t i v e   t y p e

s y s t e m , t h e t e r m s a r e d e c o r a t e d w i t h t y p e s . I n t h e f o r m e r c a t e g o r y , t y p e s

a r e i n t h e d e n i t i o n o f t h e t e r m s t h u s t h e s e m a n t i c s a n d t h e i n t e r p r e t a t i o n  

o f t h e l a n g u a g e m u s t t a k e t h e s e t y p e s i n t o a c c o u n t . I n t h e l a t t e r c a t e g o r y ,

t e r m s a r e o n l y m a p p e d t o t y p e s w i t h a \ h a v e t y p e " f u n c t i o n . T h i s m a p -

p i n g g i v e s a s t r u c t u r e t o t h e m o d e l o f t h e p r o g r a m b u t d o e s n o t c h a n g e t h i s

m o d e l .

Page 2: Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

7/27/2019 Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

http://slidepdf.com/reader/full/avoiding-dynamic-type-checking-in-a-polymorphic-logic-programming-language 2/14

O n e o f o u r m o t i v a t i o n i s t o i m p l e m e n t a t y p e s y s t e m f o r   P r o l o g 1 6 ] .

  P r o l o g u s e s a t y p e d l o g i c , i . e . n e e d s   t y p e s . T h e r e f o r e w e a r e i n t e r e s t e d i n  

a p r e s c r i p t i v e t y p e s y s t e m . E v e n i f t h e m o t i v a t i o n i s t h e l a n g u a g e   P r o l o g ,

t h e i d e a s w e e x p o s e i n t h i s p a p e r c a n b e a p p l i e d t o t h e i m p l e m e n t a t i o n o f a  

p o l y m o r p h i c t y p e s y s t e m f o r s t a n d a r d P r o l o g .

E v e n w i t h a p r e s c r i p t i v e t y p e s y s t e m , t y p e s m a y n o t b e e x p l i c i t i n t h e

p r o g r a m ; t h e t y p e s m a y b e i n f e r r e d . I n t h i s c a s e , t h e p r o g r a m m e r t h i n k  

a b o u t t y p e d t e r m s , b u t w r i t e p r o g r a m s w i t h o u t t y p e s . T h e n a t y p e - c h e c k e r

a d d s t y p e s t o t h e t e r m s . I n o u r w o r k , w e h a v e c h o s e n t o h a v e t y p e d e c l a r a -

t i o n s b e c a u s e t h e s e d e c l a r a t i o n s h e l p t h e p r o g r a m m e r t o s p e c i f y h i s p r o b l e m  

c o r r e c t l y . T h e m o r e t h e u s e r g i v e s i n f o r m a t i o n a b o u t h i s p r o g r a m , t h e m o r e

p r e c i s e t h e c o m p i l e r d o e s t h e c h e c k i n g .

N u m e r o u s t y p e s y s t e m s h a v e b e e n p r o p o s e d f o r P r o l o g 1 9 ] . O n e o f t h e

m a i n f e a t u r e s a d d r e s s e d i n t h e s e t y p e s y s t e m s i s p o l y m o r p h i s m a n d o n e o f

t h e m a i n c o n s t r a i n t s i s t h a t p o l y m o r p h i s m d o e s n o t i n t r o d u c e d y n a m i c t y p e -

c h e c k i n g . T h e r e f o r e , p o l y m o r p h i s m i s r e s t r i c t e d . T h e r e s t r i c t i o n w e d i s c u s s

h e r e i s c a l l e d   t y p e p r e s e r v i n g   b y H a n u s 5 , 6 ] o r t r a n s p a r e n c y p r o p e r t y   b y H i l l

a n d T o p o r 8 ] . H a n u s s h o w e d t h a t t h i s r e s t r i c t i o n i s n o t c o m p a t i b l e w i t h  

h i g h e r - o r d e r p r o g r a m m i n g . B e c a u s e w e h a v e t h e   P r o l o g l a n g u a g e i n m i n d ,

f o r w h i c h h i g h e r - o r d e r p r o g r a m m i n g i s a s t a n d a r d t e c h n i q u e , w e c a n n o t k e e p  

t h i s r e s t r i c t i o n .

H a n u s s h o w e d t h a t s o m e d y n a m i c t y p e - c h e c k i n g i n a t y p e s y s t e m w i t h o u t

t h e r e s t r i c t i o n c a n b e s o m e t i m e s a v o i d e d . W e s h o w i n t h i s p a p e r t h a t H a n u s '

s o l u t i o n c a n b e i m p r o v e d a n d t h a t m o s t o f t h e d y n a m i c t y p e - c h e c k i n g c a n  

b e s i m p l y o m i t e d w i t h o u t a n y c o m p l e x s t a t i c a n a l y s i s . W e a l s o a d d r e s s

t h e p r o b l e m o f d y n a m i c t y p e r e p r e s e n t a t i o n i n     P r o l o g i n w h i c h t y p e s a r e

n e c e s s a r y t o c o m p u t e t h e u n i c a t i o n .

T h e p a p e r i s s t r u c t u r e d a s f o l l o w s . I n s e c t i o n 2 , a t y p e s y s t e m f o r P r o l o g  

i s p r o p o s e d . I t i s t h e n e x t e n d e d t o t h e   P r o l o g c a s e . I n s e c t i o n 3 , w e s h o w  

w h i c h t y p e s a r e s u c i e n t a t r u n t i m e . I n s e c t i o n 4 , s o m e m o d i c a t i o n s o f t h e

s t a n d a r d d a t a - s t r u c t u r e s a n d u n i c a t i o n a l g o r i t h m a r e p r o p o s e d t o t a k e t h e

t y p e s i n t o a c c o u n t . W e c o n c l u d e w i t h a d i s c u s s i o n a b o u t r e l a t e d w o r k s .

2 A T y p e S y s t e m a n d i t s P r o p e r t i e s  

I n t h i s s e c t i o n w e r e c a l l a c l a s s i c t y p e s y s t e m s f o r P r o l o g a n d     P r o l o g . W e

d i s c u s s a n d j u s t i f y t h e a d o p t i o n o f s o m e r e s t r i c t i o n s , i . e . t y p e p r e s e r v i n g  

a n d d e n i t i o n a l g e n e r i c i t y .

2 . 1 T y p e d P r o l o g  

T y p e s , T e r m s a n d F o r m u l a s A t y p e ( d e n o t e d     ,   , . . . ) i s a n e x p r e s s i o n  

b u i l t f r o m t y p e c o n s t r u c t o r s a n d t y p e v a r i a b l e s . E a c h t y p e c o n s t r u c t o r h a s

a n a r i t y ( e . g . , i n t  h a s a r i t y 0 , l i s t   h a s a r i t y 1 ) . T h e r e i s a t y p e o f a r i t y 0  

c a l l e d   o  , t h e t y p e f o r p r o p o s i t i o n s .

Page 3: Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

7/27/2019 Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

http://slidepdf.com/reader/full/avoiding-dynamic-type-checking-in-a-polymorphic-logic-programming-language 3/14

W e w r i t e  

1

   

2

i f t h e t y p e  

1

i s a n i n s t a n c e o f  

2

:  

1

=  s  (  

2

) w h e r e s  i s

a s u b s t i t u t i o n o n t y p e v a r i a b l e s . W e w r i t e  

1

 

 

2

i f  

1

a n d   

2

a r e e q u a l s t o  

a r e n a m i n g o f t h e t y p e v a r i a b l e s .

T e r m s i n o u r t y p e d P r o l o g a r e r s t - o r d e r t e r m s . E v e r y c o n s t r u c t o r f  o f

t e r m s w i t h a r i t y   n  i s d e c l a r e d w i t h a   t y p e s c h e m e  :

f : ( T 1 , . . . , T n ) - > T  

w h e r e T 1  , . . . , T n  , T  a r e a r b i t r a r y t y p e s . T  i s t h e n c a l l e d t h e r e s u l t t y p e   W e

v o l u n t a r i l y d o n o t d i s t i n g u i s h t h e t y p e d e c l a r a t i o n o f a f u n c t i o n s y m b o l f r o m  

t h e t y p e d e c l a r a t i o n o f a p r e d i c a t e . T h e r e s u l t t y p e o f a p r e d i c a t e i s o  W e

n o t e   ( f  ) t h e t y p e s c h e m e ( f r o m t h e t y p e d e c l a r a t i o n ) o f t h e f u n c t i o n   f 

A p r o g r a m i s a s e t o f c l a u s e s L    L 

1

 ̂  ̂ L 

( n  p o s s i b l y 0 ) w h e r e

L  , L 

1

, . . . , L 

a r e l i t e r a l s . A l i t e r a l i s a t e r m o f t y p e o  , i . e p  ( t

1

; : : : ; t

q

) w i t h   p  a 

p r e d i c a t e s y m b o l .

I n t h e c o n c r e t e s y n t a x , l o g i c v a r i a b l e s a n d t y p e v a r i a b l e s i d e n t i e r s s t a r t

w i t h a n u p p e r c a s e l e t t e r , f u n c t i o n a n d p r e d i c a t e s y m b o l s s t a r t w i t h a l o w -

e r c a s e l e t t e r .

T h e t y p e s y s t e m   W e b r i e y r e c a l l h e r e M y c r o f t - O ' K e e f e t y p e s y s t e m 1 5 ]

( s e e a l s o T y p e d P r o l o g 1 1 ] ) a n d g i v e m o t i v a t i o n s f o r s u c h a s y s t e m .

v a r i a b l e

?  X     ̀ X   

a t o m  

?   ̀ a   

      ( a  )

c o m p o u n d t e r m  

?   ̀ t

1

 

1

?   ̀ t

n

 

n

?   ̀ f  ( t

1

; : : : ; t

n

)  

(  

1

; : : : ;  

n

) !        ( f  )

c l a u s e

?   ̀ t

1

 

1

?   ̀ t

k

 

k

?   ̀ B 

1

o : : : ?   ̀ B 

n

?   ̀ p  ( t

1

; : : : ; t

k

)   B 

1

 ̂  ̂ B 

n

  ( p  )

 

(  

1

; : : : ;  

n

) !  o 

T h e t w o b a s i c i d e a s b e h i n d t h i s t y p e s y s t e m a r e t h a t e v e r y c l a u s e s h o u l d  

b e w e l l - t y p e d i n a n M L - l i k e f a s h i o n . F i r s t ,

T y p e s o f d i e r e n t o c c u r r e n c e s o f a c o n s t a n t a r e i n d e p e n d e n t i n -

s t a n c e s o f i t s t y p e s c h e m e (   c o n d i t i o n i n t h e a t o m   a n d  c o m p o u n d  

t e r m   r u l e s ) ,

a n d s e c o n d , t h e t y p i n g o f p r e d i c a t e s h o u l d o b e y t h e d e n i t i o n a l g e n e r i c i t y  

p r i n c i p l e   ( a l s o c a l l e d   h e a d c o n d i t i o n   i n 8 ] )

T y p e s o f b o d y o c c u r r e n c e s o f a p r e d i c a t e c o n s t a n t a r e i n d e p e n -

d e n t i n s t a n c e s o f i t s t y p e s c h e m e , w h e r e a s t y p e s o f h e a d o c c u r -

r e n c e s a r e o n l y r e n a m i n g o f t h e t y p e s c h e m e (

 

c o n d i t i o n i n t h e

c l a u s e   r u l e ) .

F o r t h e r s t i d e a , c h e c k i n g a n d i n f e r r i n g t y p e s a r e b o t h d e c i d a b l e , a s i n  

M L 1 4 ] , b u t f o r t h e s e c o n d i d e a , o n l y t y p e c h e c k i n g i s d e c i d a b l e . W i t h  

t h e d e n i t i o n a l g e n e r i c i t y p r i n c i p l e , t y p e i n f e r e n c e l e a d s t o a n o n - u n i f o r m  

Page 4: Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

7/27/2019 Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

http://slidepdf.com/reader/full/avoiding-dynamic-type-checking-in-a-polymorphic-logic-programming-language 4/14

s e m i - u n i c a t i o n p r o b l e m w h i c h h a s b e e n s h o w n t o b e u n d e c i d a b l e b y K f o u r y ,

T i u r y n a n d U r z y c z y n 1 0 ] .

O n e r e a s o n f o r s t i c k i n g t o d e n i t i o n a l g e n e r i c i t y ( o r h e a d c o n d i t i o n ) i s

t h a t i t i s t h e m o s t n a t u r a l w h e n p r e d i c a t e s a r e s e e n a s d e n i t i o n s a n d t y p e

s c h e m e s a s a b s t r a c t i o n s o f t h e d e n i t i o n s . A s o u n d a n d e a s y m o d u l a r a n a l y -

s i s o f p r o g r a m s a l s o r e q u i r e s d e n i t i o n a l g e n e r i c i t y . T h e o t h e r r e a s o n i s t h a t

d e n i t i o n a l g e n e r i c i t y c o r r e s p o n d s t o i m p l e m e n t a t i o n g e n e r i c i t y : w e d o n o t

w a n t t o t a k e i n t o a c c o u n t t h e t y p e o f t h e a r g u m e n t s o f t h e g o a l t o s e l e c t t h e

r i g h t c l a u s e . F r o m a s o f t w a r e e n g i n e e r i n g p o i n t o f v i e w i t m e a n s t h a t i f t h e

u s e r s p e c i e s a g e n e r i c t y p e f o r a p r e d i c a t e , h e a l s o g i v e s a g e n e r i c d e n i t i o n  

o f t h i s p r e d i c a t e .

F o r e x a m p l e , a s s u m e p r e d i c a t e c o n s t a n t a p p e n d   i s d e c l a r e d a s

a p p e n d : ( l i s t ( A ) , l i s t ( A ) , l i s t ( A ) ) - > o .

a n d t h a t c l a u s e

a p p e n d 1 ] 2 , 3 ] 1 , 2 , 3 ] .

i s a d d e d t o t h e s t a n d a r d d e n i t i o n o f p r e d i c a t e a p p e n d  . I t v i o l a t e s t h e

d e n i t i o n a l g e n e r i c i t y p r i n c i p l e b e c a u s e t h e t y p e o f c o n s t a n t a p p e n d   i n t h e

n e w c l a u s e ,

( l i s t ( i n t ) , l i s t ( i n t ) , l i s t ( i n t ) ) - > o  

i s a s t r i c t i n s t a n c e o f t h e t y p e s c h e m e .

A n e x p e c t e d o u t c o m e o f a t y p e t h e o r y f o r a p r o g r a m m i n g l a n g u a g e i s

a  s e m a n t i c s o u n d n e s s   r e s u l t t h a t s t a t e s t h a t \ w e l l - t y p e d p r o g r a m s c a n n o t

g o w r o n g " w h e r e \ g o i n g w r o n g " m e a n s \ t r y i n g t o s o l v e i l l - t y p e d u n i c a t i o n  

p r o b l e m s " . T h e i n t e r e s t o f s u c h a r e s u l t i s t o g i v e f o r m a l g r o u n d s f o r n o t

r e p r e s e n t i n g t y p e s a t r u n - t i m e . W e w i l l s e e i n t h e n e x t s e c t i o n t h a t f o r

p r a g m a t i c p r o g r a m m i n g r e a s o n s , i t i s n o t p o s s i b l e t o h a v e t h i s p r o p e r t y i n  

a p o l y m o r p h i c t y p e s y s t e m f o r P r o l o g .

2 . 1 . 1 T y p e P r e s e r v i n g a n d H i g h e r O r d e r P r o g r a m m i n g  

T h e t y p e d e c l a r a t i o n l a n g u a g e s o f M L o r T y p e d P r o l o g s h a r e a c o m m o n  

r e s t r i c t i o n t h a t e n s u r e s t h e t y p e p r e s e r v i n g p r o p e r t y   7 ] :

E v e r y t y p e v a r i a b l e i n a t y p e s c h e m e s h o u l d a p p e a r i n t h e r e s u l t

t y p e .

F o r e x a m p l e i n M L , t y p e v a r i a b l e s o f a t y p e d e c l a r a t i o n m u s t o c c u r i n t h e

l e f t s i d e : t h e d e c l a r a t i o n s f o r c o n s t a n t s c o n s   a n d  n i l  c a n b e w r i t t e n i n M L  

a s

d a t a t y p e ' a l i s t = n i l | c o n s o f ' a * ' a l i s t

Page 5: Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

7/27/2019 Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

http://slidepdf.com/reader/full/avoiding-dynamic-type-checking-in-a-polymorphic-logic-programming-language 5/14

Page 6: Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

7/27/2019 Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

http://slidepdf.com/reader/full/avoiding-dynamic-type-checking-in-a-polymorphic-logic-programming-language 6/14

2 . 1 . 2 W e l l - t y p e d p r o g r a m s m a y \ g o w r o n g "  

I t i s s t r a i g h t f o r w a r d t o s h o w n o w t h a t , w i t h o u t t h e t y p e p r e s e r v i n g p r o p e r t y ,

a p r o g r a m s t a t i c a l l y w e l l t y p e d - c h e c k e d i s n o t d y n a m i c a l l y w e l l t y p e d . F o r

e x a m p l e , t h e f o l l o w i n g p r o g r a m g o e s w r o n g i f n o d y n a m i c c h e c k i n g i s d o n e .

e q : ( A , A ) - > o .

f o r g e t : A - > i .

q u e r y : - e q ( f o r g e t ( 1 ) , f o r g e t ( " 1 " ) ) .

I t i s p e r f e c t l y w e l l - t y p e d , b u t i t g o e s w r o n g b e c a u s e t h e e x e c u t i o n e v e n t u a l l y  

p r o d u c e s u n i c a t i o n p r o b l e m   < 1 , " 1 " >  , w h i c h i s i l l - t y p e d ( 1  h a s t y p e i n t 

w h i l e " 1 " h a s t y p e s t r i n g   ) . I n a p r e s c r i p t i v e t y p e s y s t e m , t h e s o l u t i o n s o f

s u c h a n i l l - t y p e d u n i c a t i o n p r o b l e m a r e n o t d e n e d .

2 2    P r o l o g  

W e b r i e y ( a n d i n f o r m a l l y ) p r e s e n t t h e e x t e n s i o n o f P r o l o g t o h i g h e r - o r d e r

h e r e d i t a r y H a r r o p f o r m u l a s . T h e d e s c r i p t i o n i s g i v e n u s i n g t h e c o n c r e t e

s y n t a x o f   P r o l o g . T h i s s y n t a x s l i g h t l y d i e r s f r o m t h e s y n t a x o f P r o l o g i n  

o r d e r t o e n c o d e b e t t e r h i g h e r - o r d e r f e a t u r e s .

T y p e s , T e r m s a n d F o r m u l a s S i m p l e t y p e s 4 ] a r e r s t - o r d e r t e r m s b u i l t

f r o m a c o l l e c t i o n o f t y p e c o n s t a n t s , a c o l l e c t i o n o f t y p e v a r i a b l e s a n d o n e

d e d i c a t e d b i n a r y t y p e c o n s t a n t , ' - > ' ( r i g h t a s s o c i a t i v e ) . I n t u i t i v e l y , A - > B  

i s t h e t y p e o f f u n c t i o n s f r o m t e r m s o f t y p e A  t o t e r m s o f t y p e B  . N o t e t h a t

w e a d o p t a d i e r e n t s y n t a x f o r t y p e s o f P r o l o g a n d t y p e s o f   P r o l o g . F o r

e x a m p l e , t h e t y p e o f c o n s   w h i c h w e r e n o t e d   ( A , l i s t ( A ) ) - > l i s t ( A ) i s

n o t e d i n     P r o l o g   A - > ( l i s t A ) - > ( l i s t A )

S i m p l y t y p e d     - t e r m s a r e b u i l t f r o m a c o l l e c t i o n o f c o n s t a n t s , a c o l -

l e c t i o n o f v a r i a b l e s , a c o l l e c t i o n o f l o g i c v a r i a b l e s a n d t w o c o n s t r u c t i o n  

r u l e s , a b s t r a c t i o n   ( x \  E  ) a n d   a p p l i c a t i o n   ( ( E F  ) ) . E a c h c o n s t a n t m u s t

b e d e c l a r e d w i t h t h e ' t y p e ' d i r e c t i v e , s p e c i f y i n g t h e t y p e o f t h e c o n s t a n t :

t y p e c o n s A - > ( l i s t A ) - > ( l i s t A ) . d e c l a r e s c o n s a s a c o n s t a n t o f t y p e

A - > ( l i s t A ) - > ( l i s t A )

T h r e e e q u i v a l e n c e r e l a t i o n s a r e d e n e d o n t h e s e t o f t e r m s .   - e q u i v a l e n c e  

d e n e s c o n s i s t e n t r e n a m i n g o f v a r i a b l e s .   - e q u i v a l e n c e   d e n e s t h e c o n s i s t e n t

r e d u c t i o n o f a n a p p l i c a t i o n   ( x \  E F  ) ( c a l l e d a     - r e d e x   ) i n t o   E  w h e r e e a c h  

f r e e o c c u r r e n c e o f x  i s s u b s t i t u t e d i n   E  b y  F    - e q u i v a l e n c e   f o r m a l i s e s e x -

t e n s i o n a l i t y o f   - d e n e d f u n c t i o n s . T h e u n i c a t i o n o f s i m p l y t y p e d     t e r m s

t a k i n g i n t o a c c o u n t t h e t h r e e e q u i v a l e n c e r e l a t i o n s i s a n o n - d e c i d a b l e a n d  

i n n i t a r y p r o b l e m : t h e r e m a y b e i n n i t e l y m a n y m o s t g e n e r a l s o l u t i o n s 9 ] .

S o , t h e a l g o r i t h m o f u n i c a t i o n i s n o t d e t e r m i n i s t i c .

A    P r o l o g p r o g r a m i s a s e t o f d e c l a r a t i o n s a n d a n o r d e r e d s e t o f c l a u s e s .

L i k e i n P r o l o g , t h e c o n s t r u c t o r o f n o n - e m p t y b o d y c l a u s e s i s ' : - ' , t h e c o n -

 j u n c t i o n c o n s t r u c t o r i s ' '

Page 7: Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

7/27/2019 Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

http://slidepdf.com/reader/full/avoiding-dynamic-type-checking-in-a-polymorphic-logic-programming-language 7/14

A T y p e S y s t e m   W e g i v e h e r e a t y p e s y s t e m f o r   P r o l o g . I t d i e r s f r o m  

t h e t y p e s y s t e m o f P r o l o g b y t h e s y n t a x a n d t h e n e w r u l e f o r t h e a b s t r a c t i o n .

v a r i a b l e

?  X     ̀ X   

a t o m  

?   ̀ a   

      ( a  )

a p p l i c a t i o n  

?   ̀ t

1

 

1

!   

2

?   ̀ t

2

 

1

?   ̀ t

1

t

2

 

2

c l a u s e

?   ̀ t

1

 

1

?   ̀ t

k

 

k

?   ̀ B 

1

o : : : ?   ̀ B 

n

?   ̀ p t

1

: : : t

k

  B 

1

 ̂  ̂ B 

n

  ( p  )

 

 

1

!  : : :  

k

!  o 

a b s t r a c t i o n  

?  x   

1

 ̀ t  

2

?   ̀ x : t  

1

!   

2

N o t e t h a t t h e v a r i a b l e   r u l e w o r k s f o r b o t h l o g i c v a r i a b l e s a n d v a r i a b l e s

f r o m a b s t r a c t i o n s .

2 . 3 H i g h e r - O r d e r U n i c a t i o n n e e d s t y p e s  

W e r e c a l l i n t h i s s e c t i o n w h y i n     P r o l o g , t y p e s a r e n e c e s s a r y f o r u n i c a t i o n .

S e e 9 ] f o r a c o m p l e t e d e s c r i p t i o n o f t h e h i g h e r - o r d e r u n i c a t i o n a l g o r i t h m .

L o g i c v a r i a b l e s m u s t c a r r y t h e i r t y p e s S u p p o s e w e h a v e t o u n i f y  

( F T 

1

) w i t h a n y t e r m w h e r e T 

1

i s a t e r m a n d   F  i s a l o g i c v a r i a b l e . T h e

p r o j e c t i o n r u l e   o f t h e h i g h e r - o r d e r u n i c a t i o n a l g o r i t h m s t a r t s t h e u n i c a t i o n  

b y u n i f y i n g   F  t o  x : x   i f  t h e r e s u l t t y p e F  i s e q u a l t o t h e t y p e o f T 

1

. A w a y  

t o c h e c k t h i s p r e r e q u i s i t e i s t o c h e c k t h a t t h e a r g u m e n t t y p e o f F  i s e q u a l t o  

t h e r e s u l t t y p e o f F  . T h i s n e e d s t o k n o w o n l y t h e t y p e o f F  . F o r e x a m p l e i f

t h e t y p e o f F  i s  

1

!   

2

, t h e p r o j e c t i o n r u l e i s a p p l i e d o n l y i f  

1

a n d   

2

a r e

u n i a b l e .

T y p e s o f c o n s t a n t s m u s t b e c o m p u t a b l e   S u p p o s e w e h a v e t o u n i f y  

t h e e x i b l e t e r m ( F  1 ) w i t h ( +   T 

1

2

) . T h e i m i t a t i o n r u l e   o f t h e u n i c a t i o n  

a l g o r i t h m l e a d s t o t h e s u b s t i t u t i o n   F  =  x ( + ( H 

1

x  ) ( H 

2

x  ) ) w h e r e H 

1

a n d 

2

a r e n e w l o g i c a l v a r i a b l e s . T h e s e v a r i a b l e s m u s t b e t y p e d p r o p e r l y . T h e y  

h a v e t y p e s i n t !   

1

a n d  i n t !   

2

. T h e  

i

t y p e s c a n b e i n f e r r e d f r o m t h e

t y p e o f t h e c o n s t a n t + . I t i s t h e t y p e o f t h e c o r r e s p o n d i n g a r g u m e n t T 

i

I t r e m a i n s t o b e a b l e t o i n f e r t h e t y p e o f t h e c o n s t a n t , i . e . t h e t y p e o f t h e

o c c u r r e n c e   o f t h e c o n s t a n t , n o t i t s t y p e s c h e m e . F o r e x a m p l e , w e m a y h a v e

  ( + ) =     !    !    a n d t h e r e s u l t t y p e o f F  e q u a l t o   i n t . W e t h e n c a n i n f e r

t h e t y p e s o f H 

1

a n d  H 

2

w h i c h i s i n t !  i n t

3 N e c e s s a r y T y p e s  

O b s e r v a t i o n s o f t h e p r e v i o u s s e c t i o n s m e a n t h a t t y p e s m u s t b e k e p t a t r u n -

t i m e s o t h a t t y p e u n i a b i l i t y c a n b e c h e c k e d b e f o r e t e r m u n i a b i l i t y . I n f a c t ,

o n l y p a r t s o f t y p e s h a v e t o b e k e p t .

Page 8: Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

7/27/2019 Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

http://slidepdf.com/reader/full/avoiding-dynamic-type-checking-in-a-polymorphic-logic-programming-language 8/14

3 . 1 W h i c h t y p e s a r e s u c i e n t : f o r g o t t e n t y p e s  

W e s h o w e d i n s e c t i o n 2 . 1 . 2 t h a t , w i t h o u t t h e t y p e p r e s e r v i n g p r o p e r t y , a  

p r o g r a m m a y \ g o w r o n g " . S o , a s i m p l e i d e a i s t o r e p r e s e n t t h e t y p e s w h i c h  

a r e n o t p r e s e r v e d a n d t o d o a c h e c k o n t h e s e n o t - p r e s e r v e d t y p e s a t r u n t i m e .

I t i s o n l y n e c e s s a r y t o r e p r e s e n t t h e t y p e s w h i c h a r e i n s t a n c e s o f t h e t y p e

v a r i a b l e s t h a t d o n o t a p p e a r i n t h e r e s u l t t y p e o f a d e c l a r a t i o n ( f o r e x a m -

p l e T 2  i n t h e t y p e s c h e m e p a i r ( T 1 , T 2 ) - > T 1  ) . W e c a l l t h e s e t y p e s t h e

 f o r g o t t e n t y p e s 

I n o r d e r t o e x p l i c i t t h e s e f o r g o t t e n t y p e s , w e w i l l u s e a w i l d e x t e n s i o n o f

o u r t y p e d e n i t i o n w i t h m o r e p o l y m o r p h i c t y p e s : s e c o n d - o r d e r t y p e s 1 8 ] .

N o t a t i o n a n d d e n i t i o n   : L e t   = (  

1

; : : : ;  

) !    b e a t y p e s c h e m e . L e t

F  =  v 

1

; : : : ; v  

p

b e t h e v a r i a b l e s o f     w h i c h d o n o t a p p e a r i n       i s n o t e d w i t h  

t h e f o l l o w i n g e x p l i c i t q u a n t i c a t i o n s     v 

1

  v 

p

(  

1

; : : : ;  

) !    . L e t  

0

b e a 

t y p e i n s t a n c e o f     :  

0

=  s  (   ) . T h e i m a g e s o f   F  b y t h e s u b s t i t u t i o n   s  a r e 

c a l l e d t h e   f o r g o t t e n t y p e s o f   

0

W e u s e t h e p r e n e x s y m b o l ' P I ' f o r t h e p r o d u c t t y p e q u a n t i e r i n t h e

c o n c r e t e s y n t a x . F o r e x a m p l e , t h e t y p e s c h e m e p a i r ( T 1 , T 2 ) - > T 1   i s

w r i t t e n   ' P I ' T 2 \ ( p a i r ( T 1 , T 2 ) - > T 1 ) a n d t h e s y m b o l f o r g e t   m a y b e

d e c l a r e d   f o r g e t : ' P I ' A \ ( A - > i )

F o l l o w i n g t h i s n o t a t i o n , o u r p r o p o s i t i o n i s :

T o r e p r e s e n t o n l y t h e f o r g o t t e n t y p e s a t r u n t i m e .

T h e n t e r m s w i l l b e r e p r e s e n t e d a s i f t h e f u n c t i o n s y m b o l s w e r e d e c l a r e d  

w i t h t h e e x p l i c i t q u a n t i c a t i o n o n t h e f o r g o t t e n v a r i a b l e s . S o , t e r m  

f o r g e t ( 1 ) i s c o d e d a s f o r g e t ( i n t , 1 ) a n d t e r m   f o r g e t ( " 1 " ) i s c o d e d  

a s f o r g e t ( s t r i n g , " 1 " )

W e c o n s i d e r a g e n e r i c u n i c a t i o n a l g o r i t h m f o r t y p e s a n d t e r m s . W e

s u p p o s e t h a t t h i s a l g o r i t h m w o r k s f r o m l e f t t o r i g h t o n t h e a r g u m e n t s ( t y p e s

a n d s u b t e r m s ) o f a t e r m . I t m e a n s t h a t t h e f o r g o t t e n t y p e s o f a t e r m a r e

u n i e d b e f o r e i t s s u b t e r m s .

P r o p o s i t i o n   : A u n i c a t i o n a l g o r i t h m w h i c h w o r k s o n t e r m s w i t h f o r g o t t e n  

t y p e s d o e s n o t g o w r o n g .

T h i s p r o p o s i t i o n e n s u r e s t h a t c o m p i l e - t i m e t y p e c h e c k i n g p l u s d y n a m i c -

t y p e c h e k i n g o n f o r g o t t e n t y p e s a r e e n o u g h t o g e t a w e l l - t y p e d e x e c u t i o n .

P r o o f  : T h e p r o o f i s d o n e b y i n d u c t i o n o n t e r m s .

  I n i t i a l c a s e : A u n i c a t i o n p r o b l e m a l w a y s s t a r t s w i t h t w o l i t e r a l s w h i c h  

h a v e t h e t y p e o 

  S u p p o s e w e h a v e t o u n i f y t h e t w o t e r m s f  (  

1

; : : : ;  

p

; t

1

; : : : ; t

) a n d  

f  (  

0

1

; : : : ;  

0

p

; t

0

1

; : : : ; t

0

) w h e r e t h e  

i

a r e t h e f o r g o t t e n t y p e s . T h e i n -

d u c t i o n h y p o t h e s i s s a y s t h a t t h e t w o t e r m s h a v e t h e s a m e t y p e   W e

Page 9: Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

7/27/2019 Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

http://slidepdf.com/reader/full/avoiding-dynamic-type-checking-in-a-polymorphic-logic-programming-language 9/14

h a v e t o p r o v e t h a t t

i

h a s t h e s a m e t y p e t h a n   t

0

i

f o r e v e r y   i a f t e r e a c h  

 

i

t y p e h a s b e e n u n i e d w i t h    

0

i

T h e t y p e o f t h e t w o   f  o c c u r r e n c e s   = (  

1

; : : : ;  

) !    a n d   

0

(  

0

1

; : : : ;  

0

) !    a r e i n s t a n c e s o f t h e s a m e t y p e s c h e m e   ( f  ) :   = 

s  (   ( f  ) ) a n d    

0

=  s 

0

(   ( f  ) ) w h e r e   ( f  ) =   v 

1

  v 

p

(  

1

; : : : ;  

) !    . F o r

e v e r y   i w e h a v e  

i

=  s  ( v 

i

) ,  

0

i

=  s 

0

( v 

i

) a n d    

i

=   

0

i

. B y d e n i t i o n ,

a l l t h e o t h e r v a r i a b l e s o f   ( f  ) ( d i e r e n t f r o m t h e v 

i

' s ) o c c u r i n     ( t h e

r e s u l t t y p e ) a n d w e h a v e s  (   ) =    =  s 

0

(   ) . S o   s  =  s 

0

a n d t h e n  

s  (  

i

) =   

i

=  s 

0

(  

i

) =   

0

i

, i . e . t h e t y p e o f t

i

i s e q u a l t o t h e t y p e o f t

0

i

f o r

e v e r y   i

N o t e t h a t , u n l i k e o t h e r p r e s e n t a t i o n s , t h e r e i s n o s p e c i a l s y n t a x f o r

d e c l a r i n g p r e d i c a t e c o n s t a n t s . T h e y a r e o n l y d i s t i n g u i s h a b l e b y t h e i r r e -

s u l t t y p e , o  . S o , e v e r y p r e d i c a t e c o n s t a n t f o r g e t s e v e r y t y p e v a r i a b l e i n i t s

t y p e s c h e m e b e c a u s e i t s r e s u l t t y p e ( o  ) c o n t a i n s n o t y p e v a r i a b l e . I t c a n  

b e s h o w n t h a t i f t h e p r e d i c a t e o b e y s t h e d e n i t i o n a l g e n e r i c i t y p r i n c i p l e ,

u n i c a t i o n o f t h e s e f o r g o t t e n t y p e s a l w a y s s u c c e e d s ; t y p e u n i c a t i o n i s o n l y  

r e q u i r e d f o r c o n v e y i n g t y p e s a l o n g t h e c o m p u t a t i o n . I n f a c t , i f t h e t y p e

p r e s e r v i n g r e s t r i c t i o n w e r e i m p o s e d o n f u n c t i o n a l c o n s t a n t s o n l y , w e l l - t y p e d  

p r o g r a m s c o u l d n o t g o w r o n g 1 5 , 7 , 1 1 ] ( e x c e p t o f c o u r s e w i t h h i g h e r - o r d e r

p r o g r a m m i n g ) .

T o s u m u p  

  F o r g o t t e n t y p e s o f b o t h f u n c t i o n a n d p r e d i c a t e c o n s t a n t s n e e d t o b e

r e p r e s e n t e d a t r u n - t i m e

  O n l y f o r g o t t e n t y p e s o f f u n c t i o n c o n s t a n t n e e d t o b e u n i e d a t r u n t i m e .

F o r g o t t e n t y p e s o f p r e d i c a t e c o n s t a n t s a r e j u s t p a s s e d .

S o , t y p e p r e s e r v i n g c o n s t a n t s ( s u c h a s ] : l i s t ( A )

a n d  c o n s : ( A , l i s t ( A ) ) - > l i s t ( A ) ) a r e i m p l e m e n t e d i n t h e s t a n d a r d w a y ,

i . e . w i t h n o t y p e i n f o r m a t i o n .

H a n u s h a s s h o w n t h a t i t i s n o t n e c e s s a r y t o r e p r e s e n t ( a n d c h e c k a t

r u n t i m e ) t h e t y p e o f t h e a r g u m e n t s o f a t y p e p r e s e r v i n g f u n c t i o n . O u r

r e s u l t i s m o r e p r e c i s e : i t i s o n l y n e c e s s a r y t o r e p r e s e n t t h e t y p e s w h i c h a r e

n o t p r e s e r v e d b y a c o n s t a n t .

O n t h e H a n u s ' m a p  e x a m p l e 6 ] , i t m e a n s t h a t , i n s t e a d o f t h e p r o p o s e d  

m a p ( P : p r e d ( A , B ) , E 1 | L 1 ] : l i s t ( A ) , E 2 | L 2 ] : l i s t ( B ) )

f o l l o w i n g t h e t y p e d e c l a r a t i o n ( A  a n d  B  a r e f o r g o t t e n t y p e s i n t h e t y p e o f

m a p  ) :

m a p : ' P I ' A \ ' P I ' B \ ( ( p r e d ( A , B ) , l i s t ( A ) , l i s t ( B ) ) - > o )

o n l y t h e g o a l

m a p ( A , B , P , E 1 | L 1 ] , E 2 | L 2 ] )

i s n e e d e d f o r c o r r e c t l y t y p e d u n i c a t i o n .

Page 10: Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

7/27/2019 Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

http://slidepdf.com/reader/full/avoiding-dynamic-type-checking-in-a-polymorphic-logic-programming-language 10/14

3 . 2 T h e     P r o l o g c a s e  

B e c a u s e o f n e e d s o f h i g h e r - o r d e r u n i c a t i o n , m o r e t y p e s n e e d t o b e r e p r e -

s e n t e d i n t h e   P r o l o g c a s e .

F i r s t l y , t y p e o f t h e l o g i c v a r i a b l e s m u s t b e f u l l y r e p r e s e n t e d i n o r d e r t o  

b e a b l e t o a p p l y t h e p r o j e c t i o n r u l e s   o f t h e u n i c a t i o n a l g o r i t h m  

S e c o n d l y w e s a w t h a t a p p l i c a t i o n o f t h e i m i t a t i o n r u l e   n e e d t o b e a b l e

t o i n f e r t h e t y p e o f a n y c o n s t a n t . W e o b s e r v e t h a t t h e t y p e s c h e m e o f a  

c o n s t a n t , p l u s t h e f o r g o t t e n t y p e s a t t a c h e d t o i t , p l u s t h e r e s u l t t y p e g i v e

e n o u g h i n f o r m a t i o n f o r r e c o n s t r u c t i n g t h e t y p e o f t h e c o n s t a n t . D u r i n g  

u n i c a t i o n , t h e r e s u l t t y p e c a n b e f o u n d i n t h e t y p e o f t h e e x i b l e h e a d  

( s e e 2 . 3 ) . N o t e t h a t t h e r e s u l t t y p e c a n n o t b e r e c o n s t r u c t e d o n l y f r o m  

t h e f o r g o t t e n t y p e s b e c a u s e i t m a y c o n t a i n i n s t a n c e s o f n o n - f o r g o t t e n t y p e

v a r i a b l e s ( t y p e v a r i a b l e s o c c u r r i n g i n t h e r e s u l t t y p e o f t h e t y p e s c h e m e ) .

T o s u m u p , t y p e d h i g h e r - o r d e r u n i c a t i o n n e e d s o n l y ( p l u s t h e f o r g o t t e n  

t y p e s a s i n t h e r s t - o r d e r c a s e ) :

  T h e t y p e o f t h e l o g i c v a r i a b l e s ( d y n a m i c a l l y r e p r e s e n t e d ) ;

  T h e t y p e s c h e m e o f t h e f u n c t i o n s y m b o l s ( d y n a m i c a l l y r e c o n s t r u c t e d ) .

M o r e d e t a i l s a b o u t t h e h a n d l i n g o f t y p e s i n     P r o l o g a r e g i v e n i n t h e n e x t

s e c t i o n .

4 I m p l e m e n t a t i o n  

W e g i v e i n t h i s s e c t i o n t h e m o d i c a t i o n s w h i c h a r e n e c e s s a r y t o d o o n a  

s t a n d a r d i m p l e m e n t a t i o n o f P r o l o g 1 ] t o h a n d l e a t y p e d P r o l o g w i t h o u t t h e

t y p e p r e s e r v i n g p r o p e r t y .

4 . 1 P r o l o g  

W e h a v e s h o w n t h a t , i n o r d e r t o h a v e a c o r r e c t i n t e r p r e t a t i o n o f a t y p e d  

P r o l o g p r o g r a m . i . e . w i t h w e l l - t y p e d u n i c a t i o n p r o b l e m s , i t i s o n l y n e c -

e s s a r y t o r e p r e s e n t t h e f o r g o t t e n t y p e s . T h i s l e a d s t o a m o d i c a t i o n o f t h e

d a t a - s t r u c t u r e s a n d t h e u n i c a t i o n a l g o r i t h m .

T h e n u m b e r p  o f f o r g o t t e n t y p e s o f e v e r y f u n c t i o n   f = n  ( f  h a s a r i t y   n  )

i s c o m p u t e d a t c o m p i l e - t i m e , f r o m t h e t y p e d e c l a r a t i o n o f t h e c o n s t a n t f 

A s t r u c t u r e o f f u n c t o r f  i s t h e n r e p r e s e n t e d b y a n a r r a y o f s i z e 1 +   n  + 

p  . T h e f o r g o t t e n t y p e s a r e r e p r e s e n t e d a s u s u a l s u b t e r m s o f t h e s t r u c t u r e .

S o , f o r e a c h c o n s t r u c t i o n o f a s t r u c t u r e w i t h f o r g o t t e n t y p e s t h e r e w i l l b e

i n s t r u c t i o n s t o c r e a t e t h e s e t y p e s .

B e c a u s e t y p e s a r e e s s e n t i a l l y r s t - o r d e r t e r m s , i t i s n o t n e c e s s a r y t o  

d i s t i n g u i s h t h e t y p e s f r o m t h e t e r m s . S o , w e d o n o t n e e d t o m o d i f y t h e

s t a n d a r d a b s t r a c t m a c h i n e . T h e c o m p i l i n g p r o c e s s i s m o d i e d t o t a k e i n t o  

a c c o u n t t h e t y p e s b u t t h e m o d i c a t i o n m a y b e t r a n s p a r e n t t o t h e u n i c a t i o n .

1 0 

Page 11: Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

7/27/2019 Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

http://slidepdf.com/reader/full/avoiding-dynamic-type-checking-in-a-polymorphic-logic-programming-language 11/14

A s w e h a v e s h o w n , i t i s o n l y n e c e s s a r y t h a t t h e f o r g o t t e n t y p e a r e u n i e d  

b e f o r e t h e o t h e r s u b t e r m s o f a s t r u c t u r e .

W e n o t e t h a t b e c a u s e o f t h e d e n i t i o n a l g e n e r i c i t y o f t h e t y p e s y s t e m ,

t h e u n i c a t i o n o f t h e f o r g o t t e n t y p e s i n t h e h e a d o f a c l a u s e i s r e d u c e d t o a  

p a r a m e t e r p a s s i n g , i . e . a   g e t _ v a r i a b l e   W A M i n s t r u c t i o n .

E x a m p l e  : ( f r o m 1 7 ] ) W e g i v e t h e t y p e d e c l a r a t i o n w i t h t h e e x p l i c i t

q u a n t i c a t i o n o n f o r g o t t e n v a r i a b l e s t o c l a r i f y t h e c o m p i l a t i o n p r o c e s s .

f s t : ' P I ' T 2 \ p a i r ( T 1 , T 2 ) - > T 1 .

s n d : ' P I ' T 1 \ p a i r ( T 1 , T 2 ) - > T 2 .

r e l _ p a i r : ' P I ' T 1 \ ' P I ' T 2 \ ( p a i r ( T 1 , T 2 ) , p a i r ( T 1 , T 2 ) ) - > o .

r e l : ' P I ' T \ ( T , T ) - > o .

r e l _ p a i r ( X , Y ) : - r e l ( f s t ( X ) , f s t ( Y ) ) ,

r e l ( s n d ( X ) , s n d ( Y ) ) .

A f t e r c o m p i l e - t i m e t y p e c h e c k i n g , w e g e t t h e f o l l o w i n g p r o g r a m w i t h t h e f o r -

g o t t e n t y p e s a s n e w a r g u m e n t s c o r r e s p o n d i n g e x a c t l y t o t h e q u a n t i c a t i o n s :

r e l _ p a i r ( T 1 , T 2 , X , Y ) : -  

r e l ( T 1 , f s t ( T 2 , X ) , f s t ( T 2 , Y ) ) ,

r e l ( T 2 , s n d ( T 1 , X ) , f s t ( T 1 , Y ) ) .

T h i s p r o g r a m i s t h e n c o m p i l e d i n t h e u s u a l w a y ( p r o v i d e d t h a t t y p e s a n d  

t e r m s a r e n o t d i s t i n g u i s h e d ) . W e r e m a r k t h a t i t i s n o t n e c e s s a r y t o e x t e n d  

t h e W A M t o e x e c u t e t h i s t y p e d p r o g r a m . B e c a u s e o f t h i s m i x i n g o f t y p e s

a n d t e r m s , w e g e t f o r f r e e a l l t h e u s u a l o p t i m i s a t i o n s ( a s r e g i s t e r a l l o c a t i o n )

f o r t h e h a n d l i n g o f t h e t y p e s .

4 2    P r o l o g  

T h e i m p l e m e n t a t i o n o f t h e t y p e s i n     P r o l o g i s m o r e c o m p l e x t h a n i n P r o l o g  

f o r t w o r e a s o n s ( s e e 2 . 3 ) :

  E v e r y l o g i c v a r i a b l e m u s t c a r r y i t s t y p e .

  I t m u s t b e p o s s i b l e t o r e c o n s t r u c t t h e t y p e o f a n y c o n s t a n t o c c u r r e n c e .

F i r s t , t h e t y p e o f t h e l o g i c v a r i a b l e s m u s t b e r e p r e s e n t e d . T h i s t y p e m a y  

b e a t t a c h e d t o t h e v a r i a b l e u s i n g t h e n o w w e l l - k n o w n   a t t r i b u t e d v a r i a b l e   1 2 ]

a l s o c a l l e d   m e t a t e r m   1 3 ] .

S e c o n d l y , t h e u n i c a t i o n a l g o r i t h m m u s t b e a b l e t o r e c o n s t r u c t t h e t y p e

o f a n y c o n s t a n t . W e p r o p o s e h e r e a c o m p i l e d s o l u t i o n . A t y p e r e c o n s t r u c t i o n  

f u n c t i o n i s g e n e r a t e d a t c o m p i l e - t i m e f r o m e v e r y t y p e s c h e m e d e c l a r a t i o n .

S u c h a f u n c t i o n t a k e s t h e r e s u l t t y p e a s a p a r a m e t e r .

L e t U  b e a f u n c t i o n r e t u r n i n g t h e m o s t g e n e r a l u n i e r ( a s u b s t i t u t i o n )

o f t w o t y p e s . T o e v e r y c o n s t a n t w i t h a t y p e s c h e m e   =   v 

1

  v 

p

 

0

, w h o s e

r e s u l t t y p e i s   ( b y d e n i t i o n , i t c a n n o t d e p e n d o n f o r g o t t e n t y p e s ) , w e

a s s o c i a t e t h e t y p e r e c o n s t r u c t i o n f u n c t i o n   v 

1

: : : v  

p

( U  ( ;   ) (  

0

) )

1 1 

Page 12: Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

7/27/2019 Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

http://slidepdf.com/reader/full/avoiding-dynamic-type-checking-in-a-polymorphic-logic-programming-language 12/14

T h e t y p e r e c o n s t r u c t i o n f u n c t i o n m u s t b e a p p l i e d t o t h e a c t u a l f o r g o t -

t e n t y p e s o f s o m e o c c u r r e n c e o f t h e c o n s t a n t a n d t o t h e r e s u l t t y p e o f t h e

m a t c h i n g l o g i c v a r i a b l e   . T y p e c h e c k i n g m a k e s i t s u r e t h a t U  ( ;   ) d e n e s

a s u b s t i t u t i o n .

F o r e x a m p l e , l e t t h e f o l l o w i n g d e c l a r a t i o n  

f : A - > B - > ( t B ) .

T y p e v a r i a b l e A  i s f o r g o t t e n , h e n c e t h e d e c l a r a t i o n i s r e a d a s

f : ' P I ' A \ ( A - > B - > ( t B ) ) .

T h e t y p e r e c o n s t r u c t i o n f u n c t i o n i s   a  U  ( ( t B ) ;   ) ( a - > B - > ( t B ) )

T h e t w o p a r a m e t e r s o f t h e f u n c t i o n a r e a  t h e f o r g o t t e n t y p e a n d     t h e r e s u l t

t y p e . T h e f u n c t i o n u n i e s   w i t h   ( t B ) ( w h e r e B  i s a n e w t y p e v a r i a b l e )

a n d r e t u r n s t h e t y p e a - > B - > ( t B )

W i t h t h e s e d e c l a r a t i o n s , u n i c a t i o n p r o b l e m   < ( F 1 ) , ( f 1 1 ] ) >   i s a c -

t u a l l y r e p r e s e n t e d a s

< ( F : ( i n t - > ( t ( l i s t i n t ) ) ) 1 )

, ( f i n t 1 1 ] )

>

T h e i m i t a t i o n r u l e y i e l d s t h e s u b s t i t u t i o n  

x \ ( f i n t ( H 1 x ) ( H 2 x ) ) / F ] .

w h i c h i n t r o d u c e s n e w l o g i c v a r i a b l e s H 1  a n d  H 2  . F o r t y p i n g t h e m , t h e

t y p e r e c o n s t r u c t i o n f u n c t i o n i s c a l l e d w i t h t y p e s i n t  a n d  ( t ( l i s t i n t ) )

a s p a r a m e t e r s . I t u n i e s ( t ( l i s t i n t ) ) a n d  ( t B ) , p r o d u c i n g t h e

s u b s t i t u t i o n   ( l i s t i n t ) / B ] , a n d a p p l i e s t h e s u b s t i t u t i o n t o t h e

t y p e s c h e m e i n w h i c h t h e f o r g o t t e n t y p e v a r i a b l e i s r e p l a c e d b y   i n t 

S o , t h e a c t u a l t y p e o f t h i s o c c u r r e n c e o f c o n s t a n t f  i s r e c o n s t r u c t e d :

i n t - > ( l i s t i n t ) - > ( t ( l i s t i n t ) ) . T h e n , l o g i c v a r i a b l e s H 1  a n d  H 2 

a r e g i v e n t y p e s i n t - > i n t   a n d  i n t - > ( l i s t i n t )

5 R e l a t e d w o r k  

T h e c o m p a r i s o n o f o u r s o l u t i o n w i t h H a n u s ' o n e h a s b e e n d o n e i n s e c t i o n  

3 . 1 . H a n u s h a s p r o p o s e d a n o p t i m i s a t i o n f o r c o n s t a n t s w h i c h v e r i f y t h e

p r e s e r v i n g p r o p e r t y o n l y w h i l e o u r i m p r o v e m e n t i s d o n e o n e v e r y c o n s t a n t ,

c o n s i d e r i n g p r e c i s e l y i t s t y p e . H o w e v e r , t h e o p t i m i s a t i o n s b a s e d o n m o d e

a n a l y s i s p r o p o s e d b y H a n u s c a n b e a d d e d t o o u r s o l u t i o n i n o r d e r t o r e m o v e

m o r e t y p e r e p r e s e n t a t i o n a n d u n i c a t i o n .

N a d a t h u r a n d W i l s o n 1 7 ] h a v e d e s c r i b e d a n i m p l e m e n t a t i o n o f a p o l y -

m o r p h i c t y p i n g i n P r o l o g . T h e y p r o p o s e t o r e p r e s e n t t h e t y p e o f t h e s y m b o l s

a n d t h e l o g i c v a r i a b l e s . W e h a v e s h o w n i n t h i s p a p e r t h a t , i n t h e r s t - o r d e r

1 2 

Page 13: Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

7/27/2019 Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

http://slidepdf.com/reader/full/avoiding-dynamic-type-checking-in-a-polymorphic-logic-programming-language 13/14

( P r o l o g ) c a s e , t h e t y p e o f t h e l o g i c v a r i a b l e s i s n o t u s e f u l t o h a v e a w e l l - t y p e d  

e x e c u t i o n .

N o t e t h a t t h e p r o p o s e d t y p e r e c o n s t r u c t i o n f u n c t i o n ( s e e 4 . 2 ) i s n o t

e q u i v a l e n t t o t h e s k e l e t o n   i d e a p r o p o s e d b y N a d a t h u r . T h e s k e l e t o n i s a  

f u n c t i o n a b l e t o r e c o n s t r u c t t h e t y p e o f a c o n s t a n t f r o m t h e i n s t a n c e s o f

a l l t h e t y p e v a r i a b l e s i n t h e t y p e s c h e m e . I n o u r s c h e m e t h e n o n - f o r g o t t e n  

t y p e s a r e n o t d i r e c t l y a v a i l a b l e : t h e i n s t a n c e o f t h e n o n - f o r g o t t e n v a r i a b l e s

a r e d e r i v e d f r o m t h e u n i c a t i o n o f t h e r e s u l t t y p e .

6 C o n c l u s i o n  

W e h a v e r e c a l l e d t h a t t h e t y p e p r e s e r v i n g p r o p e r t y o f t y p e s y s t e m s i s n o t

c o m p a t i b l e w i t h h i g h e r - o r d e r p r o g r a m m i n g . W e h a v e d e s c r i b e d a n e c o n o m i -

c a l r e p r e s e n t a t i o n o f t y p e s a n d d y n a m i c t y p e - c h e c k i n g . W e h a v e p r o v e d t h a t

t h e r e p r e s e n t a t i o n o f t h e t y p e s w h i c h a r e n o t p r e s e r v e d   i s s u c i e n t t o h a v e a  

w e l l - t y p e d e x e c u t i o n . L a s t l y , w e h a v e g i v e n s o m e h i n t s o n t h e r e p r e s e n t i o n  

o f t y p e s r e q u i r e d b y t h e h i g h e r - o r d e r u n i c a t i o n i n     P r o l o g .

T h e i d e a s p r e s e n t e d i n t h i s p a p e r h a v e b e e n v a l i d a t e d b y t h e i m p l e m e n -

t a t i o n o f t h e t y p i n g i n t h e   P r o l o g c o m p i l e r P M 3 , 2 ] .

A c k n o w l e d g e m e n t s W e w i s h t o t h a n k M . D u c a s s e f o r h e r h e l p f u l r e -

m a r k s a n d O . R i d o u x f o r h i s p r o o f - r e a d i n g .

R e f e r e n c e s  

1 ] H . A t - K a c i . W a r r e n ' s A b s t r a c t M a c h i n e : A T u t o r i a l R e c o n s t r u c t i o n  

M I T P r e s s , 1 9 9 1 .

2 ] P . B r i s s e t a n d O . R i d o u x . T h e a r c h i t e c t u r e o f a n i m p l e m e n t a t i o n o f

  P r o l o g : P r o l o g / M a l i . I n   W o r k s h o p o n     P r o l o g   , P h i l a d e l p h i a , P A ,

U S A , 1 9 9 2 . f t p : / / f t p . i r i s a . f r / l o c a l / l a n d e .

3 ] P . B r i s s e t a n d O . R i d o u x . T h e c o m p i l a t i o n o f   P r o l o g a n d  

i t s e x e c u t i o n w i t h M A L I . T e c h n i c a l R e p o r t 6 8 7 , I R I S A , 1 9 9 2 .

f t p : / / f t p . i r i s a . f r / l o c a l / l a n d e .

4 ] A . C h u r c h . A f o r m u l a t i o n o f t h e s i m p l e t h e o r y o f t y p e s . J . S y m b o l i c  

L o g i c  , 5 ( 1 ) : 5 6 { 6 8 , 1 9 4 0 .

5 ] M . H a n u s . H o r n c l a u s e p r o g r a m s w i t h p o l y m o r p h i c t y p e s : S e m a n t i c s

a n d r e s o l u t i o n . I n   T A P S O F T ' 8 9 , L N C S 3 5 2  , p a g e s 2 2 5 { 2 4 0 . S p r i n g e r -

V e r l a g , 1 9 8 9 .

6 ] M . H a n u s . P o l y m o r p h i c h i g h e r - o r d e r p r o g r a m m i n g i n P r o l o g . I n G . L e v i

a n d M . M a r t e l l i , e d i t o r s , 6 t h I n t . C o n f . L o g i c P r o g r a m m i n g  , p a g e s 3 8 2 {  

3 9 7 . M I T P r e s s , 1 9 8 9 .

1 3 

Page 14: Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

7/27/2019 Avoiding Dynamic Type Checking in a Polymorphic Logic Programming Language

http://slidepdf.com/reader/full/avoiding-dynamic-type-checking-in-a-polymorphic-logic-programming-language 14/14

7 ] M . H a n u s . H o r n c l a u s e p r o g r a m s w i t h p o l y m o r p h i c t y p e s : S e m a n t i c s

a n d r e s o l u t i o n . T h e o r e t i c a l C o m p u t e r S c i e n c e  , 8 9 : 6 3 { 1 0 6 , 1 9 9 1 .

8 ] P . M . H i l l a n d R . W . T o p o r . A s e m a n t i c s f o r t y p e d l o g i c p r o g r a m s . I n  

F . P f e n n i n g , e d i t o r , T y p e s i n L o g i c P r o g r a m m i n g  , p a g e s 1 { 6 2 . M I T  

P r e s s , 1 9 9 2 .

9 ] G . H u e t . A u n i c a t i o n a l g o r i t h m f o r t y p e d     - c a l c u l u s . T h e o r e t i c a l

C o m p u t e r S c i e n c e  , 1 : 2 7 { 5 7 , 1 9 7 5 .

1 0 ] A . J . K f o u r y , J . T i u r y n , a n d P . U r z y c z y n . T h e u n d e c i d a b i l i t y o f t h e s e m i -

u n i c a t i o n p r o b l e m . T e c h n i c a l R e p o r t B U C S 8 9 - 0 1 0 , B o s t o n U n i v e r s i t y ,

1 9 8 9 .

1 1 ] T . K . L a k s h m a n a n d U . S . R e d d y . T y p e d P r o l o g : A s e m a n t i c r e c o n s t r u c -

t i o n o f t h e M y c r o f t - O ' K e e f e t y p e s y s t e m . I n   I n t . L o g i c P r o g r a m m i n g  

S y m p . , p a g e s 2 0 2 { 2 1 7 , 1 9 9 1 .

1 2 ] S . L e H u i t o u z e . A n e w d a t a s t r u c t u r e f o r i m p l e m e n t i n g e x t e n s i o n s

t o P r o l o g . I n P . D e r a n s a r t a n d J . M a l u s z y n s k i , e d i t o r s , 2 n d I n t .

W o r k . P r o g r a m m i n g L a n g u a g e s I m p l e m e n t a t i o n a n d L o g i c P r o g r a m -

m i n g , L N C S 4 5 6  , p a g e s 1 3 6 { 1 5 0 . S p r i n g e r - V e r l a g , 1 9 9 0 .

1 3 ] M . M e i e r a n d J . S c h i m p f . A n a r c h i t e c t u r e f o r P r o l o g e x t e n t i o n s . I n  

E . L e m m a a n d P . M e l l o , e d i t o r s , T h i r d I n t . W o r k s h o p o n E x t e n s i o n s o f  

L o g i c P r o g r a m m i n g , L N A I 6 6 0  , p a g e s 3 1 9 { 3 3 8 . S p r i n g e r - V e r l a g , 1 9 9 2 .

1 4 ] R . M i l n e r . A t h e o r y o f t y p e p o l y m o r p h i s m i n p r o g r a m m i n g . J . C o m -

p u t e r a n d S y s t e m S c i e n c e s  , 1 7 : 3 4 8 { 3 7 5 , 1 9 7 8 .

1 5 ] A . M y c r o f t a n d R . A . O ' K e e f e . A p o l y m o r p h i c t y p e s y s t e m f o r P r o l o g .

A r t i c i a l I n t e l l i g e n c e  , 2 3 : 2 9 5 { 3 0 7 , 1 9 8 4 .

1 6 ] G . N a d a t h u r a n d D . A . M i l l e r . A n o v e r v i e w o f   P r o l o g . I n K . B o w e n  

a n d R . K o w a l s k i , e d i t o r s , S y m p . L o g i c P r o g r a m m i n g  , p a g e s 8 1 0 { 8 2 7 ,

S e a t t l e , W a s h i n g t o n , U S A , 1 9 8 8 .

1 7 ] G . N a d a t h u r , D . S . W i l s o n , a n d K . K w o n . I m p l e m e n t i n g p o l y m o r p h i c

t y p i n g i n a l o g i c p r o g r a m m m i n g l a n g u a g e , 1 9 9 2 . S u b m i t t e d .

1 8 ] F . P f e n n i n g . D e p e n d e n t t y p e s i n l o g i c p r o g r a m m i n g . I n F . P f e n n i n g ,

e d i t o r , T y p e s i n L o g i c P r o g r a m m i n g  , p a g e s 2 8 5 { 3 1 1 . M I T P r e s s , 1 9 9 2 .

1 9 ] F . P f e n n i n g , e d i t o r . T y p e s i n L o g i c P r o g r a m m i n g  . M I T P r e s s , 1 9 9 2 .

2 0 ] U . S . R e d d y . N o t i o n s o f p o l y m o r p h i s m f o r p r e d i c a t e l o g i c p r o g r a m s . I n  

K . A . B o w e n a n d R . A . K o w a l s k i , e d i t o r s , 5 t h I n t . C o n f . a n d S y m p . o n  

L o g i c P r o g r a m m i n g  , p a g e s 1 7 { 3 4 . M I T P r e s s , A u g u s t 1 9 8 8 .

1 4