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.Common
20 = Type_Type2 (Proxy (Lambda lam))
22 type instance Constraint2_of (Proxy (Lambda lam))
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
33 Eq_Type (Type_Type2 (Proxy (Lambda lam)) root) where
35 (Type_Type2 _ arg1 res1)
36 (Type_Type2 _ arg2 res2)
37 | Just Refl <- arg1 `eq_type` arg2
38 , Just Refl <- res1 `eq_type` res2
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 ++ ")"
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'.
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) }
60 -- | Convenient alias to include a 'Type_Fun' within a type.
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))
69 -- | Parse 'Type_Fun'.
71 :: forall (lam :: * -> *) (root :: * -> *) ast ret.
72 ( Lift_Type_Root (Type_Fun lam) root
74 , Root_of_Type root ~ root
75 ) => Proxy (Type_Fun lam root)
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))