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