@@ -15,6 +15,8 @@ module AST.Canonical
15 , Annotation(..)
15 , Annotation(..)
16 , Type(..)
16 , Type(..)
17 , AliasType(..)
17 , AliasType(..)
18 -- modules
20 -- modules
19 , Module(..)
21 , Module(..)
20 , Alias(..)
22 , Alias(..)
@@ -53,6 +55,7 @@ So it is clear why the data is kept around.
53import Control.Monad (liftM, liftM2, liftM3, liftM4, replicateM)
55import Control.Monad (liftM, liftM2, liftM3, liftM4, replicateM)
54import Data.Binary
56import Data.Binary
55import qualified Data.ByteString as B
57import qualified Data.ByteString as B
58+import qualified Data.List as List
56import qualified Data.Map as Map
59import qualified Data.Map as Map
57import Data.Text (Text)
60import Data.Text (Text)
@@ -188,7 +191,7 @@ data Type
188 = TLambda Type Type
191 = TLambda Type Type
189 | TVar N.Name
192 | TVar N.Name
190 | TType ModuleName.Canonical N.Name [Type]
193 | TType ModuleName.Canonical N.Name [Type]
191− | TRecord (Map.Map N.Name Type) (Maybe N.Name)
194+ | TRecord (Map.Map N.Name FieldType) (Maybe N.Name)
193 | TTuple Type Type (Maybe Type)
196 | TTuple Type Type (Maybe Type)
194 | TAlias ModuleName.Canonical N.Name [(N.Name, Type)] AliasType
197 | TAlias ModuleName.Canonical N.Name [(N.Name, Type)] AliasType
@@ -199,6 +202,26 @@ data AliasType
199 | Filled Type
202 | Filled Type
206+ FieldType {-# UNPACK #-} !Word16 Type
209+-- NOTE: The Word16 marks the source order, but it may not be available
210+-- for every canonical type. For example, if the canonical type is inferred
211+-- the orders will all be zeros.
213+fieldsToList :: Map.Map N.Name FieldType -> [(N.Name, Type)]
214+fieldsToList fields =
216+ getIndex (_, FieldType index _) =
219+ dropIndex (name, FieldType _ tipe) =
222+ map dropIndex (List.sortOn getIndex (Map.toList fields))
203-- MODULES
226-- MODULES
@@ -216,7 +239,7 @@ data Module =
219−data Alias = Alias [N.Name] Type (Maybe [(N.Name,Type)])
242+data Alias = Alias [N.Name] Type
220data Binop = Binop_ Binop.Associativity Binop.Precedence N.Name
243data Binop = Binop_ Binop.Associativity Binop.Precedence N.Name
@@ -297,8 +320,8 @@ data Manager
299instance Binary Alias where
322instance Binary Alias where
300− get = liftM3 Alias get get get
323+ get = liftM2 Alias get get
301− put (Alias a b c) = put a >> put b >> put c
324+ put (Alias a b) = put a >> put b
304instance Binary Union where
327instance Binary Union where
@@ -376,3 +399,8 @@ instance Binary AliasType where
376 0 -> liftM Holey get
399 0 -> liftM Holey get
377 1 -> liftM Filled get
400 1 -> liftM Filled get
378 _ -> error "binary encoding of AliasType was corrupted"
401 _ -> error "binary encoding of AliasType was corrupted"
404+instance Binary FieldType where
405+ get = liftM2 FieldType get get
406+ put (FieldType a b) = put a >> put b