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