{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE Rank2Types #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE UndecidableInstances #-} module Language.LOL.Symantic.Type.Fun where import Data.Maybe (isJust) import Data.Type.Equality ((:~:)(Refl)) import Data.Proxy import Language.LOL.Symantic.Type.Common -- * Type 'Type_Fun' -- | The function type. data Type_Fun lam root h where Type_Fun :: root h_arg -> root h_res -> Type_Fun lam root (Lambda lam h_arg h_res) type instance Root_of_Type (Type_Fun lam root) = root type instance Error_of_Type ast (Type_Fun lam root) = () instance -- Type_Eq Type_Eq root => Type_Eq (Type_Fun lam root) where type_eq (arg1 `Type_Fun` res1) (arg2 `Type_Fun` res2) | Just Refl <- arg1 `type_eq` arg2 , Just Refl <- res1 `type_eq` res2 = Just Refl type_eq _ _ = Nothing instance -- Eq Type_Eq root => Eq (Type_Fun lam root h) where x == y = isJust $ x `type_eq` y type_fun_from :: forall (lam :: * -> *) (root :: * -> *) ast ret. ( Type_Root_Lift (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_of_Type root)) ret type_fun_from (_px_ty::Proxy (Type_Fun lam root)) (ast_arg::ast) (ast_res::ast) (k::(forall h. Root_of_Type (Type_Fun lam root) h -> Either (Error_of_Type ast (Root_of_Type (Type_Fun lam root))) ret)) = 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)) instance -- String_from_Type String_from_Type root => String_from_Type (Type_Fun lam root) where string_from_type (arg `Type_Fun` res) = "(" ++ string_from_type arg ++ " -> " ++ string_from_type res ++ ")" instance -- Show String_from_Type root => Show (Type_Fun lam root h) where show = string_from_type -- | Convenient alias to include a 'Type_Fun' within a type. type_fun :: Type_Root_Lift (Type_Fun lam) root => root h_arg -> root h_res -> root (Lambda lam h_arg h_res) type_fun arg res = type_root_lift (Type_Fun arg res) -- ** Type 'Lambda' -- | A type synonym 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 implement 'val' and 'lazy'. type Lambda lam arg res = lam arg -> lam res