Skip to content

Commit

Permalink
futhark fmt: improve specs and sum types.
Browse files Browse the repository at this point in the history
  • Loading branch information
athas committed Nov 7, 2024
1 parent 393f1ae commit 7f8856e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/Futhark/Fmt/Printer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ instance Format UncheckedTypeExp where
fmt (TEUnique te loc) = addComments loc $ "*" <> fmt te
fmt (TEApply te tArgE loc) = addComments loc $ fmt te <+> fmt tArgE
fmt (TEArrow name te0 te1 loc) =
addComments loc $ fmtParamType name te0 <+> "->" </> stdIndent (fmt te1)
addComments loc $
fmtParamType name te0 </> "->" <+> case te1 of
TEArrow {} -> fmt te1
_ -> align (fmt te1)
fmt (TESum tes loc) =
-- Comments can not be inserted correctly here because names do not
-- have a location.
Expand Down Expand Up @@ -170,9 +173,10 @@ instance Format (UncheckedPat t) where
fmt (Wildcard _t loc) = addComments loc "_"
fmt (PatAscription pat t loc) = addComments loc $ fmt pat <> ":" <+> fmt t
fmt (PatLit _e _ loc) = addComments loc $ fmtCopyLoc constantStyle loc
fmt (PatConstr n _ [] loc) =
addComments loc $ "#" <> fmt n
fmt (PatConstr n _ pats loc) =
addComments loc $
"#" <> fmt n </> align (sep line (map fmt pats))
addComments loc $ "#" <> fmt n </> align (sep line (map fmt pats))
fmt (PatAttr attr pat loc) = addComments loc $ fmt attr <+> fmt pat

instance Format (FieldBase NoInfo Name) where
Expand Down Expand Up @@ -272,6 +276,8 @@ instance Format UncheckedExp where
addComments loc $ parens ("." <> idxs')
where
idxs' = brackets $ sep ("," <> space) $ map fmt idxs
fmt (Constr n [] _ loc) =
addComments loc $ "#" <> fmt n
fmt (Constr n cs _ loc) =
addComments loc $ "#" <> fmt n <+> align (sep line $ map fmt cs)
fmt (Attr attr e loc) = addComments loc $ align $ fmt attr </> fmt e
Expand Down Expand Up @@ -477,11 +483,11 @@ instance Format UncheckedSpec where
| null ps = fmtName bindingStyle name
| otherwise = fmtName bindingStyle name </> align (sep line $ map fmt ps)
fmt (ValSpec name ps te _ doc loc) =
addComments loc $ fmt doc <> "val" <+> sub <+> ":" <+> fmt te
addComments loc $ fmt doc <> "val" <+> sub <+> ":" </> stdIndent (fmt te)
where
sub
| null ps = fmtName bindingStyle name
| otherwise = fmtName bindingStyle name </> align (sep line $ map fmt ps)
| otherwise = fmtName bindingStyle name <+> align (sep line $ map fmt ps)
fmt (ModSpec name mte doc loc) =
addComments loc $ fmt doc <> "module" <+> fmtName bindingStyle name <> ":" <+> fmt mte
fmt (IncludeSpec mte loc) = addComments loc $ "include" <+> fmt mte
Expand Down
11 changes: 11 additions & 0 deletions tests_fmt/expected/modules.fut
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,14 @@ module m
def x = 123
def y = 321
}

module type mt4 = {
val f [n] : [n]i32 -> [n]i32

val g [n] :
[n]i32 -> [n]i32

val g [n] :
[n]i32
-> [n]i32
}
13 changes: 13 additions & 0 deletions tests_fmt/modules.fut
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,16 @@ module m : mt1 with a = i32
def x = 123
def y = 321
}

module type mt4 = {
val f [n] : [n]i32 -> [n]i32

val g [n] :
[n]i32 -> [n]i32

val g [n] :
[n]i32
-> [n]i32


}

0 comments on commit 7f8856e

Please sign in to comment.