]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Type/Fun.hs
Map
[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 = Type_Type2 (Proxy (->))
24
25 instance Unlift_Type1 Type_Fun where
26 unlift_type1 (Type_Type2 px a b) k =
27 k ( Type_Type1 (Proxy::Proxy ((->) a)) b
28 , Lift_Type1 (\(Type_Type1 _ b') -> Type_Type2 px a b')
29 )
30 instance Eq_Type root => Eq_Type1 (Type_Fun root) where
31 eq_type1 (Type_Type2 _ a1 _b1) (Type_Type2 _ a2 _b2)
32 | Just Refl <- eq_type a1 a2
33 = Just Refl
34 eq_type1 _ _ = Nothing
35 instance Constraint_Type Eq (Type_Fun root) where
36 constraint_type _c _ = Nothing
37 instance Constraint_Type Ord (Type_Fun root) where
38 constraint_type _c _ = Nothing
39 instance
40 Constraint_Type Monoid root =>
41 Constraint_Type Monoid (Type_Fun root) where
42 constraint_type c (Type_Type2 _ _arg res)
43 | Just Dict <- constraint_type c res
44 = Just Dict
45 constraint_type _c _ = Nothing
46 instance Constraint_Type1 Functor (Type_Fun root) where
47 constraint_type1 _c Type_Type2{} = Just Dict
48 instance Constraint_Type1 Applicative (Type_Fun root) where
49 constraint_type1 _c Type_Type2{} = Just Dict
50 instance Constraint_Type1 Foldable (Type_Fun root)
51 instance Constraint_Type1 Traversable (Type_Fun root)
52 instance Constraint_Type1 Monad (Type_Fun root) where
53 constraint_type1 _c Type_Type2{} = Just Dict
54
55 pattern Type_Fun
56 :: root arg -> root res
57 -> Type_Fun root ((->) arg res)
58 pattern Type_Fun arg res
59 = Type_Type2 Proxy arg res
60
61 instance -- Eq_Type
62 Eq_Type root =>
63 Eq_Type (Type_Fun root) where
64 eq_type
65 (Type_Type2 _ arg1 res1)
66 (Type_Type2 _ arg2 res2)
67 | Just Refl <- arg1 `eq_type` arg2
68 , Just Refl <- res1 `eq_type` res2
69 = Just Refl
70 eq_type _ _ = Nothing
71 instance -- String_from_Type
72 String_from_Type root =>
73 String_from_Type (Type_Fun root) where
74 string_from_type (Type_Type2 _ arg res) =
75 "(" ++ string_from_type arg ++ " -> "
76 ++ string_from_type res ++ ")"
77
78 -- | Convenient alias to include a 'Type_Fun' within a type.
79 type_fun
80 :: forall root h_arg h_res.
81 Lift_Type_Root Type_Fun root
82 => root h_arg -> root h_res
83 -> root ((->) h_arg h_res)
84 type_fun arg res = lift_type_root (Type_Fun arg res
85 ::Type_Fun root ((->) h_arg h_res))
86
87 -- | Parse 'Type_Fun'.
88 type_fun_from
89 :: forall (root :: * -> *) ast ret.
90 ( Lift_Type_Root Type_Fun root
91 , Type_from ast root
92 , Root_of_Type root ~ root
93 ) => Proxy (Type_Fun 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 ((->) h_arg h_res))