{-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE DefaultSignatures #-} {-# LANGUAGE ExistentialQuantification #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE Rank2Types #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE UndecidableInstances #-} module Language.Symantic.Type.Common where import Data.Maybe (isJust, fromMaybe) import Data.Proxy import Data.Type.Equality ((:~:)(Refl)) import Language.Symantic.Lib.Data.Peano import GHC.Prim (Constraint) -- * Class 'Eq_Type' -- | Test two types for equality, -- returning an Haskell type-level proof -- of the equality when it holds. class Eq_Type (ty:: * -> *) where eq_type :: forall h1 h2. ty h1 -> ty h2 -> Maybe (h1 :~: h2) -- * Class 'Eq_Type1' -- | Test two type constructors for equality, -- returning an Haskell type-level proof -- of the equality when it holds. class Eq_Type1 (ty:: * -> *) where eq_type1 :: forall h1 h2 a1 a2. ty (h1 a1) -> ty (h2 a2) -> Maybe (h1 :~: h2) eq_type1 = error "eq_type1" -- * Class 'Constraint_Type' -- | Test if a type satisfies a given 'Constraint', -- returning an Haskell type-level proof -- of that satisfaction when it holds. class Constraint_Type (c:: * -> Constraint) (ty:: * -> *) where constraint_type :: forall h. Proxy c -> ty h -> Maybe (Dict (c h)) constraint_type _c _ = Nothing -- * Class 'Constraint_Type1' -- | Test if a type constructor satisfies a given 'Constraint', -- returning an Haskell type-level proof -- of that satisfaction when it holds. class Constraint_Type1 (c:: (* -> *) -> Constraint) (ty:: * -> *) where constraint_type1 :: forall h1 h. Proxy c -> ty (h1 h) -> Maybe (Dict (c h1)) constraint_type1 _c _ = Nothing -- ** Type 'Dict' -- | 'Dict' captures the dictionary of a 'Constraint': -- pattern matching on the 'Dict' constructor -- brings the 'Constraint' into scope. data Dict :: Constraint -> * where Dict :: c => Dict c -- * Type 'Exists_Dict' data Exists_Dict = forall c. Exists_Dict (Dict c) -- * Class 'Type_from' -- | Parse given @ast@ into a 'Root_of_Type', -- or return an 'Error_of_Type'. -- -- NOTE: making a distinction between @ty@ and 'Root_of_Type'@ ty@, -- instead of having only a @root@ variable -- is what enables to define many instances, one per type. class Type_from ast (ty:: * -> *) where type_from :: Proxy ty -> ast -> (forall h. Root_of_Type ty h -> Either (Error_of_Type ast (Root_of_Type ty)) ret) -> Either (Error_of_Type ast (Root_of_Type ty)) ret -- * Class 'Type1_from' -- | Parse given @ast@ into a 'Root_of_Type' constructor, -- or return an 'Error_of_Type'. class Type1_from ast (ty:: * -> *) where type1_from :: Proxy ty -> ast -> (forall (f:: * -> *). Proxy f -> (forall h. Root_of_Type ty h -> Root_of_Type ty (f h)) -> Either (Error_of_Type ast (Root_of_Type ty)) ret) -> Either (Error_of_Type ast (Root_of_Type ty)) ret default type1_from :: ( Lift_Error_Type (Error_Type ast) (Error_of_Type ast (Root_of_Type ty)) , Implicit_HBool (Is_Last_Type ty (Root_of_Type ty)) ) => Proxy ty -> ast -> (forall (f:: * -> *). Proxy f -> (forall h. Root_of_Type ty h -> Root_of_Type ty (f h)) -> Either (Error_of_Type ast (Root_of_Type ty)) ret) -> Either (Error_of_Type ast (Root_of_Type ty)) ret type1_from ty ast _k = Left $ error_type_unsupported ty ast {- class Type1_f_from (ty:: * -> *) where type1_f_from :: Proxy ty -> (Root_of_Type ty) h_any -> (forall (f:: * -> *). Proxy f -> (forall h. (Root_of_Type ty) h -> (Root_of_Type ty) (f h)) -> Either ({-Error_of_Type ast (Root_of_Type ty)-}) ret) -> Either ({-Error_of_Type ast (Root_of_Type ty)-}) ret instance -- Type1_f_from ( Eq_Type (curr root) , Type1_f_from ast (curr root) , Type1_f_from ast (next root) , Root_of_Type (curr root) ~ root , Root_of_Type (next root) ~ root -- , Unlift_Error_Type (Error_Type ast) (Error_of_Type ast root) ) => Type1_from ast (Type_Alt curr next root) where type1_from _ty ast k = case type1_from (Proxy::Proxy (curr root)) ast (\f ty -> Right $ k f ty) of Right ret -> ret Left err -> case unlift_error_type err of Just (Error_Type_Unsupported_here (_::ast)) -> type1_from (Proxy::Proxy (next root)) ast k _ -> Left err -} -- ** Type family 'Root_of_Type' -- | Return the root type of a type. type family Root_of_Type (ty:: * -> *) :: * -> * -- ** Type family 'Error_of_Type' -- | Return the error type of a type. type family Error_of_Type (ast:: *) (ty:: * -> *) :: * -- * Type 'No_Type' -- | A discarded type. data No_Type (root:: * -> *) h = No_Type (root h) deriving (Eq, Show) -- * Type 'Type_Root' -- | The root type, passing itself as parameter to the given type. newtype Type_Root (ty:: (* -> *) -> * -> *) h = Type_Root { unType_Root :: ty (Type_Root ty) h } type instance Root_of_Type (Type_Root ty) = Type_Root ty -- type instance Root_of_Type (ty (Type_Root ty)) = Type_Root ty type instance Error_of_Type ast (Type_Root ty) = Error_Type_Alt (Error_Type ast) (Error_of_Type ast (ty (Type_Root ty))) instance -- Eq_Type Eq_Type (ty (Type_Root ty)) => Eq_Type (Type_Root ty) where eq_type (Type_Root x) (Type_Root y) = x `eq_type` y instance -- Eq_Type1 Eq_Type1 (ty (Type_Root ty)) => Eq_Type1 (Type_Root ty) where eq_type1 (Type_Root x) (Type_Root y) = x `eq_type1` y instance -- Eq Eq_Type (Type_Root ty) => Eq (Type_Root ty h) where x == y = isJust $ x `eq_type` y instance -- Constraint_Type c Type_Root Constraint_Type c (ty (Type_Root ty)) => Constraint_Type c (Type_Root ty) where constraint_type c (Type_Root ty) = constraint_type c ty instance -- Constraint_Type1 c Type_Root Constraint_Type1 c (ty (Type_Root ty)) => Constraint_Type1 c (Type_Root ty) where constraint_type1 c (Type_Root ty) = constraint_type1 c ty instance -- Type_from ( Eq_Type (Type_Root ty) , Type_from ast (ty (Type_Root ty)) , Root_of_Type (ty (Type_Root ty)) ~ Type_Root ty ) => Type_from ast (Type_Root ty) where type_from _ty = type_from (Proxy::Proxy (ty (Type_Root ty))) instance -- Type1_from ( Eq_Type (Type_Root ty) , Type1_from ast (ty (Type_Root ty)) , Root_of_Type (ty (Type_Root ty)) ~ Type_Root ty ) => Type1_from ast (Type_Root ty) where type1_from _ty = type1_from (Proxy::Proxy (ty (Type_Root ty))) instance -- String_from_Type String_from_Type (ty (Type_Root ty)) => String_from_Type (Type_Root ty) where string_from_type (Type_Root ty) = string_from_type ty instance -- Show String_from_Type (Type_Root ty) => Show (Type_Root ty h) where show = string_from_type -- ** Class 'Lift_Type_Root' -- | Lift a given type to a given root type. class Lift_Type_Root ty root where lift_type_root :: forall h. ty root h -> root h instance Lift_Type ty root => Lift_Type_Root ty (Type_Root root) where lift_type_root = Type_Root . lift_type -- * Type 'Type_Alt' -- | Type making an alternative between two types. data Type_Alt curr next (root:: * -> *) h = Type_Alt_Curr (curr root h) | Type_Alt_Next (next root h) -- | Convenient alias. Requires @TypeOperators@. type (:|:) = Type_Alt infixr 5 :|: type instance Root_of_Type (Type_Alt curr next root) = root type instance Error_of_Type ast (Type_Alt curr next root) = Error_of_Type_Alt ast (Error_of_Type ast (curr root)) (Error_of_Type ast (next root)) -- ** Type family 'Error_of_Type_Alt' -- | Remove 'No_Error_Type' type when building 'Error_of_Type'. type family Error_of_Type_Alt ast curr next where Error_of_Type_Alt ast curr No_Error_Type = curr Error_of_Type_Alt ast No_Error_Type next = next Error_of_Type_Alt ast curr next = Error_Type_Alt curr next -- ** Type 'No_Error_Type' -- | A discarded error. data No_Error_Type = No_Error_Type deriving (Eq, Show) instance -- Eq_Type ( Eq_Type (curr root) , Eq_Type (next root) ) => Eq_Type (Type_Alt curr next root) where eq_type (Type_Alt_Curr x) (Type_Alt_Curr y) = x `eq_type` y eq_type (Type_Alt_Next x) (Type_Alt_Next y) = x `eq_type` y eq_type _ _ = Nothing instance -- Eq_Type1 ( Eq_Type1 (curr root) , Eq_Type1 (next root) ) => Eq_Type1 (Type_Alt curr next root) where eq_type1 (Type_Alt_Curr x) (Type_Alt_Curr y) = x `eq_type1` y eq_type1 (Type_Alt_Next x) (Type_Alt_Next y) = x `eq_type1` y eq_type1 _ _ = Nothing instance -- Eq ( Eq_Type (curr root) , Eq_Type (next root) ) => Eq (Type_Alt curr next root h) where x == y = isJust $ x `eq_type` y instance -- Constraint_Type c Type_Alt ( Constraint_Type c (curr root) , Constraint_Type c (next root) ) => Constraint_Type c (Type_Alt curr next root) where constraint_type c (Type_Alt_Curr ty) = constraint_type c ty constraint_type c (Type_Alt_Next ty) = constraint_type c ty instance -- Constraint_Type1 c Type_Alt ( Constraint_Type1 c (curr root) , Constraint_Type1 c (next root) ) => Constraint_Type1 c (Type_Alt curr next root) where constraint_type1 c (Type_Alt_Curr ty) = constraint_type1 c ty constraint_type1 c (Type_Alt_Next ty) = constraint_type1 c ty instance -- Type_from ( Eq_Type (curr root) , Type_from ast (curr root) , Type_from ast (next root) , Root_of_Type (curr root) ~ root , Root_of_Type (next root) ~ root , Unlift_Error_Type (Error_Type ast) (Error_of_Type ast root) ) => Type_from ast (Type_Alt curr next root) where type_from _ty ast k = case type_from (Proxy::Proxy (curr root)) ast (Right . k) of Right ret -> ret Left err -> case unlift_error_type err of Just (Error_Type_Unsupported_here (_::ast)) -> type_from (Proxy::Proxy (next root)) ast k _ -> Left err instance -- Type1_from ( Eq_Type (curr root) , Type1_from ast (curr root) , Type1_from ast (next root) , Root_of_Type (curr root) ~ root , Root_of_Type (next root) ~ root , Unlift_Error_Type (Error_Type ast) (Error_of_Type ast root) ) => Type1_from ast (Type_Alt curr next root) where type1_from _ty ast k = case type1_from (Proxy::Proxy (curr root)) ast (\f ty -> Right $ k f ty) of Right ret -> ret Left err -> case unlift_error_type err of Just (Error_Type_Unsupported_here (_::ast)) -> type1_from (Proxy::Proxy (next root)) ast k _ -> Left err instance -- String_from_Type ( String_from_Type (curr root) , String_from_Type (next root) ) => String_from_Type (Type_Alt curr next root) where string_from_type (Type_Alt_Curr t) = string_from_type t string_from_type (Type_Alt_Next t) = string_from_type t -- * Type 'Type_Type0' -- | A type of kind @*@. data Type_Type0 px (root:: * -> *) h where Type_Type0 :: px -> Type_Type0 px root (Host_of px) type instance Root_of_Type (Type_Type0 px root) = root type instance Error_of_Type ast (Type_Type0 px root) = No_Error_Type instance -- Eq_Type Eq_Type (Type_Type0 (Proxy h0) root) where eq_type Type_Type0{} Type_Type0{} = Just Refl instance -- Eq_Type Eq_Type (Type_Type0 EPeano root) where eq_type (Type_Type0 p1) (Type_Type0 p2) | p1 == p2 = Just Refl eq_type _ _ = Nothing instance -- Eq Eq_Type (Type_Type0 px root) => Eq (Type_Type0 px root h) where x == y = isJust $ x `eq_type` y instance -- Show String_from_Type (Type_Type0 (Proxy h0) root) => Show (Type_Type0 (Proxy h0) root h0) where show = string_from_type instance Eq h0 => Constraint_Type Eq (Type_Type0 (Proxy h0) root) where constraint_type _c Type_Type0{} = Just Dict instance Ord h0 => Constraint_Type Ord (Type_Type0 (Proxy h0) root) where constraint_type _c Type_Type0{} = Just Dict instance Unlift_Type1 (Type_Type0 px) instance Eq_Type1 (Type_Type0 (Proxy h0) root) -- | Convenient alias to include a 'Type_Type0' within a type. type_type0 :: forall root h0. Lift_Type_Root (Type_Type0 (Proxy h0)) root => root h0 type_type0 = lift_type_root (Type_Type0 (Proxy::Proxy h0)) -- ** Type family 'Host_of' type family Host_of px :: * type instance Host_of (Proxy h0) = h0 -- ** Type 'Lift_Type' -- | Apply 'Peano_of_Type' on 'Lift_TypeP'. type Lift_Type ty tys = Lift_TypeP (Peano_of_Type ty tys) ty tys -- *** Type 'Peano_of_Type' -- | Return a 'Peano' number derived from the location -- of a given type within a given type stack, -- which is used to avoid @OverlappingInstances@. type family Peano_of_Type (ty:: (* -> *) -> * -> *) (tys:: (* -> *) -> * -> *) :: * where Peano_of_Type ty ty = Zero Peano_of_Type ty (Type_Alt ty next) = Zero Peano_of_Type other (Type_Alt curr next) = Succ (Peano_of_Type other next) -- *** Class 'Lift_TypeP' -- | Lift a given type to the top of a given type stack including it, -- by constructing the appropriate sequence of 'Type_Alt_Curr' and 'Type_Alt_Next'. class Lift_TypeP (p:: *) ty tys where lift_typeP :: forall (root:: * -> *) h. Proxy p -> ty root h -> tys root h instance Lift_TypeP Zero curr curr where lift_typeP _ = id instance Lift_TypeP Zero curr (Type_Alt curr next) where lift_typeP _ = Type_Alt_Curr instance Lift_TypeP p other next => Lift_TypeP (Succ p) other (Type_Alt curr next) where lift_typeP _ = Type_Alt_Next . lift_typeP (Proxy::Proxy p) -- | Convenient wrapper around 'lift_typeP', -- passing it the 'Peano' number from 'Peano_of_Type'. lift_type :: forall ty tys (root:: * -> *) h. Lift_Type ty tys => ty root h -> tys root h lift_type = lift_typeP (Proxy::Proxy (Peano_of_Type ty tys)) -- ** Type 'Unlift_Type' -- | Apply 'Peano_of_Type' on 'Unlift_TypeP'. type Unlift_Type ty tys = Unlift_TypeP (Peano_of_Type ty tys) ty tys -- *** Class 'Unlift_TypeP' -- | Try to unlift a given type out of a given type stack including it, -- by deconstructing the appropriate sequence of 'Type_Alt_Curr' and 'Type_Alt_Next'. class Unlift_TypeP (p:: *) ty tys where type_unliftN :: forall (root:: * -> *) h. Proxy p -> tys root h -> Maybe (ty root h) instance Unlift_TypeP Zero curr curr where type_unliftN _ = Just instance Unlift_TypeP Zero curr (Type_Alt curr next) where type_unliftN _ (Type_Alt_Curr x) = Just x type_unliftN _ (Type_Alt_Next _) = Nothing instance Unlift_TypeP p other next => Unlift_TypeP (Succ p) other (Type_Alt curr next) where type_unliftN _ (Type_Alt_Next x) = type_unliftN (Proxy::Proxy p) x type_unliftN _ (Type_Alt_Curr _) = Nothing -- | Convenient wrapper around 'type_unliftN', -- passing it the 'Peano' number from 'Peano_of_Type'. unlift_type :: forall ty tys (root:: * -> *) h. Unlift_Type ty tys => tys root h -> Maybe (ty root h) unlift_type = type_unliftN (Proxy::Proxy (Peano_of_Type ty tys)) -- * Type 'Type_Type1' -- | A type of kind @(k -> *)@. data Type_Type1 px (root:: * -> *) h where Type_Type1 :: px -> root a -> Type_Type1 px root ((Host1_of px) a) type instance Root_of_Type (Type_Type1 px root) = root type instance Error_of_Type ast (Type_Type1 px root) = No_Error_Type instance Constraint_Type Eq (Type_Type1 (Proxy h1) root) instance Constraint_Type Ord (Type_Type1 (Proxy h1) root) instance -- Eq_Type Eq_Type root => Eq_Type (Type_Type1 (Proxy h1) root) where eq_type (Type_Type1 _px1 a1) (Type_Type1 _px2 a2) | Just Refl <- a1 `eq_type` a2 = Just Refl eq_type _ _ = Nothing instance -- Eq_Type Eq_Type root => Eq_Type (Type_Type1 EPeano root) where eq_type (Type_Type1 p1 a1) (Type_Type1 p2 a2) | p1 == p2 , Just Refl <- a1 `eq_type` a2 = Just Refl eq_type _ _ = Nothing instance -- Eq Eq_Type root => Eq (Type_Type1 (Proxy h1) root h) where x == y = isJust $ eq_type x y instance -- Eq Eq_Type root => Eq (Type_Type1 EPeano root h) where x == y = isJust $ eq_type x y instance -- Eq_Type1 Eq_Type1 (Type_Type1 (Proxy t1) root) where eq_type1 Type_Type1{} Type_Type1{} = Just Refl instance -- Show String_from_Type (Type_Type1 px root) => Show (Type_Type1 px root h) where show = string_from_type -- ** Type family 'Host1_of' type family Host1_of px :: * -> * type instance Host1_of (Proxy h1) = h1 -- ** Type 'Lift_Type1' data Lift_Type1 px tys = Lift_Type1 ( forall (root:: * -> *) h . Type_Type1 px root h -> tys root h ) -- ** Type 'Unlift_Type1' class Unlift_Type1 ty where unlift_type1 :: forall (root:: * -> *) ret h. ty root h -> (forall (px:: *). ( Type_Type1 px root h , Lift_Type1 px ty ) -> Maybe ret) -> Maybe ret unlift_type1 _ty _k = Nothing instance Unlift_Type1 (Type_Type1 px) where unlift_type1 ty k = k (ty, Lift_Type1 id) instance -- Type_Alt ( Unlift_Type1 curr , Unlift_Type1 next ) => Unlift_Type1 (Type_Alt curr next) where unlift_type1 (Type_Alt_Curr ty) k = fromMaybe Nothing $ unlift_type1 ty $ \(t, Lift_Type1 l) -> Just $ k (t, Lift_Type1 $ Type_Alt_Curr . l) unlift_type1 (Type_Alt_Next ty) k = fromMaybe Nothing $ unlift_type1 ty $ \(t, Lift_Type1 l) -> Just $ k (t, Lift_Type1 $ Type_Alt_Next . l) -- * Type 'Type_Type2' data Type_Type2 px (root:: * -> *) h where Type_Type2 :: (Constraint2_of px) a b => px -> root a -> root b -> Type_Type2 px root ((Host2_of px) a b) class Constraint2_Empty a b instance Constraint2_Empty a b type instance Root_of_Type (Type_Type2 px root) = root type instance Error_of_Type ast (Type_Type2 px root) = No_Error_Type instance -- Eq_Type Eq_Type root => Eq_Type (Type_Type2 (Proxy h2) 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 -- Eq_Type Eq_Type root => Eq_Type (Type_Type2 EPeano root) where eq_type (Type_Type2 p1 arg1 res1) (Type_Type2 p2 arg2 res2) | p1 == p2 , Just Refl <- arg1 `eq_type` arg2 , Just Refl <- res1 `eq_type` res2 = Just Refl eq_type _ _ = Nothing instance -- Eq Eq_Type root => Eq (Type_Type2 (Proxy h2) root h) where x == y = isJust $ x `eq_type` y instance -- Eq Eq_Type root => Eq (Type_Type2 EPeano root h) where x == y = isJust $ x `eq_type` y instance -- Show ( String_from_Type root , String_from_Type (Type_Type2 px root) ) => Show (Type_Type2 px root h) where show = string_from_type instance Constraint_Type Eq (Type_Type2 px root) instance Constraint_Type Ord (Type_Type2 px root) instance Unlift_Type1 (Type_Type2 px) -- FIXME: should be doable instance Eq_Type1 (Type_Type2 px root) -- FIXME: should be doable -- ** Type 'Host2_of' type family Host2_of px :: * -> * -> * type instance Host2_of (Proxy h2) = h2 -- ** Type 'Constraint2_of' type family Constraint2_of px :: (* -> * -> Constraint) -- * Type family 'Is_Last_Type' -- | Return whether a given type is the last one in a given type stack. -- -- NOTE: each type parser uses this type family -- when it encounters unsupported syntax: -- to know if it is the last type parser component that will be tried -- (and thus return 'Error_Type_Unsupported') -- or if some other type parser component shall be tried -- (and thus return 'Error_Type_Unsupported_here', -- which is then handled accordingly by the 'Type_from' instance of 'Type_Alt'). type family Is_Last_Type (ty:: * -> *) (tys:: * -> *) :: Bool where Is_Last_Type ty ty = 'True Is_Last_Type ty (Type_Root tys) = Is_Last_Type ty (tys (Type_Root tys)) Is_Last_Type (ty root) (Type_Alt ty next root) = 'False Is_Last_Type other (Type_Alt curr next root) = Is_Last_Type other (next root) -- * Type 'Error_Type_Alt' -- | Error type making an alternative between two error types. data Error_Type_Alt curr next = Error_Type_Alt_Curr curr | Error_Type_Alt_Next next deriving (Eq, Show) -- ** Type 'Lift_Error_Type' type Lift_Error_Type err errs = Lift_Error_TypeP (Peano_of_Error_Type err errs) err errs -- *** Type 'Peano_of_Error_Type' -- | Return a 'Peano' number derived from the location -- of a given error type within a given error type stack. type family Peano_of_Error_Type (err:: *) (errs:: *) :: * where Peano_of_Error_Type err err = Zero Peano_of_Error_Type err (Error_Type_Alt err next) = Zero Peano_of_Error_Type other (Error_Type_Alt curr next) = Succ (Peano_of_Error_Type other next) -- *** Class 'Lift_Error_TypeP' -- | Lift a given error type to the top of a given error type stack including it, -- by constructing the appropriate sequence of 'Error_Type_Alt_Curr' and 'Error_Type_Alt_Next'. class Lift_Error_TypeP (p:: *) err errs where lift_error_typeP :: Proxy p -> err -> errs instance Lift_Error_TypeP Zero curr curr where lift_error_typeP _ = id instance Lift_Error_TypeP Zero curr (Error_Type_Alt curr next) where lift_error_typeP _ = Error_Type_Alt_Curr instance Lift_Error_TypeP p other next => Lift_Error_TypeP (Succ p) other (Error_Type_Alt curr next) where lift_error_typeP _ = Error_Type_Alt_Next . lift_error_typeP (Proxy::Proxy p) -- | Convenient wrapper around 'error_type_unliftN', -- passing it the 'Peano' number from 'Peano_of_Error_Type'. lift_error_type :: forall err errs. Lift_Error_Type err errs => err -> errs lift_error_type = lift_error_typeP (Proxy::Proxy (Peano_of_Error_Type err errs)) -- ** Type 'Unlift_Error_Type' -- | Apply 'Peano_of_Error_Type' on 'Error_Type_UnliftN'. type Unlift_Error_Type ty tys = Error_Type_UnliftN (Peano_of_Error_Type ty tys) ty tys -- | Convenient wrapper around 'error_type_unliftN', -- passing it the 'Peano' number from 'Peano_of_Error_Type'. unlift_error_type :: forall ty tys. Unlift_Error_Type ty tys => tys -> Maybe ty unlift_error_type = error_type_unliftN (Proxy::Proxy (Peano_of_Error_Type ty tys)) -- *** Class 'Error_Type_UnliftN' -- | Try to unlift a given type error out of a given type error stack including it, -- by deconstructing the appropriate sequence of 'Error_Type_Alt_Curr' and 'Error_Type_Alt_Next'. class Error_Type_UnliftN (p:: *) ty tys where error_type_unliftN :: Proxy p -> tys -> Maybe ty instance Error_Type_UnliftN Zero curr curr where error_type_unliftN _ = Just instance Error_Type_UnliftN Zero curr (Error_Type_Alt curr next) where error_type_unliftN _ (Error_Type_Alt_Curr x) = Just x error_type_unliftN _ (Error_Type_Alt_Next _) = Nothing instance Error_Type_UnliftN p other next => Error_Type_UnliftN (Succ p) other (Error_Type_Alt curr next) where error_type_unliftN _ (Error_Type_Alt_Next x) = error_type_unliftN (Proxy::Proxy p) x error_type_unliftN _ (Error_Type_Alt_Curr _) = Nothing -- ** Type 'Error_Type_Read' -- | Common type errors. data Error_Type ast = Error_Type_Unsupported ast -- ^ Given syntax is supported by none -- of the type parser components -- of the type stack. | Error_Type_Unsupported_here ast -- ^ Given syntax not supported by the current type parser component. | Error_Type_Wrong_number_of_arguments ast Int deriving (Eq, Show) -- | Convenient wrapper around 'lift_error_type', -- passing the type family boilerplate. error_type :: Lift_Error_Type (Error_Type ast) (Error_of_Type ast (Root_of_Type ty)) => Proxy ty -> Error_Type ast -> Error_of_Type ast (Root_of_Type ty) error_type _ = lift_error_type error_type_unsupported :: forall ast ty. ( Implicit_HBool (Is_Last_Type ty (Root_of_Type ty)) , Lift_Error_Type (Error_Type ast) (Error_of_Type ast (Root_of_Type ty)) ) => Proxy ty -> ast -> Error_of_Type ast (Root_of_Type ty) error_type_unsupported ty ast = case hbool :: HBool (Is_Last_Type ty (Root_of_Type ty)) of HTrue -> error_type ty $ Error_Type_Unsupported ast HFalse -> error_type ty $ Error_Type_Unsupported_here ast -- * Class 'String_from_Type' -- | Return a 'String' from a type. class String_from_Type ty where string_from_type :: ty h -> String -- * Type 'Exists_Type' -- | Existential data type wrapping the index of a type. data Exists_Type ty = forall h. Exists_Type (ty h) instance -- Eq Eq_Type ty => Eq (Exists_Type ty) where Exists_Type xh == Exists_Type yh = isJust $ xh `eq_type` yh instance -- Show String_from_Type ty => Show (Exists_Type ty) where show (Exists_Type ty) = string_from_type ty -- * Type 'Exists_Type1' -- | Existential data type wrapping the index of a type1. data Exists_Type1 ty = forall h. Exists_Type1 (ty h -> ty h) -- * Type 'Exists_Type_and_Repr' data Exists_Type_and_Repr ty repr = forall h. Exists_Type_and_Repr (ty h) (repr h) -- * Type family 'HBool' -- | Host-type equality. type family HEq x y :: Bool where HEq x x = 'True HEq x y = 'False -- ** Type 'HBool' -- | Boolean singleton. data HBool b where HTrue :: HBool 'True HFalse :: HBool 'False -- ** Class 'Implicit_HBool' -- | Construct a host-term boolean singleton from a host-type boolean. class Implicit_HBool b where hbool :: HBool b instance Implicit_HBool 'True where hbool = HTrue instance Implicit_HBool 'False where hbool = HFalse