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-missing-methods #-}
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.Type2
23 = Type_Type2 (Proxy (Lambda lam))
25 type instance Constraint2_of (Proxy (Lambda lam))
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
36 Eq_Type (Type_Type2 (Proxy (Lambda lam)) root) where
38 (Type_Type2 _ arg1 res1)
39 (Type_Type2 _ arg2 res2)
40 | Just Refl <- arg1 `eq_type` arg2
41 , Just Refl <- res1 `eq_type` res2
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 ++ ")"
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').
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) }
63 -- | Convenient alias to include a 'Type_Fun' within a type.
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))
72 -- | Parse 'Type_Fun'.
74 :: forall (lam :: * -> *) (root :: * -> *) ast ret.
75 ( Lift_Type_Root (Type_Fun lam) root
77 , Root_of_Type root ~ root
78 ) => Proxy (Type_Fun lam root)
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))