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