1
1
mirror of https://github.com/anoma/juvix.git synced 2025-01-07 08:08:44 +03:00

Remove braces from let expressions (#1790)

This commit is contained in:
janmasrovira 2023-02-01 19:22:43 +01:00 committed by GitHub
parent 5ec80641cb
commit 3c33916034
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 78 additions and 77 deletions

View File

@ -118,7 +118,7 @@ ppPipeBlock :: (PrettyCode a, Members '[Reader Options] r, Traversable t) => t a
ppPipeBlock items = vsep <$> mapM (fmap (kwPipe <+>) . ppCode) items ppPipeBlock items = vsep <$> mapM (fmap (kwPipe <+>) . ppCode) items
ppBlock :: (PrettyCode a, Members '[Reader Options] r, Traversable t) => t a -> Sem r (Doc Ann) 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 instance PrettyCode ConstructorApp where
ppCode (ConstructorApp ctr args) = do ppCode (ConstructorApp ctr args) = do
@ -172,7 +172,7 @@ instance PrettyCode LetClause where
instance PrettyCode Let where instance PrettyCode Let where
ppCode l = do ppCode l = do
letClauses' <- ppBlock (l ^. letClauses) letClauses' <- blockIndent <$> ppBlock (l ^. letClauses)
letExpression' <- ppCode (l ^. letExpression) letExpression' <- ppCode (l ^. letExpression)
return $ kwLet <+> letClauses' <+> kwIn <+> letExpression' return $ kwLet <+> letClauses' <+> kwIn <+> letExpression'

View File

@ -144,7 +144,7 @@ instance PrettyCode Backend where
instance (SingI s) => PrettyCode (Compile s) where instance (SingI s) => PrettyCode (Compile s) where
ppCode Compile {..} = do ppCode Compile {..} = do
compileName' <- ppSymbol _compileName compileName' <- ppSymbol _compileName
compileBackendItems' <- ppBlock _compileBackendItems compileBackendItems' <- bracesIndent <$> ppBlock _compileBackendItems
return $ kwCompile <+> compileName' <+> compileBackendItems' return $ kwCompile <+> compileName' <+> compileBackendItems'
instance PrettyCode ForeignBlock where instance PrettyCode ForeignBlock where
@ -478,7 +478,7 @@ instance PrettyCode Universe where
instance (SingI s) => PrettyCode (LetBlock s) where instance (SingI s) => PrettyCode (LetBlock s) where
ppCode LetBlock {..} = do ppCode LetBlock {..} = do
letClauses' <- ppBlock _letClauses letClauses' <- blockIndent <$> ppBlock _letClauses
letExpression' <- ppExpression _letExpression letExpression' <- ppExpression _letExpression
return $ kwLet <+> letClauses' <+> kwIn <+> letExpression' return $ kwLet <+> letClauses' <+> kwIn <+> letExpression'
@ -488,7 +488,7 @@ instance (SingI s) => PrettyCode (LetClause s) where
LetFunClause cl -> ppCode cl LetFunClause cl -> ppCode cl
ppBlock :: (PrettyCode a, Members '[Reader Options] r, Traversable t) => t a -> Sem r (Doc Ann) 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 :: (PrettyCode a, Members '[Reader Options] r, Traversable t) => t a -> Sem r (Doc Ann)
ppPipeBlock items = vsep <$> mapM (fmap (kwPipe <+>) . ppCode) items ppPipeBlock items = vsep <$> mapM (fmap (kwPipe <+>) . ppCode) items

View File

@ -412,7 +412,7 @@ letClause = either LetTypeSig LetFunClause <$> auxTypeSigFunClause
letBlock :: (Members '[InfoTableBuilder, JudocStash, NameIdGen] r) => ParsecS r (LetBlock 'Parsed) letBlock :: (Members '[InfoTableBuilder, JudocStash, NameIdGen] r) => ParsecS r (LetBlock 'Parsed)
letBlock = do letBlock = do
_letKw <- kw kwLet _letKw <- kw kwLet
_letClauses <- braces (P.sepEndBy1 letClause (kw kwSemicolon)) _letClauses <- P.sepEndBy1 letClause (kw kwSemicolon)
kw kwIn kw kwIn
_letExpression <- parseExpressionAtoms _letExpression <- parseExpressionAtoms
return LetBlock {..} return LetBlock {..}

View File

@ -74,7 +74,7 @@ instance PrettyCode Expression where
instance PrettyCode Let where instance PrettyCode Let where
ppCode l = do ppCode l = do
letClauses' <- ppBlock (l ^. letClauses) letClauses' <- blockIndent <$> ppBlock (l ^. letClauses)
letExpression' <- ppCode (l ^. letExpression) letExpression' <- ppCode (l ^. letExpression)
return $ kwLet <+> letClauses' <+> kwIn <+> letExpression' return $ kwLet <+> letClauses' <+> kwIn <+> letExpression'
@ -135,7 +135,7 @@ ppBlock ::
(PrettyCode a, Members '[Reader Options] r, Traversable t) => (PrettyCode a, Members '[Reader Options] r, Traversable t) =>
t a -> t a ->
Sem r (Doc Ann) Sem r (Doc Ann)
ppBlock items = bracesIndent . vsep . toList <$> mapM ppCode items ppBlock items = vsep . toList <$> mapM ppCode items
instance PrettyCode InductiveParameter where instance PrettyCode InductiveParameter where
ppCode (InductiveParameter v) = do ppCode (InductiveParameter v) = do

View File

@ -258,4 +258,7 @@ ppStringLit = annotate AnnLiteralString . doubleQuotes . escaped
escaped = mconcatMap (pretty . showChar) . unpack escaped = mconcatMap (pretty . showChar) . unpack
bracesIndent :: Doc Ann -> Doc Ann 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

View File

@ -5,11 +5,11 @@ open import Stdlib.Prelude;
main : IO; main : IO;
main := main :=
let { x : Nat; x := 1 } in let x : Nat; x := 1 in
let { x1 : Nat; x1 := x + let { x2 : Nat; x2 := 2 } in x2 } in let x1 : Nat; x1 := x + let x2 : Nat; x2 := 2 in x2 in
let { x3 : Nat; x3 := x1 * x1 } in let x3 : Nat; x3 := x1 * x1 in
let { y : Nat; y := x3 + 2 } in let y : Nat; y := x3 + 2 in
let { z : Nat; z := x3 + y } in let z : Nat; z := x3 + y in
printNatLn (x + y + z); printNatLn (x + y + z);
end; end;

View File

@ -7,7 +7,7 @@ open import Stdlib.Data.Nat.Ord;
terminating terminating
f : Nat → Nat → Nat; f : Nat → Nat → Nat;
f x := f x :=
let {g : Nat → Nat; g y := x + y} in let g : Nat → Nat; g y := x + y in
if (x == 0) if (x == 0)
(f 10) (f 10)
(if (x < 10) (if (x < 10)

View File

@ -16,12 +16,12 @@ terminating
f : Tree → Nat; f : Tree → Nat;
f leaf := 1; f leaf := 1;
f (node l r) := f (node l r) :=
let {l : Tree; l := g l} in let l : Tree; l := g l in
let {r : Tree; r := g r} 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 a : Nat; a := λ {leaf := 1 | (node l r) := f l + f r} l in
let {terminating b : Nat; let terminating b : Nat;
b := λ {(node l r) := f l + f r b := λ {(node l r) := f l + f r
| _ := 2} r} in | _ := 2} r in
a * b; a * b;
isNode : Tree → Bool; isNode : Tree → Bool;

View File

@ -16,7 +16,7 @@ front q := head (qfst q);
pop_front : {A : Type} → Queue A → Queue A; pop_front : {A : Type} → Queue A → Queue A;
pop_front {A} q := 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') λ{ case (qfst q') λ{
nil := queue (reverse (qsnd q')) nil nil := queue (reverse (qsnd q')) nil
| _ := q' | _ := q'

View File

@ -20,7 +20,7 @@ merge' xs@(x :: xs') ys@(y :: ys') := if (x <= y) (x :: merge' xs' ys) (y :: mer
terminating terminating
sort : List Nat → List Nat; sort : List Nat → List Nat;
sort xs := sort xs :=
let {n : Nat; n := length xs} in let n : Nat; n := length xs in
if (n <= 1) if (n <= 1)
xs xs
(case (split (div n 2) xs) λ{ (case (split (div n 2) xs) λ{

View File

@ -4,16 +4,16 @@ module test034;
open import Stdlib.Data.Nat.Ord; open import Stdlib.Data.Nat.Ord;
sum : Nat → Nat; sum : Nat → Nat;
sum := let { sum := let
sum' : Nat → Nat; sum' : Nat → Nat;
sum' := λ { sum' := λ {
zero := zero zero := zero
| (suc n) := suc n + sum' n | (suc n) := suc n + sum' n
}; };
} in sum'; in sum';
mutrec : IO; mutrec : IO;
mutrec := let { mutrec := let
terminating terminating
f : Nat → Nat; f : Nat → Nat;
terminating terminating
@ -23,7 +23,7 @@ module test034;
f x := if (x < 1) 1 (g (sub x 1) + 2 * x); f x := if (x < 1) 1 (g (sub x 1) + 2 * x);
g x := if (x < 1) 1 (x + h (sub x 1)); g x := if (x < 1) 1 (x + h (sub x 1));
h x := if (x < 1) 1 (x * f (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 (f 10)
>> printNatLn (g 5) >> printNatLn (g 5)
>> printNatLn (h 5); >> printNatLn (h 5);

View File

@ -8,13 +8,13 @@ module test037;
(x :: _) := x (x :: _) := x
| nil := id | nil := id
} }
(let { (let
y : Nat → Nat; y : Nat → Nat;
y := id; y := id;
} in (let { in (let
z : (Nat → Nat) → Nat → Nat; z : (Nat → Nat) → Nat → Nat;
z := id; z := id;
} in case l λ { (_ :: _) := id } z) in case l λ { (_ :: _) := id } z)
y) y)
7; 7;

View File

@ -1,6 +1,6 @@
module LetMissingClause; module LetMissingClause;
id : {A : Type} → A → A; id : {A : Type} → A → A;
id {A} := let { id {A} := let
id' : A → A; id' : A → A;
} in id'; in id';
end; end;

View File

@ -6,10 +6,10 @@ module SignatureWithBody;
| _ := false | _ := false
}; };
isNull' : {A : Type} → List A → Bool := let { isNull' : {A : Type} → List A → Bool := let
aux : {A : Type} → List A → Bool := λ { aux : {A : Type} → List A → Bool := λ {
| nil := true | nil := true
| _ := false | _ := false
}; };
} in aux; in aux;
end; end;

View File

@ -12,21 +12,21 @@ match : {A : Type} → {B : Type} → A → (A → B) → B;
match x f := f x; match x f := f x;
foldr : (a : Type) → (b : Type) → (a → b → b) → b → List a → b; foldr : (a : Type) → (b : Type) → (a → b → b) → b → List a → b;
foldr _ _ _ z (nil _) := z; foldr _ _ _ z nil := z;
foldr a b f z (:: _ h hs) := f h (foldr a b f z hs); 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 : Type) → (b : Type) → (b → a → b) → b → List a → b;
foldl a b f z (nil _) := z ; 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 (:: h hs) := foldl a b f (f z h) hs;
map : (a : Type) → (b : Type) → (a → b) → List a → List b; map : (a : Type) → (b : Type) → (a → b) → List a → List b;
map _ b f (nil _) := nil b; map _ b f nil := nil;
map a b f (:: _ h hs) := :: a (f h) (map a b f hs); map a b f (:: h hs) := :: (f h) (map a b f hs);
filter : (a : Type) → (a → Bool) → List a → List a; filter : (a : Type) → (a → Bool) → List a → List a;
filter a f (nil _) := nil a; filter a f nil := nil;
filter a f (:: _ h hs) := match (f h) λ { filter a f (:: h hs) := match (f h) λ {
| true := :: a h (filter a f hs) | true := :: h (filter a f hs)
| false := filter a f hs | false := filter a f hs
}; };
@ -34,24 +34,24 @@ import Data.Nat;
open Data.Nat; open Data.Nat;
length : (a : Type) → List a → ; length : (a : Type) → List a → ;
length _ (nil _) := zero; length _ nil := zero;
length a (:: _ _ l) := suc (length a l); length a (:: _ l) := suc (length a l);
reverse : (a : Type) → List a → List a; reverse : (a : Type) → List a → List a;
reverse a l := reverse a l :=
let { let
rev : List a → List a → List a; rev : List a → List a → List a;
rev (nil _) a := a; rev nil a := a;
rev (:: _ x xs) a := rev xs (:: a x a) rev (:: x xs) a := rev xs (:: x a)
} in rev l (nil a); in rev l nil;
replicate : (a : Type) → → a → List a; replicate : (a : Type) → → a → List a;
replicate a zero _ := nil a; replicate a zero _ := nil;
replicate a (suc n) x := :: a x (replicate a n x); replicate a (suc n) x := :: x (replicate a n x);
take : (a : Type) → → List a → List a; take : (a : Type) → → List a → List a;
take a (suc n) (:: _ x xs) := :: a x (take a n xs); take a (suc n) (:: x xs) := :: x (take a n xs);
take a _ _ := nil a; take a _ _ := nil;
import Data.Ord; import Data.Ord;
open Data.Ord; open Data.Ord;
@ -59,32 +59,32 @@ open Data.Ord;
import Data.Product; import Data.Product;
open Data.Product; open Data.Product;
splitAt : (a : Type) → → List a → List a; splitAt : (a : Type) → → List a → List a × List a;
splitAt a _ (nil _) := nil a, nil a; splitAt a _ nil := , nil nil ;
splitAt a zero xs := , (List a) (List a) (nil a) xs; splitAt a zero xs := , nil xs;
splitAt a (suc zero) (:: _ x xs) := , (List a) (List a) (:: a x (nil a)) xs; splitAt a (suc zero) (:: x xs) := , (:: x nil) xs;
splitAt a (suc (suc m)) (:: _ x xs) := match (splitAt a m xs) λ { splitAt a (suc (suc m)) (:: x xs) := match (splitAt a m xs) λ {
(, la _ xs' xs'') := , la la (:: a x xs') xs'' (, xs' xs'') := , (:: x xs') xs''
}; };
merge : (a : Type) → (a → a → Ordering) → List a → List a → List a; terminating merge : (a : Type) → (a → a → Ordering) → List a → List a → List a;
merge a cmp (:: _ x xs) (:: _ y ys) := match (cmp x y) λ { merge a cmp (:: x xs) (:: y ys) := match (cmp x y) λ {
| LT := :: a x (merge a cmp xs (:: a y ys)) | LT := :: x (merge a cmp xs (:: y ys))
| _ := :: a y (merge a cmp (:: a x xs) ys) | _ := :: y (merge a cmp (:: x xs) ys)
}; };
merge _ _ (nil _) ys := ys; merge _ _ nil ys := ys;
merge _ _ xs (nil _) := xs; merge _ _ xs nil := xs;
-- infixr 5 ++; waiting for implicit arguments -- infixr 5 ++; waiting for implicit arguments
++ : (a : Type) → List a → List a → List a; ++ : (a : Type) → List a → List a → List a;
++ a (nil _) ys := ys; ++ a nil ys := ys;
++ a (:: _ x xs) ys := :: a x (++ a xs ys); ++ a (:: x xs) ys := :: x (++ a xs ys);
quickSort : (a : Type) → (a → a → Ordering) → List a → List a; terminating quickSort : (a : Type) → (a → a → Ordering) → List a → List a;
quickSort a _ (nil _) := nil a; quickSort a _ nil := nil;
quickSort a _ (:: _ x (nil _)) := :: a x (nil a); quickSort a _ (:: x nil) := :: x nil;
quickSort a cmp (:: _ x ys) := quickSort a cmp (:: x ys) :=
let { let
ltx : a → Bool; ltx : a → Bool;
ltx y := match (cmp y x) λ{ ltx y := match (cmp y x) λ{
| LT := true | LT := true
@ -92,10 +92,8 @@ quickSort a cmp (:: _ x ys) :=
}; };
gex : a → Bool; gex : a → Bool;
gex y := not (ltx y) gex y := not (ltx y)
} in in
{ ++ a (quickSort a cmp (filter a ltx ys))
++ a (quickSort a (filter a ltx) ys) (++ a (:: x nil) (quickSort a cmp (filter a gex ys)));
(++ a (:: a x (nil a)) (quickSort a (filter a gex) ys))
};
end; end;

View File

@ -122,7 +122,7 @@ tests:
command: command:
- juvix - juvix
- repl - repl
stdin: "let {x : Nat; x := 2 + 1} in x" stdin: "let x : Nat; x := 2 + 1 in x"
stdout: stdout:
contains: contains:
"suc (suc (suc zero))" "suc (suc (suc zero))"