{-# 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 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 lam = Type_Type2 (Proxy (Lambda lam)) type instance Constraint2_of (Proxy (Lambda lam)) = Constraint2_Empty instance Unlift_Type1 (Type_Type2 (Proxy (Lambda lam))) where unlift_type1 (Type_Type2 px a b) k = k ( Type_Type1 (Proxy::Proxy (Lambda lam a)) b , Lift_Type1 (\(Type_Type1 _ b') -> Type_Type2 px a b') ) instance Eq_Type root => Eq_Type1 (Type_Type2 (Proxy (Lambda lam)) 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_Type1 Functor (Type_Fun lam root) instance Constraint_Type1 Applicative (Type_Fun lam root) instance Constraint_Type1 Traversable (Type_Fun lam root) instance Constraint_Type1 Monad (Type_Fun lam root) pattern Type_Fun :: root arg -> root res -> Type_Fun lam root ((Lambda lam) arg res) pattern Type_Fun arg res = Type_Type2 Proxy arg res instance -- Eq_Type Eq_Type root => Eq_Type (Type_Type2 (Proxy (Lambda lam)) 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 lam root) where string_from_type (Type_Type2 _ arg res) = "(" ++ string_from_type arg ++ " -> " ++ string_from_type res ++ ")" -- ** Type 'Lambda' -- | A newtype for the host-type function (->), -- wrapping argument and result within a type constructor @lam@, -- which is used in the 'Repr_Host' instance of 'Sym_Lambda' -- to control the calling (see 'val' and 'lazy'). -- -- NOTE: a newtype is used instead of a type synonym -- in order to be able to use it as a type constructor: @Lambda lam arg@, -- which for instance has instances: 'Functor', 'Applicative', and 'Monad'. newtype Lambda lam arg res = Lambda { unLambda :: (->) (lam arg) (lam res) } -- | Convenient alias to include a 'Type_Fun' within a type. type_fun :: forall lam root h_arg h_res. Lift_Type_Root (Type_Fun lam) root => root h_arg -> root h_res -> root (Lambda lam h_arg h_res) type_fun arg res = lift_type_root (Type_Fun arg res ::Type_Fun lam root (Lambda lam h_arg h_res)) -- | Parse 'Type_Fun'. type_fun_from :: forall (lam :: * -> *) (root :: * -> *) ast ret. ( Lift_Type_Root (Type_Fun lam) root , Type_from ast root , Root_of_Type root ~ root ) => Proxy (Type_Fun lam 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 (Lambda lam h_arg h_res))