]> Git — Sourcephile - haskell/symantic.git/blob - Language/LOL/Symantic/Type/Maybe.hs
init
[haskell/symantic.git] / Language / LOL / Symantic / Type / Maybe.hs
1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE GADTs #-}
3 {-# LANGUAGE MultiParamTypeClasses #-}
4 {-# LANGUAGE OverloadedStrings #-}
5 {-# LANGUAGE ScopedTypeVariables #-}
6 {-# LANGUAGE TypeFamilies #-}
7 {-# LANGUAGE UndecidableInstances #-}
8 module Language.LOL.Symantic.Type.Maybe where
9
10 import Data.Maybe (isJust)
11 import Data.Type.Equality ((:~:)(Refl))
12 import Data.Proxy
13
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
18
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)
24
25 type instance Root_of_Type (Type_Maybe root) = root
26 type instance Error_of_Type ast (Type_Maybe root) = ()
27
28 instance -- Type_Eq
29 Type_Eq root =>
30 Type_Eq (Type_Maybe root) where
31 type_eq (Type_Maybe a1) (Type_Maybe a2)
32 | Just Refl <- a1 `type_eq` a2
33 = Just Refl
34 type_eq _ _ = Nothing
35 instance -- Eq
36 Type_Eq root =>
37 Eq (Type_Maybe root h) where
38 x == y = isJust $ type_eq x y
39 instance -- Type_from AST
40 ( Type_Eq root
41 , Type_from AST root
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 =
50 case ast of
51 AST "Maybe" asts ->
52 case asts of
53 [ast_a] ->
54 type_from (Proxy::Proxy root) ast_a $ \(ty_a::root h_a) ->
55 k (type_root_lift $ Type_Maybe ty_a
56 :: root (Maybe h_a))
57 _ -> Left $ error_type_lift $
58 Error_Type_Fun_Wrong_number_of_arguments 1 ast
59 _ -> Left $
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 ++ ")"
68 instance -- Show
69 String_from_Type root =>
70 Show (Type_Maybe root h) where
71 show = string_from_type
72
73 -- | Convenient alias to include a 'Type_Maybe' within a type.
74 type_maybe
75 :: Type_Root_Lift Type_Maybe root
76 => root h_a
77 -> root (Maybe h_a)
78 type_maybe a = type_root_lift (Type_Maybe a)
79
80 -- * Type 'Type_Fun_Bool_Maybe'
81 -- | Convenient alias.
82 type Type_Fun_Bool_Maybe lam
83 = Type_Root (Type_Alt (Type_Fun lam)
84 (Type_Alt Type_Bool
85 Type_Maybe))