]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Type/Fun.hs
Monad
[haskell/symantic.git] / Language / Symantic / Type / Fun.hs
1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE GADTs #-}
4 {-# LANGUAGE MultiParamTypeClasses #-}
5 {-# LANGUAGE OverloadedStrings #-}
6 {-# LANGUAGE PatternSynonyms #-}
7 {-# LANGUAGE Rank2Types #-}
8 {-# LANGUAGE ScopedTypeVariables #-}
9 {-# LANGUAGE TypeFamilies #-}
10 {-# OPTIONS_GHC -fno-warn-orphans #-}
11 module Language.Symantic.Type.Fun where
12
13 import Data.Proxy
14 import Data.Type.Equality ((:~:)(Refl))
15 import Language.Symantic.Type.Root
16 import Language.Symantic.Type.Error
17 import Language.Symantic.Type.Type0
18 import Language.Symantic.Type.Type1
19 import Language.Symantic.Type.Type2
20
21 -- * Type 'Type_Fun'
22 -- | The @->@ type.
23 type Type_Fun lam
24 = Type_Type2 (Proxy (Lambda lam))
25
26 type instance Constraint2_of (Proxy (Lambda lam))
27 = Constraint2_Empty
28 instance Unlift_Type1 (Type_Type2 (Proxy (Lambda lam))) where
29 unlift_type1 (Type_Type2 px a b) k =
30 k ( Type_Type1 (Proxy::Proxy (Lambda lam a)) b
31 , Lift_Type1 (\(Type_Type1 _ b') -> Type_Type2 px a b')
32 )
33 instance Eq_Type root => Eq_Type1 (Type_Type2 (Proxy (Lambda lam)) root) where
34 eq_type1 (Type_Type2 _ a1 _b1) (Type_Type2 _ a2 _b2)
35 | Just Refl <- eq_type a1 a2
36 = Just Refl
37 eq_type1 _ _ = Nothing
38 instance Constraint_Type1 Functor (Type_Fun lam root)
39 instance Constraint_Type1 Applicative (Type_Fun lam root)
40 instance Constraint_Type1 Traversable (Type_Fun lam root)
41 instance Constraint_Type1 Monad (Type_Fun lam root)
42
43 pattern Type_Fun
44 :: root arg -> root res
45 -> Type_Fun lam root ((Lambda lam) arg res)
46 pattern Type_Fun arg res
47 = Type_Type2 Proxy arg res
48
49 instance -- Eq_Type
50 Eq_Type root =>
51 Eq_Type (Type_Type2 (Proxy (Lambda lam)) root) where
52 eq_type
53 (Type_Type2 _ arg1 res1)
54 (Type_Type2 _ arg2 res2)
55 | Just Refl <- arg1 `eq_type` arg2
56 , Just Refl <- res1 `eq_type` res2
57 = Just Refl
58 eq_type _ _ = Nothing
59 instance -- String_from_Type
60 String_from_Type root =>
61 String_from_Type (Type_Fun lam root) where
62 string_from_type (Type_Type2 _ arg res) =
63 "(" ++ string_from_type arg ++ " -> "
64 ++ string_from_type res ++ ")"
65
66 -- ** Type 'Lambda'
67 -- | A newtype for the host-type function (->),
68 -- wrapping argument and result within a type constructor @lam@,
69 -- which is used in the 'Repr_Host' instance of 'Sym_Lambda'
70 -- to control the calling (see 'val' and 'lazy').
71 --
72 -- NOTE: a newtype is used instead of a type synonym
73 -- in order to be able to use it as a type constructor: @Lambda lam arg@,
74 -- which for instance has instances: 'Functor', 'Applicative', and 'Monad'.
75 newtype Lambda lam arg res
76 = Lambda { unLambda :: (->) (lam arg) (lam res) }
77
78 -- | Convenient alias to include a 'Type_Fun' within a type.
79 type_fun
80 :: forall lam root h_arg h_res.
81 Lift_Type_Root (Type_Fun lam) root
82 => root h_arg -> root h_res
83 -> root (Lambda lam h_arg h_res)
84 type_fun arg res = lift_type_root (Type_Fun arg res
85 ::Type_Fun lam root (Lambda lam h_arg h_res))
86
87 -- | Parse 'Type_Fun'.
88 type_fun_from
89 :: forall (lam :: * -> *) (root :: * -> *) ast ret.
90 ( Lift_Type_Root (Type_Fun lam) root
91 , Type_from ast root
92 , Root_of_Type root ~ root
93 ) => Proxy (Type_Fun lam root)
94 -> ast -> ast
95 -> (forall h. root h -> Either (Error_of_Type ast root) ret)
96 -> Either (Error_of_Type ast root) ret
97 type_fun_from _ty ast_arg ast_res k =
98 type_from (Proxy::Proxy root) ast_arg $ \(ty_arg::root h_arg) ->
99 type_from (Proxy::Proxy root) ast_res $ \(ty_res::root h_res) ->
100 k (ty_arg `type_fun` ty_res
101 :: root (Lambda lam h_arg h_res))