1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE FlexibleInstances #-}
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
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
23 type Type_Fun = Type_Type2 (Proxy (->))
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')
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
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
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
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
56 :: root arg -> root res
57 -> Type_Fun root ((->) arg res)
58 pattern Type_Fun arg res
59 = Type_Type2 Proxy arg res
63 Eq_Type (Type_Fun root) where
65 (Type_Type2 _ arg1 res1)
66 (Type_Type2 _ arg2 res2)
67 | Just Refl <- arg1 `eq_type` arg2
68 , Just Refl <- res1 `eq_type` res2
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 ++ ")"
78 -- | Convenient alias to include a 'Type_Fun' within a type.
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))
87 -- | Parse 'Type_Fun'.
89 :: forall (root :: * -> *) ast ret.
90 ( Lift_Type_Root Type_Fun root
92 , Root_of_Type root ~ root
93 ) => Proxy (Type_Fun root)
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))