git.iko.soy/rowtype-yoga/purescript-native
on 7873a98 help sign in
155 lines 3.5 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Language.PureScript.Types
4-- Copyright : (c) Phil Freeman 2013
5-- License : MIT
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- |
12-- Data types for types
13--
14-----------------------------------------------------------------------------
16{-# LANGUAGE DeriveDataTypeable #-}
18module Language.PureScript.Types where
20import Data.Data
21import Data.Generics (mkT, mkQ, everywhereBut)
23import Control.Monad.Unify
25import Language.PureScript.Names
27-- |
28-- The type of types
29--
30data Type
31 -- |
32 -- A unification variable of type Type
33 --
34 = TUnknown Unknown
35 -- |
36 -- Javascript numbers
37 --
38 | Object Type
39 -- |
40 -- A named type variable
41 --
42 | TypeVar String
43 -- |
44 -- A type constructor
45 --
46 | TypeConstructor (Qualified ProperName)
47 -- |
48 -- A type application
49 --
50 | TypeApp Type Type
51 -- |
52 -- A type synonym which is \"saturated\", i.e. fully applied
53 --
54 | SaturatedTypeSynonym (Qualified ProperName) [Type]
55 -- |
56 -- Forall quantifier
57 --
58 | ForAll String Type
59 -- |
60 -- A type with a set of type class constraints
61 --
62 | ConstrainedType [(Qualified ProperName, Type)] Type
63 -- |
64 -- A skolem constant
65 --
66 | Skolem Int
67 -- |
68 -- An empty row
69 --
70 | REmpty
71 -- |
72 -- A non-empty row
73 --
74 | RCons String Type Type deriving (Show, Eq, Data, Typeable)
76-- |
77-- Type constructor for functions
78--
79tyFunction :: Type
80tyFunction = TypeConstructor $ (Qualified $ Just $ ModuleName $ ProperName "Prelude") (ProperName "Function")
82-- |
83-- Type constructor for strings
84--
85tyString :: Type
86tyString = TypeConstructor $ (Qualified $ Just $ ModuleName $ ProperName "Prelude") (ProperName "String")
88-- |
89-- Type constructor for numbers
90--
91tyNumber :: Type
92tyNumber = TypeConstructor $ (Qualified $ Just $ ModuleName $ ProperName "Prelude") (ProperName "Number")
94-- |
95-- Type constructor for booleans
96--
97tyBoolean :: Type
98tyBoolean = TypeConstructor $ (Qualified $ Just $ ModuleName $ ProperName "Prelude") (ProperName "Boolean")
100-- |
101-- Type constructor for arrays
102--
103tyArray :: Type
104tyArray = TypeConstructor $ (Qualified $ Just $ ModuleName $ ProperName "Prelude") (ProperName "Array")
106-- |
107-- Smart constructor for function types
108--
109function :: Type -> Type -> Type
110function t1 t2 = TypeApp (TypeApp tyFunction t1) t2
112-- |
113-- Convert a row to a list of pairs of labels and types
114--
115rowToList :: Type -> ([(String, Type)], Type)
116rowToList (RCons name ty row) = let (tys, rest) = rowToList row
117 in ((name, ty):tys, rest)
118rowToList r = ([], r)
120-- |
121-- Convert a list of labels and types to a row
122--
123rowFromList :: ([(String, Type)], Type) -> Type
124rowFromList ([], r) = r
125rowFromList ((name, t):ts, r) = RCons name t (rowFromList (ts, r))
127-- |
128-- Check whether a type is a monotype
129--
130isMonoType :: Type -> Bool
131isMonoType (ForAll _ _) = False
132isMonoType ty = True
134-- |
135-- Universally quantify a type
136--
137mkForAll :: [String] -> Type -> Type
138mkForAll = flip . foldl . flip $ ForAll
140-- |
141-- The empty record type
142--
143unit :: Type
144unit = Object REmpty
146-- |
147-- Replace a type variable, taking into account variable shadowing
148--
149replaceTypeVars :: (Data d) => String -> Type -> d -> d
150replaceTypeVars name t = everywhereBut (mkQ False isShadowed) (mkT replaceTypeVar)
151 where
152 replaceTypeVar (TypeVar v) | v == name = t
153 replaceTypeVar other = other
154 isShadowed (ForAll v _) | v == name = True
155 isShadowed _ = False