]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Expr/Applicative.hs
Map
[haskell/symantic.git] / Language / Symantic / Expr / Applicative.hs
1 {-# LANGUAGE DefaultSignatures #-}
2 {-# LANGUAGE GADTs #-}
3 {-# LANGUAGE FlexibleContexts #-}
4 {-# LANGUAGE FlexibleInstances #-}
5 {-# LANGUAGE MultiParamTypeClasses #-}
6 {-# LANGUAGE Rank2Types #-}
7 {-# LANGUAGE ScopedTypeVariables #-}
8 {-# LANGUAGE TypeFamilies #-}
9 {-# LANGUAGE TypeOperators #-}
10 {-# OPTIONS_GHC -fno-warn-orphans #-}
11 -- | Expression for 'Applicative'.
12 module Language.Symantic.Expr.Applicative where
13
14 import Control.Applicative (Applicative)
15 import Data.Proxy (Proxy(..))
16 import Data.Type.Equality ((:~:)(Refl))
17 import Prelude hiding (Functor(..), (<$>), Applicative(..), id, const)
18 import qualified Data.Function as Fun
19
20 import Language.Symantic.Type
21 import Language.Symantic.Trans.Common
22 import Language.Symantic.Expr.Root
23 import Language.Symantic.Expr.Error
24 import Language.Symantic.Expr.From
25 import Language.Symantic.Expr.Lambda
26 import Language.Symantic.Expr.Functor
27
28 -- * Class 'Sym_Applicative'
29 -- | Symantic.
30 class Sym_Functor repr => Sym_Applicative repr where
31 pure :: Applicative f => repr a -> repr (f a)
32 (<*>) :: Applicative f => repr (f ((->) a b)) -> repr (f a) -> repr (f b)
33 default pure :: (Trans t repr, Applicative f) => t repr a -> t repr (f a)
34 default (<*>) :: (Trans t repr, Applicative f)
35 => t repr (f ((->) a b)) -> t repr (f a) -> t repr (f b)
36 pure = trans_map1 pure
37 (<*>) = trans_map2 (<*>)
38 (*>) :: Applicative f => repr (f a) -> repr (f b) -> repr (f b)
39 (<*) :: Applicative f => repr (f a) -> repr (f b) -> repr (f a)
40 x *> y = (lam Fun.id <$ x) <*> y
41 x <* y = (lam (lam . Fun.const) <$> x) <*> y
42 infixl 4 *>
43 infixl 4 <*
44 infixl 4 <*>
45
46 -- * Type 'Expr_Applicative'
47 -- | Expression.
48 data Expr_Applicative (root:: *)
49 type instance Root_of_Expr (Expr_Applicative root) = root
50 type instance Type_of_Expr (Expr_Applicative root) = No_Type
51 type instance Sym_of_Expr (Expr_Applicative root) repr = (Sym_Applicative repr)
52 type instance Error_of_Expr ast (Expr_Applicative root) = No_Error_Expr
53 instance Constraint_Type1 Applicative (Type_Var1 root)
54
55 -- | Parse 'pure'.
56 pure_from
57 :: forall root ty ast hs ret.
58 ( ty ~ Type_Root_of_Expr (Expr_Applicative root)
59 , Eq_Type ty
60 , Type1_from ast ty
61 , Expr_from ast root
62 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
63 (Error_of_Expr ast root)
64 , Root_of_Expr root ~ root
65 , Constraint_Type1 Applicative ty
66 ) => ast -> ast
67 -> Expr_From ast (Expr_Applicative root) hs ret
68 pure_from ast_f ast_a ex ast ctx k =
69 -- pure :: Applicative f => a -> f a
70 either (\err -> Left $ error_expr ex $ Error_Expr_Type err ast) Fun.id $
71 type1_from (Proxy::Proxy ty) ast_f $ \_f ty_f -> Right $
72 expr_from (Proxy::Proxy root) ast_a ctx $
73 \(ty_a::ty h_a) (Forall_Repr_with_Context a) ->
74 let ty_fa = ty_f ty_a in
75 check_constraint_type1 ex (Proxy::Proxy Applicative) ast ty_fa $ \Dict ->
76 k ty_fa $ Forall_Repr_with_Context $
77 \c -> pure (a c)
78
79 -- | Parse '<*>'.
80 ltstargt_from
81 :: forall root ty ast hs ret.
82 ( ty ~ Type_Root_of_Expr (Expr_Applicative root)
83 , Eq_Type ty
84 , Eq_Type1 ty
85 , Expr_from ast root
86 , Lift_Type Type_Fun (Type_of_Expr root)
87 , Unlift_Type Type_Fun (Type_of_Expr root)
88 , Unlift_Type1 (Type_of_Expr root)
89 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
90 (Error_of_Expr ast root)
91 , Root_of_Expr root ~ root
92 , Constraint_Type1 Applicative ty
93 ) => ast -> ast
94 -> Expr_From ast (Expr_Applicative root) hs ret
95 ltstargt_from ast_fg ast_fa ex ast ctx k =
96 -- (<*>) :: Applicative f => f (a -> b) -> f a -> f b
97 expr_from (Proxy::Proxy root) ast_fg ctx $
98 \(ty_fg::ty h_fg) (Forall_Repr_with_Context fg) ->
99 expr_from (Proxy::Proxy root) ast_fa ctx $
100 \(ty_fa::ty h_fa) (Forall_Repr_with_Context fa) ->
101 check_type1 ex ast ty_fg $ \(Type_Type1 _f (ty_g::ty h_g), _) ->
102 check_type1 ex ast ty_fa $ \(Type_Type1 f ty_fa_a, Lift_Type1 ty_f) ->
103 check_eq_type1 ex ast ty_fg ty_fa $ \Refl ->
104 check_type_fun ex ast ty_g $ \(Type_Type2 Proxy ty_g_a ty_g_b
105 :: Type_Fun ty h_g) ->
106 check_constraint_type1 ex (Proxy::Proxy Applicative) ast ty_fa $ \Dict ->
107 check_eq_type ex ast ty_g_a ty_fa_a $ \Refl ->
108 k (Type_Root $ ty_f $ Type_Type1 f ty_g_b) $ Forall_Repr_with_Context $
109 \c -> (<*>) (fg c) (fa c)