1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE DefaultSignatures #-}
4 {-# LANGUAGE FlexibleContexts #-}
5 {-# LANGUAGE FlexibleInstances #-}
6 {-# LANGUAGE MultiParamTypeClasses #-}
7 {-# LANGUAGE ScopedTypeVariables #-}
8 {-# LANGUAGE TypeFamilies #-}
9 {-# LANGUAGE TypeOperators #-}
10 {-# LANGUAGE UndecidableInstances #-}
11 {-# OPTIONS_GHC -fno-warn-orphans #-}
12 -- | Expression for 'Functor'.
13 module Language.Symantic.Expr.Functor where
15 import Data.Proxy (Proxy(..))
16 import Data.Type.Equality ((:~:)(Refl))
17 import Prelude hiding (fmap)
18 import qualified Data.Function as Fun
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
27 -- * Class 'Sym_Functor'
29 class Sym_Lambda repr => Sym_Functor repr where
30 fmap :: Functor f => repr ((->) a b) -> repr (f a) -> repr (f b)
32 :: (Trans t repr, Functor f)
36 fmap = trans_map2 fmap
38 (<$) :: Functor f => repr a -> repr (f b) -> repr (f a)
39 (<$) a = fmap (lam (Fun.const a))
43 (<$>) :: (Sym_Functor repr, Functor f)
44 => repr ((->) a b) -> repr (f a) -> repr (f b)
48 -- * Type 'Expr_Functor'
50 data Expr_Functor (root:: *)
51 type instance Root_of_Expr (Expr_Functor root) = root
52 type instance Type_of_Expr (Expr_Functor root) = No_Type
53 type instance Sym_of_Expr (Expr_Functor root) repr = (Sym_Functor repr)
54 type instance Error_of_Expr ast (Expr_Functor root) = No_Error_Expr
55 instance Constraint_Type1 Functor (Type_Type0 px root)
56 instance Constraint_Type1 Functor (Type_Var1 root)
60 :: forall root ty ast hs ret.
61 ( ty ~ Type_Root_of_Expr (Expr_Functor root)
64 , Lift_Type Type_Fun (Type_of_Expr root)
65 , Unlift_Type Type_Fun (Type_of_Expr root)
66 , Unlift_Type1 (Type_of_Expr 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 Functor ty
72 -> Expr_From ast (Expr_Functor root) hs ret
73 fmap_from ast_g ast_fa ex ast ctx k =
74 -- NOTE: fmap :: Functor f => (a -> b) -> f a -> f b
75 expr_from (Proxy::Proxy root) ast_g ctx $
76 \(ty_g::ty h_g) (Forall_Repr_with_Context g) ->
77 expr_from (Proxy::Proxy root) ast_fa ctx $
78 \(ty_fa::ty h_fa) (Forall_Repr_with_Context fa) ->
79 check_type_fun ex ast ty_g $ \(Type_Type2 Proxy ty_g_a ty_g_b
80 :: Type_Fun ty h_g) ->
81 check_type1 ex ast ty_fa $ \(Type_Type1 f ty_fa_a, Lift_Type1 f_lift) ->
82 check_constraint_type1 ex (Proxy::Proxy Functor) ast ty_fa $ \Dict ->
83 check_eq_type ex ast ty_g_a ty_fa_a $ \Refl ->
84 k (Type_Root $ f_lift $ Type_Type1 f ty_g_b) $ Forall_Repr_with_Context $
85 \c -> fmap (g c) (fa c)