1 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE MultiParamTypeClasses #-}
4 {-# LANGUAGE OverloadedStrings #-}
5 {-# LANGUAGE ScopedTypeVariables #-}
6 {-# LANGUAGE TypeFamilies #-}
7 {-# LANGUAGE UndecidableInstances #-}
8 module Language.LOL.Symantic.Type.Maybe where
10 import Data.Maybe (isJust)
11 import Data.Type.Equality ((:~:)(Refl))
14 import Language.LOL.Symantic.AST
15 import Language.LOL.Symantic.Type.Common
16 import Language.LOL.Symantic.Type.Fun
17 import Language.LOL.Symantic.Type.Bool
19 -- * Type 'Type_Maybe'
20 -- | The 'Maybe' type.
21 data Type_Maybe root h where
22 Type_Maybe :: root h_a
23 -> Type_Maybe root (Maybe h_a)
25 type instance Root_of_Type (Type_Maybe root) = root
26 type instance Error_of_Type ast (Type_Maybe root) = ()
30 Type_Eq (Type_Maybe root) where
31 type_eq (Type_Maybe a1) (Type_Maybe a2)
32 | Just Refl <- a1 `type_eq` a2
37 Eq (Type_Maybe root h) where
38 x == y = isJust $ type_eq x y
39 instance -- Type_from AST
42 , Type_Root_Lift Type_Maybe root
43 , Error_Type_Lift (Error_Type AST) (Error_of_Type AST root)
44 , Error_Type_Unlift (Error_Type AST) (Error_of_Type AST root)
45 , Error_Type_Lift (Error_Type_Fun AST) (Error_of_Type AST root)
46 , Root_of_Type root ~ root
47 , Implicit_HBool (Is_Last_Type (Type_Maybe root) root)
48 ) => Type_from AST (Type_Maybe root) where
49 type_from _px_ty ast k =
54 type_from (Proxy::Proxy root) ast_a $ \(ty_a::root h_a) ->
55 k (type_root_lift $ Type_Maybe ty_a
57 _ -> Left $ error_type_lift $
58 Error_Type_Fun_Wrong_number_of_arguments 1 ast
60 case hbool :: HBool (Is_Last_Type (Type_Maybe root) root) of
61 HTrue -> error_type_lift $ Error_Type_Unsupported ast
62 HFalse -> error_type_lift $ Error_Type_Unsupported_here ast
63 instance -- String_from_Type
64 String_from_Type root =>
65 String_from_Type (Type_Maybe root) where
66 string_from_type (Type_Maybe a) =
67 "Maybe (" ++ string_from_type a ++ ")"
69 String_from_Type root =>
70 Show (Type_Maybe root h) where
71 show = string_from_type
73 -- | Convenient alias to include a 'Type_Maybe' within a type.
75 :: Type_Root_Lift Type_Maybe root
78 type_maybe a = type_root_lift (Type_Maybe a)
80 -- * Type 'Type_Fun_Bool_Maybe'
81 -- | Convenient alias.
82 type Type_Fun_Bool_Maybe lam
83 = Type_Root (Type_Alt (Type_Fun lam)