]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Type/Fun.hs
fix (->) by removing inline/val/lazy
[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
24 = Type_Type2 (Proxy (->))
25
26 type instance Constraint2_of (Proxy (->))
27 = Constraint2_Empty
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')
32 )
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
36 = Just Refl
37 eq_type1 _ _ = Nothing
38 instance
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
43 = Just Dict
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
55
56 pattern Type_Fun
57 :: root arg -> root res
58 -> Type_Fun root ((->) arg res)
59 pattern Type_Fun arg res
60 = Type_Type2 Proxy arg res
61
62 instance -- Eq_Type
63 Eq_Type root =>
64 Eq_Type (Type_Type2 (Proxy (->)) root) where
65 eq_type
66 (Type_Type2 _ arg1 res1)
67 (Type_Type2 _ arg2 res2)
68 | Just Refl <- arg1 `eq_type` arg2
69 , Just Refl <- res1 `eq_type` res2
70 = Just Refl
71 eq_type _ _ = Nothing
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 ++ ")"
78
79 -- | Convenient alias to include a 'Type_Fun' within a type.
80 type_fun
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))
87
88 -- | Parse 'Type_Fun'.
89 type_fun_from
90 :: forall (root :: * -> *) ast ret.
91 ( Lift_Type_Root Type_Fun root
92 , Type_from ast root
93 , Root_of_Type root ~ root
94 ) => Proxy (Type_Fun root)
95 -> ast -> ast
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))