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
15 import Language.Symantic.Type.Common
20 = Type_Type2 Constraint2_Empty (Lambda lam)
21 pattern Type_Fun arg res
22 = Type_Type2 Proxy Proxy arg res
24 instance -- String_from_Type
25 String_from_Type root =>
26 String_from_Type (Type_Fun lam root) where
27 string_from_type (Type_Type2 _ _ arg res) =
28 "(" ++ string_from_type arg ++ " -> "
29 ++ string_from_type res ++ ")"
32 -- | A newtype for the host-type function (->),
33 -- wrapping argument and result within a type constructor @lam@,
34 -- which is used in the 'Repr_Host' instance of 'Sym_Lambda'
35 -- to implement 'val' and 'lazy'.
37 -- NOTE: a newtype is used instead of a type synonym
38 -- in order to be able to use it as a type constructor: @Lambda lam arg@,
39 -- which for instance has instances: 'Functor', 'Applicative', and 'Monad'.
40 newtype Lambda lam arg res
41 = Lambda { unLambda :: (->) (lam arg) (lam res) }
43 -- | Convenient alias to include a 'Type_Fun' within a type.
45 :: forall lam root h_arg h_res.
46 Lift_Type_Root (Type_Fun lam) root
47 => root h_arg -> root h_res
48 -> root (Lambda lam h_arg h_res)
49 type_fun arg res = lift_type_root (Type_Fun arg res
50 ::Type_Fun lam root (Lambda lam h_arg h_res))
52 -- | Parse 'Type_Fun'.
54 :: forall (lam :: * -> *) (root :: * -> *) ast ret.
55 ( Lift_Type_Root (Type_Fun lam) root
57 , Root_of_Type root ~ root
58 ) => Proxy (Type_Fun lam root)
60 -> (forall h. root h -> Either (Error_of_Type ast root) ret)
61 -> Either (Error_of_Type ast root) ret
62 type_fun_from _ty ast_arg ast_res k =
63 type_from (Proxy::Proxy root) ast_arg $ \(ty_arg::root h_arg) ->
64 type_from (Proxy::Proxy root) ast_res $ \(ty_res::root h_res) ->
65 k (ty_arg `type_fun` ty_res
66 :: root (Lambda lam h_arg h_res))