]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Expr/Functor.hs
Integer, Integral, Num
[haskell/symantic.git] / Language / Symantic / Expr / Functor.hs
1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE DefaultSignatures #-}
3 {-# LANGUAGE GADTs #-}
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
14
15 import Data.Proxy (Proxy(..))
16 import Data.Type.Equality ((:~:)(Refl))
17 import Prelude hiding (fmap)
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
27 -- * Class 'Sym_Functor'
28 -- | Symantic.
29 class Sym_Lambda repr => Sym_Functor repr where
30 fmap :: Functor f => repr ((->) a b) -> repr (f a) -> repr (f b)
31 default fmap
32 :: (Trans t repr, Functor f)
33 => t repr ((->) a b)
34 -> t repr (f a)
35 -> t repr (f b)
36 fmap = trans_map2 fmap
37
38 (<$) :: Functor f => repr a -> repr (f b) -> repr (f a)
39 (<$) a = fmap (lam (Fun.const a))
40 infixl 4 <$
41
42 -- | 'fmap' alias.
43 (<$>) :: (Sym_Functor repr, Functor f)
44 => repr ((->) a b) -> repr (f a) -> repr (f b)
45 (<$>) = fmap
46 infixl 4 <$>
47
48 -- * Type 'Expr_Functor'
49 -- | Expression.
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
56 -- | Parse 'fmap'.
57 fmap_from
58 :: forall root ty ast hs ret.
59 ( ty ~ Type_Root_of_Expr (Expr_Functor root)
60 , Eq_Type ty
61 , Expr_from ast root
62 , Lift_Type Type_Fun (Type_of_Expr root)
63 , Unlift_Type Type_Fun (Type_of_Expr root)
64 , Unlift_Type1 (Type_of_Expr root)
65 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
66 (Error_of_Expr ast root)
67 , Root_of_Expr root ~ root
68 , Constraint_Type1 Functor ty
69 ) => ast -> ast
70 -> Expr_From ast (Expr_Functor root) hs ret
71 fmap_from ast_g ast_fa ex ast ctx k =
72 -- NOTE: fmap :: Functor f => (a -> b) -> f a -> f b
73 expr_from (Proxy::Proxy root) ast_g ctx $
74 \(ty_g::ty h_g) (Forall_Repr_with_Context g) ->
75 expr_from (Proxy::Proxy root) ast_fa ctx $
76 \(ty_fa::ty h_fa) (Forall_Repr_with_Context fa) ->
77 check_type_fun ex ast ty_g $ \(Type_Type2 Proxy ty_g_a ty_g_b
78 :: Type_Fun ty h_g) ->
79 check_type1 ex ast ty_fa $ \(Type_Type1 f ty_fa_a, Lift_Type1 f_lift) ->
80 check_constraint_type1 ex (Proxy::Proxy Functor) ast ty_fa $ \Dict ->
81 check_eq_type ex ast ty_g_a ty_fa_a $ \Refl ->
82 k (Type_Root $ f_lift $ Type_Type1 f ty_g_b) $ Forall_Repr_with_Context $
83 \c -> fmap (g c) (fa c)