1 {-# LANGUAGE DataKinds #-}
2 {-# LANGUAGE ExistentialQuantification #-}
3 {-# LANGUAGE FlexibleInstances #-}
5 {-# LANGUAGE KindSignatures #-}
6 {-# LANGUAGE MultiParamTypeClasses #-}
7 {-# LANGUAGE Rank2Types #-}
8 {-# LANGUAGE TypeFamilies #-}
9 {-# LANGUAGE TypeOperators #-}
10 module TFHOE.Type.Common where
12 import Data.Maybe (isJust)
13 import Data.Type.Equality ((:~:)(Refl))
17 -- | Test two types for equality,
18 -- returning an Haskell type-level proof
19 -- of the equality when it holds.
20 class Type_Eq ty where
21 type_eq :: forall h1 h2. ty h1 -> ty h2 -> Maybe (h1 :~: h2)
23 -- * Class 'Type_from'
25 class Type_Eq ty => Type_from raw ty where
28 -> (forall h. ty h -> Either Error_Type ret)
29 -> Either Error_Type ret
31 type Error_Type = String
33 -- * Class 'String_from_Type'
35 class String_from_Type ty where
36 string_from_type :: ty h -> String
40 -- | Join a name and a type;
41 -- used to handle lambda variables by name
42 -- (and not DeBruijn indices).
48 -- | GADT for a typing context,
49 -- accumulating an @item@ at each lambda;
50 -- used to accumulate object-types of lambda variables in 'Expr_from'
51 -- or meta-terms of lambda variables in 'Repr_Host'.
52 data Context :: (* -> *) -> [*] -> * where
53 Context_Empty :: Context item '[]
54 Context_Next :: item h -> Context item hs -> Context item (h ': hs)
55 infixr 5 `Context_Next`
59 -- | Close an extensible type.
61 Type_End :: Type_End ()
63 instance Type_Eq Type_End where
64 type_eq Type_End Type_End = Just Refl
65 instance Eq (Type_End h) where
67 instance String_from_Type Type_End where
68 string_from_type _ = ""
69 instance Type_from raw Type_End where
70 type_from _raw k = k Type_End
72 -- * Type 'Exists_Type'
74 -- | Existential data type to wrap an indexed type.
76 = forall h. Exists_Type (ty h)
79 Eq (Exists_Type ty) where
80 Exists_Type xh == Exists_Type yh =
81 isJust $ type_eq xh yh
83 String_from_Type ty =>
84 Show (Exists_Type ty) where
85 show (Exists_Type ty) = string_from_type ty
87 -- * Type 'Exists_Type_and_Repr'
89 data Exists_Type_and_Repr ty repr
91 Exists_Type_and_Repr (ty h) (repr h)