@@ -14,6 +14,7 @@ import Data.Map (Map)
1414import Data.Maybe (Maybe (..), maybe )
1515import Data.Newtype (alaF )
1616import Data.String (trim , split , joinWith )
17+ import Data.String.CodeUnits (singleton , toCharArray )
1718import Data.String.Pattern (Pattern (..))
1819import Data.String.Regex (regex , match )
1920import Data.String.Regex.Flags (noFlags )
@@ -74,7 +75,7 @@ inferType ctx tm = { term, bounds, matches: matches <> typeToMatches, errors }
7475 replaceInferredType bounds $
7576 (acc <> step <> empty { bounds = bounds, matches = [Matched matches] })
7677 { type = Ty (a <#> replaceBoxed bounds) (c <#> replaceBoxed bounds) }
77- tmWithDecl = tm # traverse (\{ bid, box } -> Map .lookup bid ctx # maybe (Left $ " Undeclared name: " <> show bid) \decl -> Right { bid, box, decl })
78+ tmWithDecl = tm # traverse (\{ bid, box } -> Map .lookup bid ctx # maybe (Left $ " Undeclared name: " <> show bid) \decl -> Right { bid, box, decl: decl.type })
7879 fatTerm = tmWithDecl # either (const (Fix (Ann empty TUnit ))) (reannotateFix alg)
7980 typeToMatches = case (getAnn fatTerm).type of Ty l r -> [Unmatched Valid Input l, Unmatched Valid Output r]
8081 { bounds, matches, errors } = getAnn fatTerm
@@ -145,7 +146,7 @@ getSubTerm' _ _ mkTerm = mkTerm []
145146
146147
147148defaultEnv :: Context String String
148- defaultEnv = Map .fromFoldable
149+ defaultEnv = Map .fromFoldable $ map (\(name /\ ty) -> name /\ { name, type: ty })
149150 [ " " /\ Perm []
150151 , " -" /\ Perm [1 ]
151152 , " =" /\ Perm [1 , 2 ]
@@ -167,22 +168,28 @@ parseContext = spl "\n" >>> alaF App foldMap toEntry
167168 where
168169 spl :: String -> String -> Array String
169170 spl p = trim >>> split (Pattern p) >>> map trim
171+ parseName :: String -> { name :: String , bids :: Array String }
172+ parseName nameBid = case nameBid # spl " @" of
173+ [name, bids] -> { name, bids: toCharArray bids <#> singleton }
174+ _ -> { name: nameBid, bids: toCharArray nameBid <#> singleton }
170175 toEntry :: String -> Either String (Context String String )
171176 toEntry line = case spl " :" line of
172- [name, typ] -> case typ # spl " ->" <#> (spl " " >>> filter (_ /= " " )) of
173- [left, right] -> pure $ Map .singleton name (Gen $ Ty left right)
174- _ -> do
175- permRe <- regex " ^\\ [(.*)\\ ]$" noFlags
176- case match permRe typ # map toArray of
177- Just [_, Just perm] ->
178- pure $ Map .singleton name (Perm $ perm # (spl " " >>> filter (_ /= " " ) >>> map (readInt 10 >>> floor)))
179- _ -> do
180- spiderRe <- regex " ^(\\ d+)(o|\\ .)(\\ d+)$" noFlags
181- case match spiderRe typ # map toArray of
182- Just [_, Just ls, Just cs, Just rs] -> pure $ Map .singleton name (Spider
183- (if cs == " ." then Black else White )
184- (readInt 10 ls # floor)
185- (readInt 10 rs # floor)
186- )
187- _ -> Left $ " Invalid type: " <> typ
177+ [nameBid, typ] -> let { name, bids } = parseName nameBid in
178+ (\ty -> foldMap (\bid -> Map .singleton bid { name, type: ty }) bids) <$>
179+ case typ # spl " ->" <#> (spl " " >>> filter (_ /= " " )) of
180+ [left, right] -> pure $ Gen $ Ty left right
181+ _ -> do
182+ permRe <- regex " ^\\ [(.*)\\ ]$" noFlags
183+ case match permRe typ # map toArray of
184+ Just [_, Just perm] ->
185+ pure (Perm $ perm # (spl " " >>> filter (_ /= " " ) >>> map (readInt 10 >>> floor)))
186+ _ -> do
187+ spiderRe <- regex " ^(\\ d+)(o|\\ .)(\\ d+)$" noFlags
188+ case match spiderRe typ # map toArray of
189+ Just [_, Just ls, Just cs, Just rs] -> pure (Spider
190+ (if cs == " ." then Black else White )
191+ (readInt 10 ls # floor)
192+ (readInt 10 rs # floor)
193+ )
194+ _ -> Left $ " Invalid type: " <> typ
188195 _ -> Left $ " Invalid signature: " <> line
0 commit comments