{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE PatternSynonyms #-} {-# LANGUAGE Rank2Types #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeFamilies #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Language.Symantic.Type.Fun where import Data.Proxy import Data.Type.Equality ((:~:)(Refl)) import qualified Data.MonoTraversable as MT import Language.Symantic.Type.Root import Language.Symantic.Type.Error import Language.Symantic.Type.Type0 import Language.Symantic.Type.Type1 import Language.Symantic.Type.Type2 -- * Type 'Type_Fun' -- | The @->@ type. type Type_Fun = Type_Type2 (Proxy (->)) instance Unlift_Type1 Type_Fun where unlift_type1 (Type_Type2 px a b) k = k ( Type_Type1 (Proxy::Proxy ((->) a)) b , Lift_Type1 (\(Type_Type1 _ b') -> Type_Type2 px a b') ) instance Eq_Type root => Eq_Type1 (Type_Fun root) where eq_type1 (Type_Type2 _ a1 _b1) (Type_Type2 _ a2 _b2) | Just Refl <- eq_type a1 a2 = Just Refl eq_type1 _ _ = Nothing instance Constraint_Type Eq (Type_Fun root) instance Constraint_Type Ord (Type_Fun root) instance Constraint_Type Monoid root => Constraint_Type Monoid (Type_Fun root) where constraint_type c (Type_Type2 _ _arg res) | Just Dict <- constraint_type c res = Just Dict constraint_type _c _ = Nothing instance Constraint_Type Num (Type_Fun root) instance Constraint_Type Integral (Type_Fun root) instance Constraint_Type MT.MonoFunctor (Type_Fun root) where constraint_type _c Type_Type2{} = Just Dict instance Constraint_Type1 Functor (Type_Fun root) where constraint_type1 _c Type_Type2{} = Just Dict instance Constraint_Type1 Applicative (Type_Fun root) where constraint_type1 _c Type_Type2{} = Just Dict instance Constraint_Type1 Foldable (Type_Fun root) instance Constraint_Type1 Traversable (Type_Fun root) instance Constraint_Type1 Monad (Type_Fun root) where constraint_type1 _c Type_Type2{} = Just Dict pattern Type_Fun :: root arg -> root res -> Type_Fun root ((->) arg res) pattern Type_Fun arg res = Type_Type2 Proxy arg res instance -- Eq_Type Eq_Type root => Eq_Type (Type_Fun root) where eq_type (Type_Type2 _ arg1 res1) (Type_Type2 _ arg2 res2) | Just Refl <- arg1 `eq_type` arg2 , Just Refl <- res1 `eq_type` res2 = Just Refl eq_type _ _ = Nothing instance -- String_from_Type String_from_Type root => String_from_Type (Type_Fun root) where string_from_type (Type_Type2 _ arg res) = "(" ++ string_from_type arg ++ " -> " ++ string_from_type res ++ ")" -- | Convenient alias to include a 'Type_Fun' within a type. type_fun :: forall root h_arg h_res. Lift_Type_Root Type_Fun root => root h_arg -> root h_res -> root ((->) h_arg h_res) type_fun = type_type2 -- | Parse 'Type_Fun'. type_fun_from :: forall (root :: * -> *) ast ret. ( Lift_Type_Root Type_Fun root , Type_from ast root , Root_of_Type root ~ root ) => Proxy (Type_Fun root) -> ast -> ast -> (forall h. root h -> Either (Error_of_Type ast root) ret) -> Either (Error_of_Type ast root) ret type_fun_from _ty ast_arg ast_res k = type_from (Proxy::Proxy root) ast_arg $ \(ty_arg::root h_arg) -> type_from (Proxy::Proxy root) ast_res $ \(ty_res::root h_res) -> k (ty_arg `type_fun` ty_res :: root ((->) h_arg h_res))