Syntactic use of curly braces

43 views
Skip to first unread message

August Alm

unread,
Feb 4, 2017, 5:00:32 PM2/4/17
to ats-lang-users
Hi!

This is my first post. I recently started learning ATS2 and have some trouble grokking what is syntactically correct and what isn't when it comes to the use of braces. Apart from simply understanding the logic of the syntax I would also like to give feedback for future design choices. Let me give some examples. Both of these compile:

val () = if (4 < 5) then
           {
              val () = println!("true!");
           }
           else
           {
              val () = println!("dumb!");
           }

and

val () = if (4 < 5) then
           begin
                println!("true!")
           end
           else
           begin
                println!("dumb!")
           end

The latter example with alla "begin" and "end" removed also compiles. However, the following version is incorrect:

val () = if (4 < 5) then
           {
              println!("true!")
           }
           else
           {
              println!("dumb!")
           }

Why is this? The use of curly braces in function bodies is even more constrained. For example,

fun loop
     { n, i: nat | i <= n }
     ( n: int n, i: int i, res: int ) : int
  = if i >= n then
     {
        res
     }
     else loop(n, i+1, (i+1)*res)

does not compile, even with "val"-declarations, but replacing the braces with a "begin ... end" works just fine.

A possibly related question: Why does the "where" keyword only accept braces? E.g, why should

fn fact
     ( n: Nat ) : int
  = loop(n, 0, 1)
where
{
    fun loop
        { n, i: nat | i <= n }
        ( n: int n, i: int i, res: int ) : int
     = if i >= n then res
        else loop(n, i+1, (i+1)*res)
}

be legit and

fn fact
     ( n: Nat ) : int
  = loop(n, 0, 1)
where
    fun loop
         { n, i: nat | i <= n }
         ( n: int n, i: int i, res: int ) : int
      = if i >= n then res
         else loop(n, i+1, (i+1)*res)
end

fail? Especially since the "let .. in ..end" requires an "end". I find it difficult to develop a style that I feel is both consistent and pretty. Only using "begin"-"end" looks coherent but requires avoiding "where" (which is a keyword that I am very fond of). To other alternative, to have more keywords than "where" accept braces, would also be preferable, to my mind.

My two main languages are Scala (similar function definitions, "f(x: a): b", but allows using braces as liberally) and Haskell (no need for braces, similar style to doing everything (including where) with "begin"-"end"). Is there a dirty trick to let me code in a style I like, to either have braces as liberally as I currently have "begin"-"end", or, conversely, to have "where"-"end"? The current mash-up annoys me, which is a shame because I love every feature of what my ATS2 code actually *does*. I apologize for being superficial about the looks.

Best wishes,
August

August Alm

unread,
Feb 4, 2017, 5:27:28 PM2/4/17
to ats-lang-users
I will add a related note. From what I understand the praxis

implement main0 () = {
...
}

is really a short for the more syntactically stringent

implement main0 () = () where {
...
}

Is this short hand syntax only available for functions returning the void type? Is it possible to write one's own template to make it available also for other functions with non-void return type, so that all function bodies could be delimited by braces? 

Hongwei Xi

unread,
Feb 4, 2017, 7:38:20 PM2/4/17
to ats-lan...@googlegroups.com
In ATS, curly braces { ... } are used for grouping declarations:

{
   val x = ...
   fun foo (...) = ...
   abstype xyz = ...
}

Usually, { ... } should follow the keyword 'where'. If used alone as an expression,
{ ... } means:

() where { ... }

Supporting 'where ... end' should be straightforward. I thought about adding it long time
ago. Maybe it is time to do it now :)


--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-users+unsubscribe@googlegroups.com.
To post to this group, send email to ats-lang-users@googlegroups.com.
Visit this group at https://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/bd4640df-a2a3-4ffc-8aad-49ee773474ad%40googlegroups.com.

gmhwxi

unread,
Feb 4, 2017, 8:19:25 PM2/4/17
to ats-lang-users

FYI.

The syntax 'where <declist> end' is now supported.

The changes should move into the next release of ATS2 (ATS2-0.3.2).

Cheers!


On Saturday, February 4, 2017 at 7:38:20 PM UTC-5, gmhwxi wrote:
In ATS, curly braces { ... } are used for grouping declarations:

{
   val x = ...
   fun foo (...) = ...
   abstype xyz = ...
}

Usually, { ... } should follow the keyword 'where'. If used alone as an expression,
{ ... } means:

() where { ... }

Supporting 'where ... end' should be straightforward. I thought about adding it long time
ago. Maybe it is time to do it now :)

gmhwxi

unread,
Feb 4, 2017, 8:23:24 PM2/4/17
to ats-lang-users

August Alm

unread,
Feb 5, 2017, 4:05:14 PM2/5/17
to ats-lang-users
WOW! Now I have to figure out how to seriously prove my loyalty and contribute to the ATS community. =)
Best wishes,
August
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages