Skip to content

Commit

Permalink
Parse constructors to OrderedMap
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Grant Jeffries committed Nov 11, 2019
1 parent e99072b commit cee86ed
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion bowtie/src/Bowtie/Surface/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ parse path =

-- |
-- >>> parseTest sourceParser "type A = A\n\ntype B = B"
-- AST {astTypes = OrderedMap (fromList [(Id "A",(0,TypeDeclaration [] (fromList [(Id "A",[])]))),(Id "B",(1,TypeDeclaration [] (fromList [(Id "B",[])])))]) (fromList [(0,(Id "A",TypeDeclaration [] (fromList [(Id "A",[])]))),(1,(Id "B",TypeDeclaration [] (fromList [(Id "B",[])])))]), astTerms = OrderedMap (fromList []) (fromList [])}
-- AST {astTypes = OrderedMap (fromList [(Id "A",(0,TypeDeclaration [] (OrderedMap (fromList [(Id "A",(0,[]))]) (fromList [(0,(Id "A",[]))])))),(Id "B",(1,TypeDeclaration [] (OrderedMap (fromList [(Id "B",(0,[]))]) (fromList [(0,(Id "B",[]))]))))]) (fromList [(0,(Id "A",TypeDeclaration [] (OrderedMap (fromList [(Id "A",(0,[]))]) (fromList [(0,(Id "A",[]))])))),(1,(Id "B",TypeDeclaration [] (OrderedMap (fromList [(Id "B",(0,[]))]) (fromList [(0,(Id "B",[]))]))))]), astTerms = OrderedMap (fromList []) (fromList [])}
--
-- ^^ TODO: OrderedMaps do not do well with Show
sourceParser :: Parser AST
Expand Down
3 changes: 2 additions & 1 deletion bowtie/src/Bowtie/Type/AST.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Bowtie.Type.AST where

import Bowtie.Lib.FreeVars
import Bowtie.Lib.OrderedMap (OrderedMap)
import Bowtie.Lib.Prelude

import qualified Data.Set as Set
Expand All @@ -9,7 +10,7 @@ import qualified Data.Set as Set
data TypeDeclaration
= TypeDeclaration
[Id]-- ^ Polymorphic over these variables
(HashMap Id [Type])
(OrderedMap Id [Type])
deriving (Eq, Show)

data Type
Expand Down
2 changes: 1 addition & 1 deletion bowtie/src/Bowtie/Type/Kindcheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ kindcheck =
-- E.g. if the first is Maybe, the second is Nothing and Just.
constructorsFromDecls :: (Id, TypeDeclaration) -> [(Id, TypeScheme)]
constructorsFromDecls (typeId, TypeDeclaration polyVars constructors) =
fmap constructorType (HashMap.toList constructors)
fmap constructorType (OrderedMap.toList constructors)
where
constructorType :: (Id, [Type]) -> (Id, TypeScheme)
constructorType (conId, args) =
Expand Down
13 changes: 9 additions & 4 deletions bowtie/src/Bowtie/Type/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import Control.Applicative.Combinators.NonEmpty
import Text.Megaparsec hiding
(State, Token, parse, parseTest, runParser, sepBy1)

import qualified Bowtie.Lib.OrderedMap as OrderedMap
import qualified Data.Char as Char
import qualified Data.HashMap.Strict as HashMap
import qualified Data.List.NonEmpty as NE
import qualified Data.Text as Text
import qualified Text.Megaparsec as Mega
Expand All @@ -54,9 +54,9 @@ type ParserErrorBundle = ParseErrorBundle Text Void
-- )
--
-- >>> parseTest declEntryParser "type Foo a b = Bar Bool a | Baz Int b"
-- (Id "Foo",TypeDeclaration [Id "a",Id "b"] (fromList [(Id "Baz",[TConstructor (Id "Int"),TVariable (Id "b")]),(Id "Bar",[TConstructor (Id "Bool"),TVariable (Id "a")])]))
-- (Id "Foo",TypeDeclaration [Id "a",Id "b"] (OrderedMap (fromList [(Id "Baz",(1,[TConstructor (Id "Int"),TVariable (Id "b")])),(Id "Bar",(0,[TConstructor (Id "Bool"),TVariable (Id "a")]))]) (fromList [(0,(Id "Bar",[TConstructor (Id "Bool"),TVariable (Id "a")])),(1,(Id "Baz",[TConstructor (Id "Int"),TVariable (Id "b")]))])))
--
-- TODO: switch type constructors to ordered map
-- ^^ TODO: OrderedMaps do not do well with Show
declEntryParser :: Parser (Id, TypeDeclaration)
declEntryParser = do
-- Note that we don't parse a @Break@ here. If each top level
Expand All @@ -67,7 +67,12 @@ declEntryParser = do
typeArgs <- many (lexeme lowerIdParser)
symbol "="
constructors <- constructorParser `sepBy` symbol "|"
pure (typeId, TypeDeclaration typeArgs (HashMap.fromList constructors))
case OrderedMap.fromList constructors of
Left id ->
fail ("Duplicate constructors found in type declaration with id: " <> Text.unpack (unId id))

Right constructorMap ->
pure (typeId, TypeDeclaration typeArgs constructorMap)

-- |
-- >>> parseTest constructorParser "Bar Bool a"
Expand Down

0 comments on commit cee86ed

Please sign in to comment.