mirror of
https://github.com/anoma/juvix.git
synced 2025-01-05 14:34:03 +03:00
Remove braces from let expressions (#1790)
This commit is contained in:
parent
5ec80641cb
commit
3c33916034
@ -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'
|
||||
|
||||
|
@ -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
|
||||
|
@ -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 {..}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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)
|
||||
|
@ -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;
|
||||
|
@ -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'
|
||||
|
@ -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) λ{
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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))"
|
||||
|
Loading…
Reference in New Issue
Block a user