2019-11-15 05:35:45 +03:00
|
|
|
module Deppy.Core where
|
|
|
|
|
|
|
|
import ClassyPrelude
|
|
|
|
|
|
|
|
import Bound
|
|
|
|
import Data.Deriving (deriveEq1, deriveOrd1, deriveRead1, deriveShow1)
|
2019-12-09 08:46:52 +03:00
|
|
|
import Data.Maybe (fromJust)
|
2019-12-05 04:08:40 +03:00
|
|
|
import Data.Set (isSubsetOf)
|
2019-12-09 08:46:52 +03:00
|
|
|
import qualified Data.Set as Set
|
2019-11-15 05:35:45 +03:00
|
|
|
import Numeric.Natural
|
|
|
|
|
2019-11-15 21:53:43 +03:00
|
|
|
type Typ = Exp
|
2019-11-15 05:35:45 +03:00
|
|
|
|
|
|
|
data Exp a
|
|
|
|
= Var a
|
2019-12-05 04:08:40 +03:00
|
|
|
-- types
|
2019-12-04 02:50:21 +03:00
|
|
|
| Typ
|
2019-11-15 05:35:45 +03:00
|
|
|
| Fun (Abs a)
|
2019-12-05 04:08:40 +03:00
|
|
|
| Cel (Abs a)
|
|
|
|
| Wut (Set Tag)
|
|
|
|
-- introduction forms
|
2019-11-15 05:35:45 +03:00
|
|
|
| Lam (Abs a)
|
2019-12-05 04:08:40 +03:00
|
|
|
| Cns (Exp a) (Exp a)
|
|
|
|
| Tag Tag
|
|
|
|
-- elimination forms
|
2019-11-15 05:35:45 +03:00
|
|
|
| App (Exp a) (Exp a)
|
2019-12-05 04:08:40 +03:00
|
|
|
| Hed (Exp a)
|
|
|
|
| Tal (Exp a)
|
2019-12-09 07:38:43 +03:00
|
|
|
| Cas (Typ a) (Exp a) (Map Tag (Exp a))
|
2019-12-09 08:46:52 +03:00
|
|
|
-- recursion
|
|
|
|
| Rec (Abs a)
|
2019-11-15 05:35:45 +03:00
|
|
|
deriving (Functor, Foldable, Traversable)
|
|
|
|
|
2019-12-05 04:08:40 +03:00
|
|
|
type Tag = Natural
|
|
|
|
|
2019-11-15 05:35:45 +03:00
|
|
|
data Abs a = Abs
|
|
|
|
{ spec :: Typ a
|
|
|
|
, body :: Scope () Exp a
|
|
|
|
}
|
|
|
|
deriving (Functor, Foldable, Traversable)
|
|
|
|
|
2019-11-16 09:04:42 +03:00
|
|
|
deriveEq1 ''Abs
|
|
|
|
deriveOrd1 ''Abs
|
2019-11-15 05:35:45 +03:00
|
|
|
deriveRead1 ''Abs
|
|
|
|
deriveShow1 ''Abs
|
|
|
|
--makeBound ''Abs
|
|
|
|
|
|
|
|
deriveEq1 ''Exp
|
|
|
|
deriveOrd1 ''Exp
|
|
|
|
deriveRead1 ''Exp
|
|
|
|
deriveShow1 ''Exp
|
|
|
|
--makeBound ''Exp
|
|
|
|
|
|
|
|
deriving instance Eq a => Eq (Abs a)
|
|
|
|
deriving instance Ord a => Ord (Abs a)
|
|
|
|
deriving instance Read a => Read (Abs a)
|
|
|
|
deriving instance Show a => Show (Abs a)
|
|
|
|
|
|
|
|
deriving instance Eq a => Eq (Exp a)
|
|
|
|
deriving instance Ord a => Ord (Exp a)
|
|
|
|
deriving instance Read a => Read (Exp a)
|
|
|
|
deriving instance Show a => Show (Exp a)
|
|
|
|
|
|
|
|
instance Applicative Exp where
|
|
|
|
pure = Var
|
|
|
|
(<*>) = ap
|
|
|
|
|
|
|
|
instance Monad Exp where
|
|
|
|
return = Var
|
|
|
|
Var a >>= f = f a
|
2019-12-04 02:50:21 +03:00
|
|
|
Typ >>= _ = Typ
|
2019-11-15 05:35:45 +03:00
|
|
|
Fun a >>= f = Fun (bindAbs a f)
|
2019-12-05 04:08:40 +03:00
|
|
|
Cel a >>= f = Cel (bindAbs a f)
|
|
|
|
Wut ls >>= _ = Wut ls
|
2019-11-15 05:35:45 +03:00
|
|
|
Lam a >>= f = Lam (bindAbs a f)
|
2019-12-05 04:08:40 +03:00
|
|
|
Cns x y >>= f = Cns (x >>= f) (y >>= f)
|
|
|
|
Tag l >>= _ = Tag l
|
2019-11-15 05:35:45 +03:00
|
|
|
App x y >>= f = App (x >>= f) (y >>= f)
|
2019-12-05 04:08:40 +03:00
|
|
|
Hed x >>= f = Hed (x >>= f)
|
|
|
|
Tal x >>= f = Tal (x >>= f)
|
2019-12-09 07:38:43 +03:00
|
|
|
Cas t x cs >>= f = Cas (t >>= f) (x >>= f) (cs <&> (>>= f))
|
2019-12-09 08:46:52 +03:00
|
|
|
Rec a >>= f = Rec (bindAbs a f)
|
2019-11-15 05:35:45 +03:00
|
|
|
|
|
|
|
bindAbs :: Abs a -> (a -> Exp b) -> Abs b
|
|
|
|
bindAbs (Abs s b) f = Abs (s >>= f) (b >>>= f)
|
|
|
|
|
2019-11-15 21:53:43 +03:00
|
|
|
lam :: Eq a => a -> Typ a -> Exp a -> Exp a
|
|
|
|
lam v t e = Lam (Abs t (abstract1 v e))
|
2019-11-15 05:35:45 +03:00
|
|
|
|
2019-12-05 04:08:40 +03:00
|
|
|
fun :: Eq a => a -> Typ a -> Typ a -> Typ a
|
|
|
|
fun v t u = Fun (Abs t (abstract1 v u))
|
|
|
|
|
|
|
|
fun_ :: Typ a -> Typ a -> Typ a
|
|
|
|
fun_ t u = Fun (Abs t (abstract (const Nothing) u))
|
|
|
|
|
|
|
|
cel :: Eq a => a -> Typ a -> Typ a -> Typ a
|
|
|
|
cel v t u = Cel (Abs t (abstract1 v u))
|
|
|
|
|
|
|
|
cel_ :: Typ a -> Typ a -> Typ a
|
|
|
|
cel_ t u = Cel (Abs t (abstract (const Nothing) u))
|
2019-11-15 05:35:45 +03:00
|
|
|
|
2019-12-04 02:50:21 +03:00
|
|
|
infixl 9 @:
|
|
|
|
(@:) = App
|
|
|
|
|
2019-12-09 08:46:52 +03:00
|
|
|
-- typing environment
|
2019-11-15 21:53:43 +03:00
|
|
|
type Env a = a -> Typ a
|
|
|
|
|
|
|
|
extend :: (b -> Typ a) -> Env a -> Env (Var b a)
|
|
|
|
extend handleNewBindings oldEnv = \case
|
|
|
|
-- TODO can we use Scope to decrease the cost of this?
|
|
|
|
B v -> F <$> handleNewBindings v
|
|
|
|
F v -> F <$> oldEnv v
|
|
|
|
|
|
|
|
extend1 :: Typ a -> Env a -> Env (Var () a)
|
2019-11-15 05:35:45 +03:00
|
|
|
extend1 t = extend \() -> t
|
|
|
|
|
2019-12-09 08:46:52 +03:00
|
|
|
-- amber rule assumptions
|
|
|
|
type Asm a = Set (Typ a, Typ a)
|
|
|
|
|
|
|
|
extendAsm :: (Ord a, Ord b) => Asm a -> Asm (Var b a)
|
|
|
|
extendAsm = Set.map \(t, u) -> (F <$> t, F <$> u)
|
|
|
|
|
2019-11-16 09:04:42 +03:00
|
|
|
type Typing = Maybe
|
|
|
|
|
2019-12-04 02:50:21 +03:00
|
|
|
-- TODO maybe this should be Typing () for error reporting?
|
|
|
|
-- think about env vs instantiate for bindings; if instantiate
|
|
|
|
-- as below, should the types be different?
|
2019-12-05 04:08:40 +03:00
|
|
|
-- better organize
|
2019-12-09 08:46:52 +03:00
|
|
|
nest :: Ord a => Env a -> Asm a -> Typ a -> Typ a -> Bool
|
|
|
|
nest _ _ Typ Typ = True
|
|
|
|
nest _ _ (Var v) (Var v') = v == v' -- TODO amber for Rec
|
|
|
|
nest env asm (Var v) u = nest env asm (env v) u
|
|
|
|
nest env asm t (Var v) = nest env asm t (env v)
|
2019-12-04 02:50:21 +03:00
|
|
|
-- following Cardelli 80something, we check the RHSs assuming
|
2019-12-05 04:08:40 +03:00
|
|
|
-- the putatively *lesser* of the LHSs for both
|
2019-12-09 08:46:52 +03:00
|
|
|
nest env asm (Fun (Abs a b)) (Fun (Abs a' b')) =
|
|
|
|
nest env asm a' a && nest (extend1 a' env) (extendAsm asm) (fromScope b) (fromScope b')
|
|
|
|
nest env asm (Cel (Abs a b)) (Cel (Abs a' b')) =
|
|
|
|
nest env asm a a' && nest (extend1 a env) (extendAsm asm) (fromScope b) (fromScope b')
|
|
|
|
nest env asm (Wut ls) (Wut ls') = ls `isSubsetOf` ls'
|
|
|
|
nest _ _ Lam{} _ = error "nest: lambda"
|
|
|
|
nest _ _ _ Lam{} = error "nest: lambda"
|
|
|
|
nest _ _ Cns{} _ = error "nest: cons"
|
|
|
|
nest _ _ _ Cns{} = error "nest: cons"
|
|
|
|
nest _ _ Tag{} _ = error "nest: tag"
|
|
|
|
nest _ _ _ Tag{} = error "nest: tag"
|
|
|
|
nest env asm t@App{} u = nest env asm (whnf t) u
|
|
|
|
nest env asm t u@App{} = nest env asm t (whnf u)
|
|
|
|
nest env asm t@Hed{} u = nest env asm (whnf t) u
|
|
|
|
nest env asm t u@Hed{} = nest env asm t (whnf u)
|
|
|
|
nest env asm t@Tal{} u = nest env asm (whnf t) u
|
|
|
|
nest env asm t u@Tal{} = nest env asm t (whnf u)
|
2019-12-09 07:38:43 +03:00
|
|
|
-- TODO meet and join bro
|
2019-12-09 08:46:52 +03:00
|
|
|
nest env asm (Cas t _ _) u = nest env asm t u
|
|
|
|
nest env asm t (Cas u _ _) = nest env asm t u
|
|
|
|
nest _ _ Rec{} _ = undefined
|
|
|
|
nest _ _ _ Rec{} = undefined
|
|
|
|
nest _ _ _ _ = False
|
2019-12-04 02:50:21 +03:00
|
|
|
|
2019-12-09 08:46:52 +03:00
|
|
|
check :: Ord a => Env a -> Exp a -> Typ a -> Typing ()
|
2019-11-16 09:04:42 +03:00
|
|
|
check env e t = do
|
|
|
|
t' <- infer env e
|
2019-12-09 08:46:52 +03:00
|
|
|
guard (nest env mempty t' t)
|
2019-11-16 09:04:42 +03:00
|
|
|
|
2019-12-09 08:46:52 +03:00
|
|
|
infer :: forall a. Ord a => Env a -> Exp a -> Typing (Typ a)
|
2019-11-15 05:35:45 +03:00
|
|
|
infer env = \case
|
2019-11-16 09:04:42 +03:00
|
|
|
Var v -> pure $ env v
|
2019-12-05 04:08:40 +03:00
|
|
|
Typ -> pure Typ
|
|
|
|
Fun (Abs t b) -> do
|
|
|
|
Typ <- infer env t
|
|
|
|
Typ <- infer (extend1 t env) (fromScope b)
|
|
|
|
pure Typ
|
|
|
|
Cel (Abs t b) -> do
|
|
|
|
Typ <- infer env t
|
|
|
|
Typ <- infer (extend1 t env) (fromScope b)
|
|
|
|
pure Typ
|
|
|
|
Wut _ -> pure Typ
|
2019-11-16 09:04:42 +03:00
|
|
|
Lam (Abs t b) -> do
|
2019-12-05 04:08:40 +03:00
|
|
|
-- TODO do I need (whnf -> Typ)? (and elsewhere)
|
2019-12-04 02:50:21 +03:00
|
|
|
Typ <- infer env t
|
2019-11-16 09:04:42 +03:00
|
|
|
(toScope -> t') <- infer (extend1 t env) (fromScope b)
|
|
|
|
pure $ Fun (Abs t t')
|
2019-12-05 04:08:40 +03:00
|
|
|
Cns x y -> do
|
|
|
|
-- Infer non-dependent pairs; if you want dependency, you must annotate
|
|
|
|
t <- infer env x
|
|
|
|
u <- infer env y
|
|
|
|
pure $ Cel (Abs t (abstract (const Nothing) u))
|
|
|
|
Tag t -> pure $ Wut (singleton t)
|
2019-12-09 07:38:43 +03:00
|
|
|
App x y -> do
|
|
|
|
Fun (Abs t b) <- infer env x
|
|
|
|
check env y t
|
|
|
|
pure $ whnf (instantiate1 y b)
|
2019-12-05 04:08:40 +03:00
|
|
|
Hed x -> do
|
|
|
|
Cel (Abs t _) <- infer env x
|
|
|
|
pure t
|
|
|
|
Tal x -> do
|
|
|
|
Cel (Abs _ u) <- infer env x
|
|
|
|
pure $ instantiate1 (whnf $ Hed $ x) u
|
2019-12-09 07:38:43 +03:00
|
|
|
Cas t x cs -> do
|
|
|
|
Typ <- infer env t
|
|
|
|
Wut ts <- infer env x
|
|
|
|
-- pretty restrictive - do we want?
|
|
|
|
guard (ts == keysSet cs)
|
|
|
|
traverse_ (\e -> check env e t) cs
|
|
|
|
pure t
|
2019-12-09 08:46:52 +03:00
|
|
|
Rec (Abs t b) -> do
|
|
|
|
Typ <- infer env t
|
|
|
|
-- todo can F <$> be made faster?
|
|
|
|
check (extend1 t env) (fromScope b) (F <$> t)
|
|
|
|
pure t
|
2019-11-16 09:04:42 +03:00
|
|
|
|
|
|
|
whnf :: Eq a => Exp a -> Exp a
|
|
|
|
whnf = \case
|
2019-12-09 08:46:52 +03:00
|
|
|
App (whnf -> Lam (Abs _ b)) x -> whnf $ instantiate1 x b
|
|
|
|
Hed (whnf -> Cns x _) -> whnf x
|
|
|
|
Tal (whnf -> Cns _ y) -> whnf y
|
|
|
|
Cas _ (whnf -> Tag t) cs -> whnf $ fromJust $ lookup t cs
|
2019-11-16 09:04:42 +03:00
|
|
|
e -> e
|