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)
40 , Proj_TyConst cs (->)
41 , Proj_TyConst cs (#>)
42 ) => Show (Error_TeApp src cs)
44 instance Inj_Error (Error_TeApp src cs) (Error_TeApp src cs) where
46 instance Inj_Error (Error_MGU src cs) (Error_TeApp src cs) where
47 inj_Error = Error_TeApp_MGU
49 -- | Return the next free 'Var' considering all the 'KType's of given 'BinTree'.
50 freeTyVarTreeKT :: BinTree (KType src cs k) -> Var
51 freeTyVarTreeKT t = foldr max 0 $ ofKT freeTyVar <$> t
53 -- | Term application: apply second given 'KTerm' to the first,
54 -- applying embedded 'TeSym's if possible.
58 => Proj_TyConst cs (#>)
60 => Inj_Source (EType src cs) src
64 -> Either (Error_TeApp src cs) (KTerm src cs)
65 teApp (KT fun) (KT arg) = go fun arg
70 -> Either (Error_TeApp src cs) (KTerm src cs)
71 go f@TyConst{} _a = Left $ Error_TeApp_Term_missing (EType $ f `source` EType fun)
72 go (TyApp _sb (TyApp _sa (TyConst _src' a2b) a2b_a) a2b_b) a
73 | Just KRefl <- projKiTyConst @_ @(->) a2b
74 = case eqTy a2b_a a of
75 Just Refl -> Right $ KT a2b_b
76 Nothing -> Left $ Error_TeApp_Type_mismatch (EType a2b_a) (EType a)
77 go f@TyApp{} _a = Left $ Error_TeApp_Term_not_applicable (EType $ f `source` EType fun)
78 go f@TyVar{} _a = Left $ Error_TeApp_Term_missing (EType $ f `source` EType fun)
79 go t@(TyQuant src n kv f) a =
80 let v = freeTyVar t in
81 case f $ TyVar src kv v of
82 KT f' -> bindKT (polyTyVar n kv v) <$> go f' a
83 go (Term (TeSym te_fun) (TyApp _sb (TyApp _sa (TyConst _src' a2b) a2b_a) a2b_b)) a
84 | Just KRefl <- projKiTyConst @_ @(->) a2b
86 Term (TeSym te_arg) a' ->
88 Just Refl -> Right $ KT $ Term (TeSym $ sym_app te_fun te_arg) a2b_b
89 Nothing -> Left $ Error_TeApp_Type_mismatch (EType a2b_a) (EType a')
90 _ -> Left $ Error_TeApp_Term_missing (EType $ a `source` EType arg)
91 go (Term _te x) _a = Left $ Error_TeApp_Term_not_applicable (EType $ x `source` EType fun)
93 -- | Monomorphize a 'BinTree' of 'KT'.
94 monoTreeKT :: BinTree (KT src cs k) -> BinTree (KT src cs k)
96 (`SS.evalState` freeTyVarTreeKT t) $
97 traverse (monoTy `ofKT`) t
99 -- | Fold given 'BinTree' of 'KTerm' in order to compute
100 -- a resulting 'KTerm', if possible.
102 :: Inj_Source (EType src cs) src
104 => Proj_TyConst cs (->)
105 => Proj_TyConst cs (#>)
107 => BinTree (KTerm src cs)
108 -> Either (Error_TeApp src cs) (MapTyVar src cs, Maybe (KTerm src cs))
110 foldl (\acc (KT arg) -> do
111 (mgu, may_fun) <- acc
113 Nothing -> Right (mgu, Just (KT arg))
116 Nothing -> inj_Error $ Error_TeApp_FunArg $ EType fun
117 Just (KT fun_arg, KT _fun_res) -> do
118 mgu' <- inj_Error `left` mguTy mgu fun_arg arg
119 let fun' = substTyVar mgu' fun
120 let arg' = substTyVar mgu' arg
121 KT res <- teApp fun' arg'
122 Right (mgu', Just (KT res)))
123 (Right (Map.empty, Nothing)) $
131 evalTe (Term te _ty) = Just te
132 evalTe TyQuant{} = Nothing
133 evalTe TyConst{} = Nothing
134 evalTe TyVar{} = Nothing
135 evalTe TyApp{} = Nothing