1 {-# LANGUAGE UndecidableInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 module Language.Symantic.Typing.Term where
5 import Control.Arrow (left)
6 import qualified Control.Monad.Trans.State.Strict as SS
7 import qualified Data.Map.Strict as Map
9 import Language.Symantic.Parsing
10 import Language.Symantic.Typing.Kind
11 import Language.Symantic.Typing.Constant
12 import Language.Symantic.Typing.Constraint
13 import Language.Symantic.Typing.Type
14 import Language.Symantic.Typing.Quantification
16 import Debug.Trace (trace)
18 dbg :: Show a => [Char] -> a -> a
19 dbg msg x = trace (msg ++ " = " ++ show x) x
21 -- * Type 'Error_TeApp'
22 data Error_TeApp src cs
23 = Error_TeApp_FunArg (EType src cs)
24 -- ^ Cannot get the expected 'Type' of the argument of the function.
25 | Error_TeApp_MGU (Error_MGU src cs)
26 -- ^ Cannot unify the expected 'Type' of the argument of the function,
27 -- with the 'Type' of the argument.
28 | Error_TeApp_Term_missing (EType src cs)
29 -- ^ Cannot find a 'TeTerm', either in the function or in the argument.
31 | Error_TeApp_TyQuant_remaining (EType src cs)
32 -- ^ A 'TyQuant' remains to be instantiated.
34 | Error_TeApp_Term_not_applicable (EType src cs)
35 | Error_TeApp_Type_mismatch (EType src cs) (EType src cs)
41 ) => Show (Error_TeApp src cs)
43 instance Inj_Error (Error_TeApp src cs) (Error_TeApp src cs) where
45 instance Inj_Error (Error_MGU src cs) (Error_TeApp src cs) where
46 inj_Error = Error_TeApp_MGU
48 -- | Return the next free 'Var' considering all the 'KType's of given 'BinTree'.
49 freeTyVarTreeKT :: BinTree (KType src cs k) -> Var
50 freeTyVarTreeKT t = foldr max 0 $ ofKT freeTyVar <$> t
52 -- | Term application: apply second given 'KTerm' to the first,
53 -- applying embedded 'TeSym's if possible.
57 => Proj_TyConst cs (#>)
59 => Inj_Source (EType src cs) src
63 -> Either (Error_TeApp src cs) (KTerm src cs)
64 teApp (KT fun) (KT arg) = go fun arg
69 -> Either (Error_TeApp src cs) (KTerm src cs)
70 go f@TyConst{} _a = Left $ Error_TeApp_Term_missing (EType $ f `source` EType fun)
71 go (TyApp _sb (TyApp _sa (TyConst _src' a2b) a2b_a) a2b_b) a
72 | Just KRefl <- proj_TyConstKi @_ @(->) a2b
73 = case eqTy a2b_a a of
74 Just Refl -> Right $ KT a2b_b
75 Nothing -> Left $ Error_TeApp_Type_mismatch (EType a2b_a) (EType a)
76 go f@TyApp{} _a = Left $ Error_TeApp_Term_not_applicable (EType $ f `source` EType fun)
77 go f@TyVar{} _a = Left $ Error_TeApp_Term_missing (EType $ f `source` EType fun)
78 go t@(TyQuant src n kv f) a =
79 let v = freeTyVar t in
80 case f $ TyVar src kv v of
81 KT f' -> bindKT (polyTyVar n kv v) <$> go f' a
82 go (Term (TeSym te_fun) (TyApp _sb (TyApp _sa (TyConst _src' a2b) a2b_a) a2b_b)) a
83 | Just KRefl <- proj_TyConstKi @_ @(->) a2b
85 Term (TeSym te_arg) a' ->
87 Just Refl -> Right $ KT $ Term (TeSym $ sym_app te_fun te_arg) a2b_b
88 Nothing -> Left $ Error_TeApp_Type_mismatch (EType a2b_a) (EType a')
89 _ -> Left $ Error_TeApp_Term_missing (EType $ a `source` EType arg)
90 go (Term _te x) _a = Left $ Error_TeApp_Term_not_applicable (EType $ x `source` EType fun)
92 -- | Monomorphize a 'BinTree' of 'KT'.
93 monoTreeKT :: BinTree (KT src cs k) -> BinTree (KT src cs k)
95 (`SS.evalState` freeTyVarTreeKT t) $
96 traverse (monoTy `ofKT`) t
98 -- | Fold given 'BinTree' of 'KTerm' in order to compute
99 -- a resulting 'KTerm', if possible.
101 :: Inj_Source (EType src cs) src
103 => Proj_TyConst cs (->)
104 => Proj_TyConst cs (#>)
106 => BinTree (KTerm src cs)
107 -> Either (Error_TeApp src cs) (MapTyVar src cs, Maybe (KTerm src cs))
109 foldl (\acc (KT arg) -> do
110 (mgu, may_fun) <- acc
112 Nothing -> Right (mgu, Just (KT arg))
115 Nothing -> inj_Error $ Error_TeApp_FunArg $ EType fun
116 Just (KT fun_arg, KT _fun_res) -> do
117 mgu' <- inj_Error `left` mguTy mgu fun_arg arg
118 let fun' = substTyVar mgu' fun
119 let arg' = substTyVar mgu' arg
120 KT res <- teApp fun' arg'
121 Right (mgu', Just (KT res)))
122 (Right (Map.empty, Nothing)) $
130 evalTe (Term te _ty) = Just te
131 evalTe TyQuant{} = Nothing
132 evalTe TyConst{} = Nothing
133 evalTe TyVar{} = Nothing
134 evalTe TyApp{} = Nothing