git.iko.soy/rowtype-yoga/purescript-native
on 5ce8345 help sign in
149 lines 3.3 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 Language.PureScript.Names
24import Language.PureScript.Unknown (Unknown(..))
26-- |
27-- The type of types
28--
29data Type
30 -- |
31 -- A unification variable of type Type
32 --
33 = TUnknown (Unknown Type)
34 -- |
35 -- Javascript numbers
36 --
37 | Number
38 -- |
39 -- Javascript strings
40 --
41 | String
42 -- |
43 -- Javascript booleans
44 --
45 | Boolean
46 -- |
47 -- Javascript array type constructor
48 --
49 | Array
50 -- |
51 -- Records, parameterized by a row of types
52 --
53 | Object Type
54 -- |
55 -- A function, with zero or more arguments
56 --
57 | Function [Type] Type
58 -- |
59 -- A named type variable
60 --
61 | TypeVar String
62 -- |
63 -- A type constructor
64 --
65 | TypeConstructor (Qualified ProperName)
66 -- |
67 -- A type application
68 --
69 | TypeApp Type Type
70 -- |
71 -- A type synonym which is \"saturated\", i.e. fully applied
72 --
73 | SaturatedTypeSynonym (Qualified ProperName) [Type]
74 -- |
75 -- Forall quantifier
76 --
77 | ForAll String Type
78 -- |
79 -- A type with a set of type class constraints
80 --
81 | ConstrainedType [(Qualified ProperName, Type)] Type
82 -- |
83 -- A skolem constant
84 --
85 | Skolem Int
86 -- |
87 -- An empty row
88 --
89 | REmpty
90 -- |
91 -- A non-empty row
92 --
93 | RCons String Type Type deriving (Show, Eq, Data, Typeable)
95-- |
96-- Convert a row to a list of pairs of labels and types
97--
98rowToList :: Type -> ([(String, Type)], Type)
99rowToList (RCons name ty row) = let (tys, rest) = rowToList row
100 in ((name, ty):tys, rest)
101rowToList r = ([], r)
103-- |
104-- Convert a list of labels and types to a row
105--
106rowFromList :: ([(String, Type)], Type) -> Type
107rowFromList ([], r) = r
108rowFromList ((name, t):ts, r) = RCons name t (rowFromList (ts, r))
110-- |
111-- Check whether a type is a monotype
112--
113isMonoType :: Type -> Bool
114isMonoType (ForAll _ _) = False
115isMonoType ty = isPolyType ty
117-- |
118-- Check whather a type is a valid polytype
119--
120isPolyType :: Type -> Bool
121isPolyType (Object ps) = all isPolyType (map snd . fst $ rowToList ps)
122isPolyType (Function args ret) = all isPolyType args && isPolyType ret
123isPolyType (TypeApp t1 t2) = isMonoType t1 && isMonoType t2
124isPolyType (SaturatedTypeSynonym _ args) = all isPolyType args
125isPolyType (ForAll _ ty) = isPolyType ty
126isPolyType _ = True
128-- |
129-- Universally quantify a type
130--
131mkForAll :: [String] -> Type -> Type
132mkForAll = flip . foldl . flip $ ForAll
134-- |
135-- The empty record type
136--
137unit :: Type
138unit = Object REmpty
140-- |
141-- Replace a type variable, taking into account variable shadowing
142--
143replaceTypeVars :: (Data d) => String -> Type -> d -> d
144replaceTypeVars name t = everywhereBut (mkQ False isShadowed) (mkT replaceTypeVar)
145 where
146 replaceTypeVar (TypeVar v) | v == name = t
147 replaceTypeVar other = other
148 isShadowed (ForAll v _) | v == name = True
149 isShadowed _ = False