Haskell level error lambda calculus level (or lack thereof)

I read the Haskell wiki page into type arithmetic and had a little problem with the section on the built-in lambda calculus in the type system. From this section, I realized that the following code should not work with GHC / GHCi - it is obvious that GHC will not be able to determine a signature of type g.

{-# OPTIONS -fglasgow-exts #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-}

data X
data App t u
data Lam t

class Subst s t u | s t -> u
instance Subst X u u
instance (Subst s u s', Subst t u t') => Subst (App s t) u (App s' t')
instance Subst (Lam t) u (Lam t)

class Apply s t u | s t -> u
instance (Subst s t u, Eval u u') => Apply (Lam s) t u'

class Eval t u | t -> u
instance Eval X X
instance Eval (Lam t) (Lam t)
instance (Eval s s', Apply s' t u) => Eval (App s t) u

f :: Eval (App (Lam X) X) u => u
f = undefined
g :: Eval (App (Lam (App X X )) (Lam (App X X ))) u => u
g = undefined

Please note that I had to add FlexibleContexts and UndecidableInstances, as this code does not seem to compile without these extensions. However, when I run this using GHCi (version 8.0.2), I get the following results:

*Main> :t f
f :: X
*Main> :t g
g :: u

This is especially strange for me, because the type u is not defined anywhere. Is this the result of an extension of two language extensions interacting with each other and glasgow-ex? If so, how?

+4
1

u - - , a undefined :: a.

, :

{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE FlexibleInstances #-}

class Loopy a
instance Loopy a => Loopy a

x :: Loopy a => a
x = undefined

x ghci, , a, . ; , GHC , .

, , , . .

, , :

Eval (App (Lam (App X X)) (Lam (App X X))) u

instance (Eval s s', Apply s' t u) => Eval (App s t) u

, :

Eval (Lam (App X X)) s'
Apply s' (Lam (App X X)) u

, :

instance Eval (Lam t) (Lam t)

, , :

Apply (Lam (App X X)) (Lam (App X X)) u

instance (Subst s t u, Eval u u') => Apply (Lam s) t u'

, :

Subst (App X X) (Lam (App X X)) u
Eval u u'

instance (Subst s u s', Subst t u t') => Subst (App s t) u (App s' t')

, ,

Subst X (Lam (App X X)) s'
Subst X (Lam (App X X)) t'
Eval (App s' t') u'

, :

instance Subst X u u

, :

Eval (App (Lam (App X X)) (Lam (App X X))) u'

, , ! , . , , . , , ! .

+5

Source: https://habr.com/ru/post/1682276/


All Articles