]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Expr/Maybe.hs
IO, Monoid, Foldable, Text
[haskell/symantic.git] / Language / Symantic / Expr / Maybe.hs
1 {-# LANGUAGE DefaultSignatures #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE MultiParamTypeClasses #-}
5 {-# LANGUAGE ScopedTypeVariables #-}
6 {-# LANGUAGE TypeFamilies #-}
7 {-# LANGUAGE TypeOperators #-}
8 {-# OPTIONS_GHC -fno-warn-orphans #-}
9 -- | Expression for 'Maybe'.
10 module Language.Symantic.Expr.Maybe where
11
12 import Data.Proxy (Proxy(..))
13 import Data.Type.Equality ((:~:)(Refl))
14 import Prelude hiding (maybe)
15
16 import Language.Symantic.Type
17 import Language.Symantic.Trans.Common
18 import Language.Symantic.Expr.Root
19 import Language.Symantic.Expr.Error
20 import Language.Symantic.Expr.From
21 import Language.Symantic.Expr.Lambda
22
23 -- * Class 'Sym_Maybe_Lam'
24 -- | Symantic.
25 class Sym_Maybe repr where
26 nothing :: repr (Maybe a)
27 just :: repr a -> repr (Maybe a)
28
29 default nothing :: Trans t repr => t repr (Maybe a)
30 default just :: Trans t repr => t repr a -> t repr (Maybe a)
31 nothing = trans_lift nothing
32 just = trans_map1 just
33 -- | Symantic requiring a 'Lambda'.
34 class Sym_Maybe_Lam lam repr where
35 maybe
36 :: repr b
37 -> repr (Lambda lam a b)
38 -> repr (Maybe a)
39 -> repr b
40
41 default maybe
42 :: Trans t repr
43 => t repr b
44 -> t repr (Lambda lam a b)
45 -> t repr (Maybe a)
46 -> t repr b
47 maybe = trans_map3 maybe
48
49 -- * Type 'Expr_Maybe'
50 -- | Expression.
51 data Expr_Maybe (lam:: * -> *) (root:: *)
52 type instance Root_of_Expr (Expr_Maybe lam root) = root
53 type instance Type_of_Expr (Expr_Maybe lam root) = Type_Maybe
54 type instance Sym_of_Expr (Expr_Maybe lam root) repr = (Sym_Maybe repr, Sym_Maybe_Lam lam repr)
55 type instance Error_of_Expr ast (Expr_Maybe lam root) = No_Error_Expr
56
57 -- | Parsing utility to check that the given type is a 'Type_Maybe'
58 -- or raise 'Error_Expr_Type_mismatch'.
59 check_type_maybe
60 :: forall ast ex root ty h ret.
61 ( root ~ Root_of_Expr ex
62 , ty ~ Type_Root_of_Expr ex
63 , Lift_Type Type_Maybe (Type_of_Expr root)
64 , Unlift_Type Type_Maybe (Type_of_Expr root)
65 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
66 (Error_of_Expr ast root)
67 )
68 => Proxy ex -> ast -> ty h
69 -> (Type_Maybe ty h -> Either (Error_of_Expr ast root) ret)
70 -> Either (Error_of_Expr ast root) ret
71 check_type_maybe ex ast ty k =
72 case unlift_type $ unType_Root ty of
73 Just ty_l -> k ty_l
74 Nothing -> Left $
75 error_expr ex $
76 Error_Expr_Type_mismatch ast
77 (Exists_Type (type_maybe $ type_var0 SZero
78 :: ty (Maybe Var0)))
79 (Exists_Type ty)
80
81 -- | Parse 'maybe'.
82 maybe_from
83 :: forall root lam ty ast hs ret.
84 ( ty ~ Type_Root_of_Expr (Expr_Maybe lam root)
85 , Eq_Type ty
86 , Expr_from ast root
87 , Lift_Type (Type_Fun lam) (Type_of_Expr root)
88 , Unlift_Type (Type_Fun lam) (Type_of_Expr root)
89 , Lift_Type Type_Maybe (Type_of_Expr root)
90 , Unlift_Type Type_Maybe (Type_of_Expr root)
91 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
92 (Error_of_Expr ast root)
93 , Root_of_Expr root ~ root
94 ) => ast -> ast -> ast
95 -> Expr_From ast (Expr_Maybe lam root) hs ret
96 maybe_from ast_n ast_j ast_m ex ast ctx k =
97 expr_from (Proxy::Proxy root) ast_n ctx $
98 \(ty_n::ty h_n) (Forall_Repr_with_Context n) ->
99 expr_from (Proxy::Proxy root) ast_j ctx $
100 \(ty_j::ty h_j) (Forall_Repr_with_Context j) ->
101 expr_from (Proxy::Proxy root) ast_m ctx $
102 \(ty_m::ty h_m) (Forall_Repr_with_Context m) ->
103 check_type_fun ex ast ty_j $ \(Type_Type2 Proxy ty_j_a ty_j_b
104 :: Type_Fun lam ty h_j) ->
105 check_type_maybe ex ast ty_m $ \(Type_Type1 _ ty_m_a) ->
106 check_eq_type ex ast ty_n ty_j_b $ \Refl ->
107 check_eq_type ex ast ty_m_a ty_j_a $ \Refl ->
108 k ty_n $ Forall_Repr_with_Context $
109 \c -> maybe (n c) (j c) (m c)
110
111 -- | Parse 'nothing'.
112 nothing_from
113 :: forall root lam ty ast hs ret.
114 ( ty ~ Type_Root_of_Expr (Expr_Maybe lam root)
115 , Type_from ast ty
116 , Lift_Type Type_Maybe (Type_of_Expr root)
117 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
118 (Error_of_Expr ast root)
119 , Root_of_Expr root ~ root
120 ) => ast
121 -> Expr_From ast (Expr_Maybe lam root) hs ret
122 nothing_from ast_ty_a ex ast _ctx k =
123 case type_from (Proxy::Proxy ty)
124 ast_ty_a (Right . Exists_Type) of
125 Left err -> Left $ error_expr ex $ Error_Expr_Type err ast
126 Right (Exists_Type ty_a) ->
127 k (type_maybe ty_a) $ Forall_Repr_with_Context $
128 const nothing
129
130 -- | Parse 'just'.
131 just_from
132 :: forall root lam ty ast hs ret.
133 ( ty ~ Type_Root_of_Expr (Expr_Maybe lam root)
134 , Expr_from ast root
135 , Lift_Type Type_Maybe (Type_of_Expr root)
136 , Root_of_Expr root ~ root
137 ) => ast
138 -> Expr_From ast (Expr_Maybe lam root) hs ret
139 just_from ast_a _ex _ast ctx k =
140 expr_from (Proxy::Proxy root) ast_a ctx $
141 \(ty_a::ty h_a) (Forall_Repr_with_Context a) ->
142 k (type_maybe ty_a) $ Forall_Repr_with_Context $
143 \c -> just (a c)