From 3c339160346a634a03e622d97676bd7add36ae82 Mon Sep 17 00:00:00 2001 From: janmasrovira Date: Wed, 1 Feb 2023 19:22:43 +0100 Subject: [PATCH] Remove braces from let expressions (#1790) --- src/Juvix/Compiler/Abstract/Pretty/Base.hs | 4 +- src/Juvix/Compiler/Concrete/Pretty/Base.hs | 6 +- .../Concrete/Translation/FromSource.hs | 2 +- src/Juvix/Compiler/Internal/Pretty/Base.hs | 4 +- src/Juvix/Data/CodeAnn.hs | 5 +- tests/Compilation/positive/test010.juvix | 10 +-- tests/Compilation/positive/test015.juvix | 2 +- tests/Compilation/positive/test024.juvix | 10 +-- tests/Compilation/positive/test026.juvix | 2 +- tests/Compilation/positive/test032.juvix | 2 +- tests/Compilation/positive/test034.juvix | 8 +- tests/Compilation/positive/test037.juvix | 6 +- tests/negative/LetMissingClause.juvix | 4 +- tests/positive/SignatureWithBody.juvix | 4 +- tests/positive/StdlibList/Data/List.juvix | 84 +++++++++---------- tests/smoke/Commands/repl.smoke.yaml | 2 +- 16 files changed, 78 insertions(+), 77 deletions(-) diff --git a/src/Juvix/Compiler/Abstract/Pretty/Base.hs b/src/Juvix/Compiler/Abstract/Pretty/Base.hs index 49407839a..f6d9a1f95 100644 --- a/src/Juvix/Compiler/Abstract/Pretty/Base.hs +++ b/src/Juvix/Compiler/Abstract/Pretty/Base.hs @@ -118,7 +118,7 @@ ppPipeBlock :: (PrettyCode a, Members '[Reader Options] r, Traversable t) => t a ppPipeBlock items = vsep <$> mapM (fmap (kwPipe <+>) . ppCode) items ppBlock :: (PrettyCode a, Members '[Reader Options] r, Traversable t) => t a -> Sem r (Doc Ann) -ppBlock items = bracesIndent . vsep . toList <$> mapM (fmap endSemicolon . ppCode) items +ppBlock items = vsep . toList <$> mapM (fmap endSemicolon . ppCode) items instance PrettyCode ConstructorApp where ppCode (ConstructorApp ctr args) = do @@ -172,7 +172,7 @@ instance PrettyCode LetClause where instance PrettyCode Let where ppCode l = do - letClauses' <- ppBlock (l ^. letClauses) + letClauses' <- blockIndent <$> ppBlock (l ^. letClauses) letExpression' <- ppCode (l ^. letExpression) return $ kwLet <+> letClauses' <+> kwIn <+> letExpression' diff --git a/src/Juvix/Compiler/Concrete/Pretty/Base.hs b/src/Juvix/Compiler/Concrete/Pretty/Base.hs index e95d4e276..cc65270df 100644 --- a/src/Juvix/Compiler/Concrete/Pretty/Base.hs +++ b/src/Juvix/Compiler/Concrete/Pretty/Base.hs @@ -144,7 +144,7 @@ instance PrettyCode Backend where instance (SingI s) => PrettyCode (Compile s) where ppCode Compile {..} = do compileName' <- ppSymbol _compileName - compileBackendItems' <- ppBlock _compileBackendItems + compileBackendItems' <- bracesIndent <$> ppBlock _compileBackendItems return $ kwCompile <+> compileName' <+> compileBackendItems' instance PrettyCode ForeignBlock where @@ -478,7 +478,7 @@ instance PrettyCode Universe where instance (SingI s) => PrettyCode (LetBlock s) where ppCode LetBlock {..} = do - letClauses' <- ppBlock _letClauses + letClauses' <- blockIndent <$> ppBlock _letClauses letExpression' <- ppExpression _letExpression return $ kwLet <+> letClauses' <+> kwIn <+> letExpression' @@ -488,7 +488,7 @@ instance (SingI s) => PrettyCode (LetClause s) where LetFunClause cl -> ppCode cl ppBlock :: (PrettyCode a, Members '[Reader Options] r, Traversable t) => t a -> Sem r (Doc Ann) -ppBlock items = bracesIndent . vsep <$> mapM (fmap endSemicolon . ppCode) items +ppBlock items = vsep <$> mapM (fmap endSemicolon . ppCode) items ppPipeBlock :: (PrettyCode a, Members '[Reader Options] r, Traversable t) => t a -> Sem r (Doc Ann) ppPipeBlock items = vsep <$> mapM (fmap (kwPipe <+>) . ppCode) items diff --git a/src/Juvix/Compiler/Concrete/Translation/FromSource.hs b/src/Juvix/Compiler/Concrete/Translation/FromSource.hs index b7e3cad2f..5d92f643c 100644 --- a/src/Juvix/Compiler/Concrete/Translation/FromSource.hs +++ b/src/Juvix/Compiler/Concrete/Translation/FromSource.hs @@ -412,7 +412,7 @@ letClause = either LetTypeSig LetFunClause <$> auxTypeSigFunClause letBlock :: (Members '[InfoTableBuilder, JudocStash, NameIdGen] r) => ParsecS r (LetBlock 'Parsed) letBlock = do _letKw <- kw kwLet - _letClauses <- braces (P.sepEndBy1 letClause (kw kwSemicolon)) + _letClauses <- P.sepEndBy1 letClause (kw kwSemicolon) kw kwIn _letExpression <- parseExpressionAtoms return LetBlock {..} diff --git a/src/Juvix/Compiler/Internal/Pretty/Base.hs b/src/Juvix/Compiler/Internal/Pretty/Base.hs index 83829e902..4c4743d13 100644 --- a/src/Juvix/Compiler/Internal/Pretty/Base.hs +++ b/src/Juvix/Compiler/Internal/Pretty/Base.hs @@ -74,7 +74,7 @@ instance PrettyCode Expression where instance PrettyCode Let where ppCode l = do - letClauses' <- ppBlock (l ^. letClauses) + letClauses' <- blockIndent <$> ppBlock (l ^. letClauses) letExpression' <- ppCode (l ^. letExpression) return $ kwLet <+> letClauses' <+> kwIn <+> letExpression' @@ -135,7 +135,7 @@ ppBlock :: (PrettyCode a, Members '[Reader Options] r, Traversable t) => t a -> Sem r (Doc Ann) -ppBlock items = bracesIndent . vsep . toList <$> mapM ppCode items +ppBlock items = vsep . toList <$> mapM ppCode items instance PrettyCode InductiveParameter where ppCode (InductiveParameter v) = do diff --git a/src/Juvix/Data/CodeAnn.hs b/src/Juvix/Data/CodeAnn.hs index fa593e08f..15e75d417 100644 --- a/src/Juvix/Data/CodeAnn.hs +++ b/src/Juvix/Data/CodeAnn.hs @@ -258,4 +258,7 @@ ppStringLit = annotate AnnLiteralString . doubleQuotes . escaped escaped = mconcatMap (pretty . showChar) . unpack bracesIndent :: Doc Ann -> Doc Ann -bracesIndent d = braces (line <> indent' d <> line) +bracesIndent = braces . blockIndent + +blockIndent :: Doc Ann -> Doc Ann +blockIndent d = line <> indent' d <> line diff --git a/tests/Compilation/positive/test010.juvix b/tests/Compilation/positive/test010.juvix index fa44823a3..22d0f1eba 100644 --- a/tests/Compilation/positive/test010.juvix +++ b/tests/Compilation/positive/test010.juvix @@ -5,11 +5,11 @@ open import Stdlib.Prelude; main : IO; main := - let { x : Nat; x := 1 } in - let { x1 : Nat; x1 := x + let { x2 : Nat; x2 := 2 } in x2 } in - let { x3 : Nat; x3 := x1 * x1 } in - let { y : Nat; y := x3 + 2 } in - let { z : Nat; z := x3 + y } in + let x : Nat; x := 1 in + let x1 : Nat; x1 := x + let x2 : Nat; x2 := 2 in x2 in + let x3 : Nat; x3 := x1 * x1 in + let y : Nat; y := x3 + 2 in + let z : Nat; z := x3 + y in printNatLn (x + y + z); end; diff --git a/tests/Compilation/positive/test015.juvix b/tests/Compilation/positive/test015.juvix index 5cf5698c3..d54c207e5 100644 --- a/tests/Compilation/positive/test015.juvix +++ b/tests/Compilation/positive/test015.juvix @@ -7,7 +7,7 @@ open import Stdlib.Data.Nat.Ord; terminating f : Nat → Nat → Nat; f x := - let {g : Nat → Nat; g y := x + y} in + let g : Nat → Nat; g y := x + y in if (x == 0) (f 10) (if (x < 10) diff --git a/tests/Compilation/positive/test024.juvix b/tests/Compilation/positive/test024.juvix index c8c698a38..bc4998fa4 100644 --- a/tests/Compilation/positive/test024.juvix +++ b/tests/Compilation/positive/test024.juvix @@ -16,12 +16,12 @@ terminating f : Tree → Nat; f leaf := 1; f (node l r) := - let {l : Tree; l := g l} in - let {r : Tree; r := g r} in - let {terminating a : Nat; a := λ{leaf := 1 | (node l r) := f l + f r} l} in - let {terminating b : Nat; + let l : Tree; l := g l in + let r : Tree; r := g r in + let terminating a : Nat; a := λ {leaf := 1 | (node l r) := f l + f r} l in + let terminating b : Nat; b := λ {(node l r) := f l + f r - | _ := 2} r} in + | _ := 2} r in a * b; isNode : Tree → Bool; diff --git a/tests/Compilation/positive/test026.juvix b/tests/Compilation/positive/test026.juvix index 11f782e07..efba13935 100644 --- a/tests/Compilation/positive/test026.juvix +++ b/tests/Compilation/positive/test026.juvix @@ -16,7 +16,7 @@ front q := head (qfst q); pop_front : {A : Type} → Queue A → Queue A; pop_front {A} q := - let {q' : Queue A; q' := queue (tail (qfst q)) (qsnd q)} in + let q' : Queue A; q' := queue (tail (qfst q)) (qsnd q) in case (qfst q') λ{ nil := queue (reverse (qsnd q')) nil | _ := q' diff --git a/tests/Compilation/positive/test032.juvix b/tests/Compilation/positive/test032.juvix index 4a28157dc..404fa009f 100644 --- a/tests/Compilation/positive/test032.juvix +++ b/tests/Compilation/positive/test032.juvix @@ -20,7 +20,7 @@ merge' xs@(x :: xs') ys@(y :: ys') := if (x <= y) (x :: merge' xs' ys) (y :: mer terminating sort : List Nat → List Nat; sort xs := - let {n : Nat; n := length xs} in + let n : Nat; n := length xs in if (n <= 1) xs (case (split (div n 2) xs) λ{ diff --git a/tests/Compilation/positive/test034.juvix b/tests/Compilation/positive/test034.juvix index b9e1e3755..8667e08bd 100644 --- a/tests/Compilation/positive/test034.juvix +++ b/tests/Compilation/positive/test034.juvix @@ -4,16 +4,16 @@ module test034; open import Stdlib.Data.Nat.Ord; sum : Nat → Nat; - sum := let { + sum := let sum' : Nat → Nat; sum' := λ { zero := zero | (suc n) := suc n + sum' n }; - } in sum'; + in sum'; mutrec : IO; - mutrec := let { + mutrec := let terminating f : Nat → Nat; terminating @@ -23,7 +23,7 @@ module test034; f x := if (x < 1) 1 (g (sub x 1) + 2 * x); g x := if (x < 1) 1 (x + h (sub x 1)); h x := if (x < 1) 1 (x * f (sub x 1)); - } in printNatLn (f 5) + in printNatLn (f 5) >> printNatLn (f 10) >> printNatLn (g 5) >> printNatLn (h 5); diff --git a/tests/Compilation/positive/test037.juvix b/tests/Compilation/positive/test037.juvix index fe57aed16..0895515ca 100644 --- a/tests/Compilation/positive/test037.juvix +++ b/tests/Compilation/positive/test037.juvix @@ -8,13 +8,13 @@ module test037; (x :: _) := x | nil := id } - (let { + (let y : Nat → Nat; y := id; - } in (let { + in (let z : (Nat → Nat) → Nat → Nat; z := id; - } in case l λ { (_ :: _) := id } z) + in case l λ { (_ :: _) := id } z) y) 7; diff --git a/tests/negative/LetMissingClause.juvix b/tests/negative/LetMissingClause.juvix index 63955c6dc..1ec8e66f2 100644 --- a/tests/negative/LetMissingClause.juvix +++ b/tests/negative/LetMissingClause.juvix @@ -1,6 +1,6 @@ module LetMissingClause; id : {A : Type} → A → A; - id {A} := let { + id {A} := let id' : A → A; - } in id'; + in id'; end; diff --git a/tests/positive/SignatureWithBody.juvix b/tests/positive/SignatureWithBody.juvix index f6264c161..0d418a80f 100644 --- a/tests/positive/SignatureWithBody.juvix +++ b/tests/positive/SignatureWithBody.juvix @@ -6,10 +6,10 @@ module SignatureWithBody; | _ := false }; - isNull' : {A : Type} → List A → Bool := let { + isNull' : {A : Type} → List A → Bool := let aux : {A : Type} → List A → Bool := λ { | nil := true | _ := false }; - } in aux; + in aux; end; diff --git a/tests/positive/StdlibList/Data/List.juvix b/tests/positive/StdlibList/Data/List.juvix index 48fd42d4a..f28e3668c 100644 --- a/tests/positive/StdlibList/Data/List.juvix +++ b/tests/positive/StdlibList/Data/List.juvix @@ -12,21 +12,21 @@ match : {A : Type} → {B : Type} → A → (A → B) → B; match x f := f x; foldr : (a : Type) → (b : Type) → (a → b → b) → b → List a → b; -foldr _ _ _ z (nil _) := z; -foldr a b f z (:: _ h hs) := f h (foldr a b f z hs); +foldr _ _ _ z nil := z; +foldr a b f z (:: h hs) := f h (foldr a b f z hs); foldl : (a : Type) → (b : Type) → (b → a → b) → b → List a → b; -foldl a b f z (nil _) := z ; -foldl a b f z (:: _ h hs) := foldl a b f (f z h) hs; +foldl a b f z nil := z ; +foldl a b f z (:: h hs) := foldl a b f (f z h) hs; map : (a : Type) → (b : Type) → (a → b) → List a → List b; -map _ b f (nil _) := nil b; -map a b f (:: _ h hs) := :: a (f h) (map a b f hs); +map _ b f nil := nil; +map a b f (:: h hs) := :: (f h) (map a b f hs); filter : (a : Type) → (a → Bool) → List a → List a; -filter a f (nil _) := nil a; -filter a f (:: _ h hs) := match (f h) λ { - | true := :: a h (filter a f hs) +filter a f nil := nil; +filter a f (:: h hs) := match (f h) λ { + | true := :: h (filter a f hs) | false := filter a f hs }; @@ -34,24 +34,24 @@ import Data.Nat; open Data.Nat; length : (a : Type) → List a → ℕ; -length _ (nil _) := zero; -length a (:: _ _ l) := suc (length a l); +length _ nil := zero; +length a (:: _ l) := suc (length a l); reverse : (a : Type) → List a → List a; reverse a l := - let { + let rev : List a → List a → List a; - rev (nil _) a := a; - rev (:: _ x xs) a := rev xs (:: a x a) - } in rev l (nil a); + rev nil a := a; + rev (:: x xs) a := rev xs (:: x a) + in rev l nil; replicate : (a : Type) → ℕ → a → List a; -replicate a zero _ := nil a; -replicate a (suc n) x := :: a x (replicate a n x); +replicate a zero _ := nil; +replicate a (suc n) x := :: x (replicate a n x); take : (a : Type) → ℕ → List a → List a; -take a (suc n) (:: _ x xs) := :: a x (take a n xs); -take a _ _ := nil a; +take a (suc n) (:: x xs) := :: x (take a n xs); +take a _ _ := nil; import Data.Ord; open Data.Ord; @@ -59,32 +59,32 @@ open Data.Ord; import Data.Product; open Data.Product; -splitAt : (a : Type) → ℕ → List a → List a; -splitAt a _ (nil _) := nil a, nil a; -splitAt a zero xs := , (List a) (List a) (nil a) xs; -splitAt a (suc zero) (:: _ x xs) := , (List a) (List a) (:: a x (nil a)) xs; -splitAt a (suc (suc m)) (:: _ x xs) := match (splitAt a m xs) λ { - (, la _ xs' xs'') := , la la (:: a x xs') xs'' +splitAt : (a : Type) → ℕ → List a → List a × List a; +splitAt a _ nil := , nil nil ; +splitAt a zero xs := , nil xs; +splitAt a (suc zero) (:: x xs) := , (:: x nil) xs; +splitAt a (suc (suc m)) (:: x xs) := match (splitAt a m xs) λ { + (, xs' xs'') := , (:: x xs') xs'' }; -merge : (a : Type) → (a → a → Ordering) → List a → List a → List a; -merge a cmp (:: _ x xs) (:: _ y ys) := match (cmp x y) λ { - | LT := :: a x (merge a cmp xs (:: a y ys)) - | _ := :: a y (merge a cmp (:: a x xs) ys) +terminating merge : (a : Type) → (a → a → Ordering) → List a → List a → List a; +merge a cmp (:: x xs) (:: y ys) := match (cmp x y) λ { + | LT := :: x (merge a cmp xs (:: y ys)) + | _ := :: y (merge a cmp (:: x xs) ys) }; -merge _ _ (nil _) ys := ys; -merge _ _ xs (nil _) := xs; +merge _ _ nil ys := ys; +merge _ _ xs nil := xs; -- infixr 5 ++; waiting for implicit arguments ++ : (a : Type) → List a → List a → List a; -++ a (nil _) ys := ys; -++ a (:: _ x xs) ys := :: a x (++ a xs ys); +++ a nil ys := ys; +++ a (:: x xs) ys := :: x (++ a xs ys); -quickSort : (a : Type) → (a → a → Ordering) → List a → List a; -quickSort a _ (nil _) := nil a; -quickSort a _ (:: _ x (nil _)) := :: a x (nil a); -quickSort a cmp (:: _ x ys) := - let { +terminating quickSort : (a : Type) → (a → a → Ordering) → List a → List a; +quickSort a _ nil := nil; +quickSort a _ (:: x nil) := :: x nil; +quickSort a cmp (:: x ys) := + let ltx : a → Bool; ltx y := match (cmp y x) λ{ | LT := true @@ -92,10 +92,8 @@ quickSort a cmp (:: _ x ys) := }; gex : a → Bool; gex y := not (ltx y) - } in - { - ++ a (quickSort a (filter a ltx) ys) - (++ a (:: a x (nil a)) (quickSort a (filter a gex) ys)) - }; + in + ++ a (quickSort a cmp (filter a ltx ys)) + (++ a (:: x nil) (quickSort a cmp (filter a gex ys))); end; diff --git a/tests/smoke/Commands/repl.smoke.yaml b/tests/smoke/Commands/repl.smoke.yaml index 4b6121184..a99fc8c7d 100644 --- a/tests/smoke/Commands/repl.smoke.yaml +++ b/tests/smoke/Commands/repl.smoke.yaml @@ -122,7 +122,7 @@ tests: command: - juvix - repl - stdin: "let {x : Nat; x := 2 + 1} in x" + stdin: "let x : Nat; x := 2 + 1 in x" stdout: contains: "suc (suc (suc zero))"