-
Notifications
You must be signed in to change notification settings - Fork 33
/
Monotype.hs
120 lines (109 loc) · 3.48 KB
/
Monotype.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveLift #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE OverloadedStrings #-}
{-| This module stores the `Monotype` type representing monomorphic types and
utilites for operating on `Monotype`s
-}
module Grace.Monotype
( -- * Types
Monotype(..)
, Scalar(..)
, Record(..)
, RemainingFields(..)
, Union(..)
, RemainingAlternatives(..)
) where
import Data.String (IsString(..))
import Data.Text (Text)
import GHC.Generics (Generic)
import Grace.Existential (Existential)
import Grace.Pretty (Pretty(..), builtin)
import Language.Haskell.TH.Syntax (Lift)
{-| A monomorphic type
This is same type as `Grace.Type.Type`, except without the
`Grace.Type.Forall` and `Grace.Type.Exists` constructors
-}
data Monotype
= VariableType Text
| UnsolvedType (Existential Monotype)
| Function Monotype Monotype
| Optional Monotype
| List Monotype
| Record Record
| Union Union
| Scalar Scalar
deriving stock (Eq, Generic, Show)
instance IsString Monotype where
fromString string = VariableType (fromString string)
-- | A scalar type
data Scalar
= Bool
-- ^ Boolean type
--
-- >>> pretty Bool
-- Bool
| Real
-- ^ Real number type
--
-- >>> pretty Real
-- Real
| Integer
-- ^ Integer number type
--
-- >>> pretty Integer
-- Integer
| JSON
-- ^ JSON type
--
-- >>> pretty JSON
-- JSON
| Natural
-- ^ Natural number type
--
-- >>> pretty Natural
-- Natural
| Text
-- ^ Text type
--
-- >>> pretty Text
-- Text
deriving stock (Eq, Generic, Lift, Show)
instance Pretty Scalar where
pretty Bool = builtin "Bool"
pretty Real = builtin "Real"
pretty JSON = builtin "JSON"
pretty Natural = builtin "Natural"
pretty Integer = builtin "Integer"
pretty Text = builtin "Text"
-- | A monomorphic record type
data Record = Fields [(Text, Monotype)] RemainingFields
deriving stock (Eq, Generic, Show)
-- | This represents whether or not the record type is open or closed
data RemainingFields
= EmptyFields
-- ^ The record type is closed, meaning that all fields are known
| UnsolvedFields (Existential Record)
-- ^ The record type is open, meaning that some fields are known and there
-- is an unsolved fields variable that is a placeholder for other fields
-- that may or may not be present
| VariableFields Text
-- ^ Same as `UnsolvedFields`, except that the user has given the fields
-- variable an explicit name in the source code
deriving stock (Eq, Generic, Lift, Show)
-- | A monomorphic union type
data Union = Alternatives [(Text, Monotype)] RemainingAlternatives
deriving stock (Eq, Generic, Show)
-- | This represents whether or not the union type is open or closed
data RemainingAlternatives
= EmptyAlternatives
-- ^ The union type is closed, meaning that all alternatives are known
| UnsolvedAlternatives (Existential Union)
-- ^ The union type is open, meaning that some alternatives are known and
-- there is an unsolved alternatives variable that is a placeholder for
-- other alternatives that may or may not be present
| VariableAlternatives Text
-- ^ Same as `UnsolvedAlternatives`, except that the user has given the
-- alternatives variable an explicit name in the source code
deriving stock (Eq, Generic, Lift, Show)