]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Expr/Functor.hs
MonoFunctor
[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 OverloadedStrings #-}
8 {-# LANGUAGE ScopedTypeVariables #-}
9 {-# LANGUAGE TypeFamilies #-}
10 {-# LANGUAGE TypeOperators #-}
11 {-# OPTIONS_GHC -fno-warn-orphans #-}
12 -- | Expression for 'Functor'.
13 module Language.Symantic.Expr.Functor where
14
15 import Control.Monad (liftM2)
16 import Data.Proxy (Proxy(..))
17 import Data.Type.Equality ((:~:)(Refl))
18 import Prelude hiding (fmap)
19 import qualified Data.Function as Fun
20 import qualified Data.Functor as Functor
21
22 import Language.Symantic.Type
23 import Language.Symantic.Repr
24 import Language.Symantic.Expr.Root
25 import Language.Symantic.Expr.Error
26 import Language.Symantic.Expr.From
27 import Language.Symantic.Expr.Lambda
28 import Language.Symantic.Trans.Common
29
30 -- * Class 'Sym_Functor'
31 -- | Symantic.
32 class Sym_Lambda repr => Sym_Functor repr where
33 fmap :: Functor f => repr (a -> b) -> repr (f a) -> repr (f b)
34 default fmap
35 :: (Trans t repr, Functor f)
36 => t repr (a -> b)
37 -> t repr (f a)
38 -> t repr (f b)
39 fmap = trans_map2 fmap
40
41 (<$) :: Functor f => repr a -> repr (f b) -> repr (f a)
42 (<$) a = fmap (lam (Fun.const a))
43 infixl 4 <$
44 instance Sym_Functor Repr_Host where
45 fmap = liftM2 Functor.fmap
46 instance Sym_Functor Repr_Text where
47 fmap = repr_text_app2 "fmap"
48 instance
49 ( Sym_Functor r1
50 , Sym_Functor r2
51 ) => Sym_Functor (Dup r1 r2) where
52 fmap (f1 `Dup` f2) (m1 `Dup` m2) =
53 fmap f1 m1 `Dup` fmap f2 m2
54
55 -- | 'fmap' alias.
56 (<$>) :: (Sym_Functor repr, Functor f)
57 => repr (a -> b) -> repr (f a) -> repr (f b)
58 (<$>) = fmap
59 infixl 4 <$>
60
61 -- * Type 'Expr_Functor'
62 -- | Expression.
63 data Expr_Functor (root:: *)
64 type instance Root_of_Expr (Expr_Functor root) = root
65 type instance Type_of_Expr (Expr_Functor root) = No_Type
66 type instance Sym_of_Expr (Expr_Functor root) repr = (Sym_Functor repr)
67 type instance Error_of_Expr ast (Expr_Functor root) = No_Error_Expr
68
69 -- | Parse 'fmap'.
70 fmap_from
71 :: forall root ty ast hs ret.
72 ( ty ~ Type_Root_of_Expr (Expr_Functor root)
73 , Eq_Type ty
74 , Expr_from ast root
75 , Lift_Type Type_Fun (Type_of_Expr root)
76 , Unlift_Type Type_Fun (Type_of_Expr root)
77 , Unlift_Type1 (Type_of_Expr root)
78 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
79 (Error_of_Expr ast root)
80 , Root_of_Expr root ~ root
81 , Constraint_Type1 Functor ty
82 ) => ast -> ast
83 -> Expr_From ast (Expr_Functor root) hs ret
84 fmap_from ast_g ast_fa ex ast ctx k =
85 -- NOTE: fmap :: Functor f => (a -> b) -> f a -> f b
86 expr_from (Proxy::Proxy root) ast_g ctx $
87 \(ty_g::ty h_g) (Forall_Repr_with_Context g) ->
88 expr_from (Proxy::Proxy root) ast_fa ctx $
89 \(ty_fa::ty h_fa) (Forall_Repr_with_Context fa) ->
90 check_type_fun ex ast ty_g $ \(Type_Type2 Proxy ty_g_a ty_g_b
91 :: Type_Fun ty h_g) ->
92 check_type1 ex ast ty_fa $ \(Type_Type1 f ty_fa_a, Lift_Type1 f_lift) ->
93 check_constraint_type1 ex (Proxy::Proxy Functor) ast ty_fa $ \Dict ->
94 check_eq_type ex ast ty_g_a ty_fa_a $ \Refl ->
95 k (Type_Root $ f_lift $ Type_Type1 f ty_g_b) $ Forall_Repr_with_Context $
96 \c -> fmap (g c) (fa c)