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