]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Expr/If.hs
init
[haskell/symantic.git] / Language / Symantic / Expr / If.hs
1 {-# LANGUAGE DefaultSignatures #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE ScopedTypeVariables #-}
4 {-# LANGUAGE TypeFamilies #-}
5 {-# LANGUAGE TypeOperators #-}
6 -- | Expression for @if@.
7 module Language.Symantic.Expr.If where
8
9 import Data.Proxy (Proxy(..))
10 import Data.Type.Equality ((:~:)(Refl))
11 import Language.Symantic.Expr.Common
12 import Language.Symantic.Repr.Dup
13 import Language.Symantic.Trans.Common
14 import Language.Symantic.Type
15
16 -- * Class 'Sym_If'
17 -- | Symantic.
18 class Sym_If repr where
19 if_ :: repr Bool -> repr a -> repr a -> repr a
20 default if_ :: Trans t repr => t repr Bool -> t repr a -> t repr a -> t repr a
21 if_ = trans_map3 if_
22 -- | Symantic.
23 class Sym_When repr where
24 when :: repr Bool -> repr () -> repr ()
25 default when :: Trans t repr => t repr Bool -> t repr () -> t repr ()
26 when = trans_map2 when
27
28 instance -- Sym_If Dup
29 ( Sym_If r1
30 , Sym_If r2
31 ) => Sym_If (Dup r1 r2) where
32 if_ (c1 `Dup` c2) (ok1 `Dup` ok2) (ko1 `Dup` ko2) =
33 if_ c1 ok1 ko1 `Dup` if_ c2 ok2 ko2
34 instance -- Sym_When Dup
35 ( Sym_When r1
36 , Sym_When r2
37 ) => Sym_When (Dup r1 r2) where
38 when (c1 `Dup` c2) (ok1 `Dup` ok2) =
39 when c1 ok1 `Dup` when c2 ok2
40
41 -- * Type 'Expr_If'
42 -- | Expression.
43 data Expr_If (root:: *)
44 type instance Root_of_Expr (Expr_If root) = root
45 type instance Type_of_Expr (Expr_If root) = No_Type
46 type instance Sym_of_Expr (Expr_If root) repr = (Sym_If repr)
47 type instance Error_of_Expr ast (Expr_If root) = No_Error_Expr
48
49 if_from
50 :: forall root ty ast hs ret.
51 ( ty ~ Type_Root_of_Expr (Expr_If root)
52 , Eq_Type ty
53 , Type_Root_Lift Type_Bool (Type_Root_of_Expr root)
54 , Expr_from ast root
55 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
56 (Error_of_Expr ast root)
57 , Root_of_Expr root ~ root
58 ) => ast -> ast -> ast
59 -> Expr_From ast (Expr_If root) hs ret
60 if_from ast_cond ast_ok ast_ko ex ast ctx k =
61 expr_from (Proxy::Proxy root) ast_cond ctx $
62 \(ty_cond::Type_Root_of_Expr root h_n) (Forall_Repr_with_Context cond) ->
63 expr_from (Proxy::Proxy root) ast_ok ctx $
64 \(ty_ok::Type_Root_of_Expr root h_ok) (Forall_Repr_with_Context ok) ->
65 expr_from (Proxy::Proxy root) ast_ko ctx $
66 \(ty_ko::Type_Root_of_Expr root h_ko) (Forall_Repr_with_Context ko) ->
67 check_eq_type ex ast type_bool ty_cond $ \Refl ->
68 check_eq_type ex ast ty_ok ty_ko $ \Refl ->
69 k ty_ok $ Forall_Repr_with_Context $
70 \c -> if_ (cond c) (ok c) (ko c)
71
72 -- * Type 'Expr_When'
73 -- | Expression.
74 data Expr_When (root:: *)
75 type instance Root_of_Expr (Expr_When root) = root
76 type instance Type_of_Expr (Expr_When root) = No_Type
77 type instance Sym_of_Expr (Expr_When root) repr = (Sym_When repr)
78 type instance Error_of_Expr ast (Expr_When root) = No_Error_Expr
79
80 when_from
81 :: forall root ty ast hs ret.
82 ( ty ~ Type_Root_of_Expr (Expr_When root)
83 , Eq_Type ty
84 , Type_Root_Lift Type_Bool (Type_Root_of_Expr root)
85 , Type_Root_Lift Type_Unit (Type_Root_of_Expr root)
86 , Expr_from ast root
87 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
88 (Error_of_Expr ast root)
89 , Root_of_Expr root ~ root
90 ) => ast -> ast
91 -> Expr_From ast (Expr_When root) hs ret
92 when_from ast_cond ast_ok ex ast ctx k =
93 expr_from (Proxy::Proxy root) ast_cond ctx $
94 \(ty_cond::Type_Root_of_Expr root h_n) (Forall_Repr_with_Context cond) ->
95 expr_from (Proxy::Proxy root) ast_ok ctx $
96 \(ty_ok::Type_Root_of_Expr root h_ok) (Forall_Repr_with_Context ok) ->
97 check_eq_type ex ast type_bool ty_cond $ \Refl ->
98 check_eq_type ex ast type_unit ty_ok $ \Refl ->
99 k ty_ok $ Forall_Repr_with_Context $
100 \c -> when (cond c) (ok c)