@@ -15,6 +15,8 @@ module AST.Canonical
1515 , Annotation(..)
1616 , Type(..)
1717 , AliasType(..)
18+ , FieldType(..)
19+ , fieldsToList
1820 -- modules
1921 , Module(..)
2022 , Alias(..)
@@ -53,6 +55,7 @@ So it is clear why the data is kept around.
5355import Control.Monad (liftM, liftM2, liftM3, liftM4, replicateM)
5456import Data.Binary
5557import qualified Data.ByteString as B
58+import qualified Data.List as List
5659import qualified Data.Map as Map
5760import Data.Text (Text)
5861
@@ -188,7 +191,7 @@ data Type
188191 = TLambda Type Type
189192 | TVar N.Name
190193 | 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)
192195 | TUnit
193196 | TTuple Type Type (Maybe Type)
194197 | TAlias ModuleName.Canonical N.Name [(N.Name, Type)] AliasType
@@ -199,6 +202,26 @@ data AliasType
199202 | Filled Type
200203
201204
205+data FieldType =
206+ FieldType {-# UNPACK #-} !Word16 Type
207+
208+
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.
212+--
213+fieldsToList :: Map.Map N.Name FieldType -> [(N.Name, Type)]
214+fieldsToList fields =
215+ let
216+ getIndex (_, FieldType index _) =
217+ index
218+
219+ dropIndex (name, FieldType _ tipe) =
220+ (name, tipe)
221+ in
222+ map dropIndex (List.sortOn getIndex (Map.toList fields))
223+
224+
202225
203226-- MODULES
204227
@@ -216,7 +239,7 @@ data Module =
216239 }
217240
218241
219−data Alias = Alias [N.Name] Type (Maybe [(N.Name,Type)])
242+data Alias = Alias [N.Name] Type
220243data Binop = Binop_ Binop.Associativity Binop.Precedence N.Name
221244
222245
@@ -297,8 +320,8 @@ data Manager
297320
298321
299322instance Binary Alias where
300− get = liftM3 Alias get get get
301− put (Alias a b c) = put a >> put b >> put c
323+ get = liftM2 Alias get get
324+ put (Alias a b) = put a >> put b
302325
303326
304327instance Binary Union where
@@ -376,3 +399,8 @@ instance Binary AliasType where
376399 0 -> liftM Holey get
377400 1 -> liftM Filled get
378401 _ -> error "binary encoding of AliasType was corrupted"
402+
403+
404+instance Binary FieldType where
405+ get = liftM2 FieldType get get
406+ put (FieldType a b) = put a >> put b