{-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE Rank2Types #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE UndecidableInstances #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Language.Symantic.Type.Type0 where import Data.Maybe (isJust) import Data.Proxy import Data.Type.Equality ((:~:)(Refl)) import GHC.Prim (Constraint) import Language.Symantic.Lib.Data.Peano import Language.Symantic.Type.Root import Language.Symantic.Type.Alt import Language.Symantic.Type.Error -- ** 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 'No_Type' -- | A discarded type. data No_Type (root:: * -> *) h = No_Type (root h) deriving (Eq, Show) -- * 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) instance -- Type_Root 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 Type_Root Eq_Type (Type_Root ty) => Eq (Type_Root ty h) where x == y = isJust $ x `eq_type` y instance -- Type_Alt ( 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 Type_Alt ( Eq_Type (curr root) , Eq_Type (next root) ) => Eq (Type_Alt curr next root h) where x == y = isJust $ x `eq_type` y -- * 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 instance -- 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 -- 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 -- * 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 instance -- Type_Root ( 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 -- Type_Alt ( 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 -- * Class 'String_from_Type' -- | Return a 'String' from a type. class String_from_Type ty where string_from_type :: ty h -> String instance -- Type_Root 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 Type_Root String_from_Type (Type_Root ty) => Show (Type_Root ty h) where show = string_from_type instance -- Type_Alt ( 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 (Host0_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 -- | 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 'Host0_of' type family Host0_of px :: * type instance Host0_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 instance Lift_Type ty root => Lift_Type_Root ty (Type_Root root) where lift_type_root = Type_Root . lift_type -- *** 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 '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_Type_and_Repr' data Exists_Type_and_Repr ty repr = forall h. Exists_Type_and_Repr (ty h) (repr h)