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
24 = Type_Type2 (Proxy (->))
26 type instance Constraint2_of (Proxy (->))
28 instance Unlift_Type1 (Type_Type2 (Proxy (->))) where
29 unlift_type1 (Type_Type2 px a b) k =
30 k ( Type_Type1 (Proxy::Proxy ((->) a)) b
31 , Lift_Type1 (\(Type_Type1 _ b') -> Type_Type2 px a b')
33 instance Eq_Type root => Eq_Type1 (Type_Type2 (Proxy (->)) root) where
34 eq_type1 (Type_Type2 _ a1 _b1) (Type_Type2 _ a2 _b2)
35 | Just Refl <- eq_type a1 a2
37 eq_type1 _ _ = Nothing
39 Constraint_Type Monoid root =>
40 Constraint_Type Monoid (Type_Type2 (Proxy (->)) root) where
41 constraint_type c (Type_Type2 _ _arg res)
42 | Just Dict <- constraint_type c res
44 constraint_type _c _ = Nothing
45 instance (Constraint_Type1 Functor root)
46 => Constraint_Type1 Functor (Type_Type2 (Proxy (->)) root) where
47 constraint_type1 _c Type_Type2{} = Just Dict
48 instance (Constraint_Type1 Applicative root)
49 => Constraint_Type1 Applicative (Type_Type2 (Proxy (->)) root) where
50 constraint_type1 _c Type_Type2{} = Just Dict
51 instance Constraint_Type1 Foldable (Type_Fun root)
52 instance Constraint_Type1 Traversable (Type_Fun root)
53 instance Constraint_Type1 Monad (Type_Fun root) where
54 constraint_type1 _c Type_Type2{} = Just Dict
57 :: root arg -> root res
58 -> Type_Fun root ((->) arg res)
59 pattern Type_Fun arg res
60 = Type_Type2 Proxy arg res
64 Eq_Type (Type_Type2 (Proxy (->)) root) where
66 (Type_Type2 _ arg1 res1)
67 (Type_Type2 _ arg2 res2)
68 | Just Refl <- arg1 `eq_type` arg2
69 , Just Refl <- res1 `eq_type` res2
72 instance -- String_from_Type
73 String_from_Type root =>
74 String_from_Type (Type_Fun root) where
75 string_from_type (Type_Type2 _ arg res) =
76 "(" ++ string_from_type arg ++ " -> "
77 ++ string_from_type res ++ ")"
79 -- | Convenient alias to include a 'Type_Fun' within a type.
81 :: forall root h_arg h_res.
82 Lift_Type_Root Type_Fun root
83 => root h_arg -> root h_res
84 -> root ((->) h_arg h_res)
85 type_fun arg res = lift_type_root (Type_Fun arg res
86 ::Type_Fun root ((->) h_arg h_res))
88 -- | Parse 'Type_Fun'.
90 :: forall (root :: * -> *) ast ret.
91 ( Lift_Type_Root Type_Fun root
93 , Root_of_Type root ~ root
94 ) => Proxy (Type_Fun root)
96 -> (forall h. root h -> Either (Error_of_Type ast root) ret)
97 -> Either (Error_of_Type ast root) ret
98 type_fun_from _ty ast_arg ast_res k =
99 type_from (Proxy::Proxy root) ast_arg $ \(ty_arg::root h_arg) ->
100 type_from (Proxy::Proxy root) ast_res $ \(ty_res::root h_res) ->
101 k (ty_arg `type_fun` ty_res
102 :: root ((->) h_arg h_res))