]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Type/Fun.hs
init
[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-missing-methods #-}
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.Type2
19
20 -- * Type 'Type_Fun'
21 -- | The @->@ type.
22 type Type_Fun lam
23 = Type_Type2 (Proxy (Lambda lam))
24
25 type instance Constraint2_of (Proxy (Lambda lam))
26 = Constraint2_Empty
27
28 pattern Type_Fun
29 :: root arg -> root res
30 -> Type_Fun lam root ((Lambda lam) arg res)
31 pattern Type_Fun arg res
32 = Type_Type2 Proxy arg res
33
34 instance -- Eq_Type
35 Eq_Type root =>
36 Eq_Type (Type_Type2 (Proxy (Lambda lam)) root) where
37 eq_type
38 (Type_Type2 _ arg1 res1)
39 (Type_Type2 _ arg2 res2)
40 | Just Refl <- arg1 `eq_type` arg2
41 , Just Refl <- res1 `eq_type` res2
42 = Just Refl
43 eq_type _ _ = Nothing
44 instance -- String_from_Type
45 String_from_Type root =>
46 String_from_Type (Type_Fun lam root) where
47 string_from_type (Type_Type2 _ arg res) =
48 "(" ++ string_from_type arg ++ " -> "
49 ++ string_from_type res ++ ")"
50
51 -- ** Type 'Lambda'
52 -- | A newtype for the host-type function (->),
53 -- wrapping argument and result within a type constructor @lam@,
54 -- which is used in the 'Repr_Host' instance of 'Sym_Lambda'
55 -- to control the calling (see 'val' and 'lazy').
56 --
57 -- NOTE: a newtype is used instead of a type synonym
58 -- in order to be able to use it as a type constructor: @Lambda lam arg@,
59 -- which for instance has instances: 'Functor', 'Applicative', and 'Monad'.
60 newtype Lambda lam arg res
61 = Lambda { unLambda :: (->) (lam arg) (lam res) }
62
63 -- | Convenient alias to include a 'Type_Fun' within a type.
64 type_fun
65 :: forall lam root h_arg h_res.
66 Lift_Type_Root (Type_Fun lam) root
67 => root h_arg -> root h_res
68 -> root (Lambda lam h_arg h_res)
69 type_fun arg res = lift_type_root (Type_Fun arg res
70 ::Type_Fun lam root (Lambda lam h_arg h_res))
71
72 -- | Parse 'Type_Fun'.
73 type_fun_from
74 :: forall (lam :: * -> *) (root :: * -> *) ast ret.
75 ( Lift_Type_Root (Type_Fun lam) root
76 , Type_from ast root
77 , Root_of_Type root ~ root
78 ) => Proxy (Type_Fun lam root)
79 -> ast -> ast
80 -> (forall h. root h -> Either (Error_of_Type ast root) ret)
81 -> Either (Error_of_Type ast root) ret
82 type_fun_from _ty ast_arg ast_res k =
83 type_from (Proxy::Proxy root) ast_arg $ \(ty_arg::root h_arg) ->
84 type_from (Proxy::Proxy root) ast_res $ \(ty_res::root h_res) ->
85 k (ty_arg `type_fun` ty_res
86 :: root (Lambda lam h_arg h_res))