Thread View: comp.lang.functional
9 messages
9 total messages
Started by Jon Freeman
Tue, 06 Oct 1992 16:20
SML Pretty-printing
Author: Jon Freeman
Date: Tue, 06 Oct 1992 16:20
Date: Tue, 06 Oct 1992 16:20
34 lines
1480 bytes
1480 bytes
Regarding SML, kff@dentex.tds.kth.se (Karl-Filip Faxen) says:
>|> and it seems to be difficult to indent automatically as well.
>
>In an Emacs mode, perhaps. In a pretty printer, certainly not (I've
>written one).
>
>Cheers,
> Karl-Filip
I never even thought that writing a pretty-printer for SML was a
feasible undertaking. Consider the following program:
--------------------------------------------------------------
datatype foo = Alpha of int | Beta of real | Gamma of string;
datatype bar = Happy of int | Dopey of real | Grumpy of string;
fun testcase (x,y) =
case x of
Alpha _ => ()
| Beta _ =>
case y of
Happy _ => ()
| Dopey _ => ()
| Grumpy _ => ()
| Gamma _ => (); (* Hmmm... *)
--------------------------------------------------------------
Any pretty-printer that can correctly indent the last line of testcase
has to be able to discriminate between the constructors of foo and the
constructors of bar!! Does your pretty-printer really do this? If
so, I'm very impressed.
Jon
Re: SML Pretty-printing
Author: George Beshers
Date: Tue, 06 Oct 1992 19:21
Date: Tue, 06 Oct 1992 19:21
27 lines
1335 bytes
1335 bytes
# datatype foo = Alpha of int | Beta of real | Gamma of string; # datatype bar = Happy of int | Dopey of real | Grumpy of string; # # fun testcase (x,y) = # case x of # Alpha _ => () # | Beta _ => # case y of # Happy _ => () # | Dopey _ => () # | Grumpy _ => () # | Gamma _ => (); (* Hmmm... *) # Any SML compiler which accepts this is wrong, i.e., as I read the rules an error message should be generated (sorry I can't quote the rule numbers as the definition is at home). However, having made the mistake, I agree that this bit of SML syntax is a mis-feature. In general, if a syntax is designed for small examples then closing brackets can `be safely omitted'. When the examples start to get large the `safely' is invalidated. The time spent (and the in elegance of) esac, fi, nf, etc. is negligible compared with problems of mis-interpretation.
Re: SML Pretty-printing
Author: Andrew Wright
Date: Tue, 06 Oct 1992 19:44
Date: Tue, 06 Oct 1992 19:44
43 lines
2053 bytes
2053 bytes
In article <91849@netnews.upenn.edu> freeman@linc.cis.upenn.edu (Jon Freeman) writes:
>I never even thought that writing a pretty-printer for SML was a
>feasible undertaking. Consider the following program:
>
>--------------------------------------------------------------
>datatype foo = Alpha of int | Beta of real | Gamma of string;
>datatype bar = Happy of int | Dopey of real | Grumpy of string;
>
>fun testcase (x,y) =
> case x of
> Alpha _ => ()
> | Beta _ =>
> case y of
> Happy _ => ()
> | Dopey _ => ()
> | Grumpy _ => ()
> | Gamma _ => (); (* Hmmm... *)
>--------------------------------------------------------------
This example is bogus. The last line belongs to the case statment,
not the function (hence this program is not type correct). Your
naive pretty printer would fix the misleading indentation.
Good pretty printing simply involves building a _full_ parser for the
language, choosing a canonical format for expressions, and rules for
line breaking.
The problem is that my choice of canonical format may not be yours.
ML suffers from this to a greater degree than Lisp. But note that
Lisp/Scheme also suffers from this problem. My scheme mode indents as:
(and one-long-name second-long-name third-long-name fourth-long-name
fifth-long-name)
but (sometimes) I would rather have:
(and one-long-name second-long-name third-long-name fourth-long-name
fifth-long-name)
In fact, the scheme mode has a list of special names and a rule about
length of names to choose between the two ways.
Andrew Wright
Re: SML Pretty-printing
Author: Brian Boutel
Date: Tue, 06 Oct 1992 20:49
Date: Tue, 06 Oct 1992 20:49
38 lines
1880 bytes
1880 bytes
In article <91849@netnews.upenn.edu>, freeman@linc.cis.upenn.edu (Jon Freeman) writes:
|>
|> I never even thought that writing a pretty-printer for SML was a
|> feasible undertaking. Consider the following program:
|>
|> --------------------------------------------------------------
|> datatype foo = Alpha of int | Beta of real | Gamma of string;
|> datatype bar = Happy of int | Dopey of real | Grumpy of string;
|>
|> fun testcase (x,y) =
|> case x of
|> Alpha _ => ()
|> | Beta _ =>
|> case y of
|> Happy _ => ()
|> | Dopey _ => ()
|> | Grumpy _ => ()
|> | Gamma _ => (); (* Hmmm... *)
|> --------------------------------------------------------------
|>
|> Any pretty-printer that can correctly indent the last line of testcase
|> has to be able to discriminate between the constructors of foo and the
|> constructors of bar!! Does your pretty-printer really do this? If
|> so, I'm very impressed.
That would be clever, but doesn't the definition say that matches extend as far
to the right as possible? In this example, the last line would be parsed as part
of the inner case expression, and the program contains a type error.
--brian
--
Internet: brian@comp.vuw.ac.nz
Postal: Brian Boutel, Computer Science Dept, Victoria University of Wellington,
PO Box 600, Wellington, New Zealand
Phone: +64 4 471-5328 Fax: +64 4 495-5232
Re: SML Pretty-printing
Author: Bennet Vance
Date: Tue, 06 Oct 1992 22:13
Date: Tue, 06 Oct 1992 22:13
31 lines
1740 bytes
1740 bytes
In article <91849@netnews.upenn.edu> freeman@linc.cis.upenn.edu (Jon Freeman) writes: ) )I never even thought that writing a pretty-printer for SML was a )feasible undertaking. Consider the following program: ) )-------------------------------------------------------------- )datatype foo = Alpha of int | Beta of real | Gamma of string; )datatype bar = Happy of int | Dopey of real | Grumpy of string; ) )fun testcase (x,y) = ) case x of ) Alpha _ => () ) | Beta _ => ) case y of ) Happy _ => () ) | Dopey _ => () ) | Grumpy _ => () ) | Gamma _ => (); (* Hmmm... *) )-------------------------------------------------------------- ) )Any pretty-printer that can correctly indent the last line of testcase )has to be able to discriminate between the constructors of foo and the )constructors of bar!! Does your pretty-printer really do this? If )so, I'm very impressed. By the same token (so to speak), any parser for SML would also have to be able to discriminate between the constructors of foo and those of bar (at parse time!) if it were to parse this program as the author intends. The given program is rejected by the SML/NJ compiler... Bennet Vance
Re: SML Pretty-printing
Author: Andrew Koenig
Date: Tue, 06 Oct 1992 22:15
Date: Tue, 06 Oct 1992 22:15
37 lines
1506 bytes
1506 bytes
In article <91849@netnews.upenn.edu> freeman@linc.cis.upenn.edu (Jon Freeman) writes: > I never even thought that writing a pretty-printer for SML was a > feasible undertaking. Consider the following program: > datatype foo = Alpha of int | Beta of real | Gamma of string; > datatype bar = Happy of int | Dopey of real | Grumpy of string; > fun testcase (x,y) = > case x of > Alpha _ => () > | Beta _ => > case y of > Happy _ => () > | Dopey _ => () > | Grumpy _ => () > | Gamma _ => (); (* Hmmm... *) No problem -- the program is a syntax error. In the absence of other delimeters, a `case' eats all the clauses it can, so in fact this program should have been indented this way: fun testcase (x,y) = case x of Alpha _ => () | Beta _ => case y of Happy _ => () | Dopey _ => () | Grumpy _ => () | Gamma _ => (); to correspond to the way it's parsed. That in turn generates a type error. -- --Andrew Koenig ark@europa.att.com
Re: SML Pretty-printing
Author: Karl-Filip Faxen
Date: Wed, 07 Oct 1992 11:23
Date: Wed, 07 Oct 1992 11:23
53 lines
2672 bytes
2672 bytes
In article <91849@netnews.upenn.edu>, freeman@linc.cis.upenn.edu (Jon Freeman) writes:
|> Regarding SML, kff@dentex.tds.kth.se (Karl-Filip Faxen) says:
|>
|> >|> and it seems to be difficult to indent automatically as well.
|> >
|> >In an Emacs mode, perhaps. In a pretty printer, certainly not (I've
|> >written one).
|> >
|> >Cheers,
|> > Karl-Filip
|>
|> I never even thought that writing a pretty-printer for SML was a
|> feasible undertaking. Consider the following program:
OOPS! I was comparing pretty-printing curried application and
applications involving tuples. I didn't say I'd written a
pretty-printer for SML (I haven't; it's for a subset of Haskell).
|> --------------------------------------------------------------
|> datatype foo = Alpha of int | Beta of real | Gamma of string;
|> datatype bar = Happy of int | Dopey of real | Grumpy of string;
|>
|> fun testcase (x,y) =
|> case x of
|> Alpha _ => ()
|> | Beta _ =>
|> case y of
|> Happy _ => ()
|> | Dopey _ => ()
|> | Grumpy _ => ()
|> | Gamma _ => (); (* Hmmm... *)
|> --------------------------------------------------------------
|>
|> Any pretty-printer that can correctly indent the last line of testcase
|> has to be able to discriminate between the constructors of foo and the
|> constructors of bar!! Does your pretty-printer really do this? If
|> so, I'm very impressed.
This isn't a problem of pretty-printing, it's a problem of parsing!
Actually, for your example to be syntactically correct, you have
to have either a layout dependent syntax or (which I understand you
to mean) a context dependent syntax. That would be really wierd for
a modern programming language! Given, however, that the parsing
problem is solved and I have an abstract syntax tree, it wouldn't
be _that_ difficult to produce this layout (it wouldn't be difficult
at all if you produced something that was parsable with a context-
independent parser).
Cheers,
Karl-Filip
------------------------------------------------------------
Make the frequent case fast, and the fast case frequent!
Re: SML Pretty-printing
Author: Thomas M. Breuel
Date: Wed, 07 Oct 1992 22:31
Date: Wed, 07 Oct 1992 22:31
24 lines
1224 bytes
1224 bytes
In article <91849@netnews.upenn.edu> freeman@linc.cis.upenn.edu (Jon Freeman) writes: I never even thought that writing a pretty-printer for SML was a feasible undertaking. Consider the following program: [...] Well, Jon's example didn't work, but what about the following: the interpretation of a curried call like f 3 foo 4 depends on whether "foo" is an "infix" operator (e.g., "before") or not. This may perhaps not affect indentation, but it may trip up other tools. Thomas. PS: the "problem" goes away if you require all arguments to be parenthesized, like other languages do (or, at least, adopt this as a convention): f(3)(foo)(4) is unambiguous.
Re: SML Pretty-printing
Author: Stephen Adams
Date: Thu, 08 Oct 1992 08:05
Date: Thu, 08 Oct 1992 08:05
69 lines
2936 bytes
2936 bytes
In article <91849@netnews.upenn.edu> freeman@linc.cis.upenn.edu (Jon Freeman) writes: > Regarding SML, kff@dentex.tds.kth.se (Karl-Filip Faxen) says: > > >|> and it seems to be difficult to indent automatically as well. > > > >In an Emacs mode, perhaps. In a pretty printer, certainly not (I've > >written one). > > > >Cheers, > > Karl-Filip > > I never even thought that writing a pretty-printer for SML was a > feasible undertaking. Consider the following program: > > -------------------------------------------------------------- > datatype foo = Alpha of int | Beta of real | Gamma of string; > datatype bar = Happy of int | Dopey of real | Grumpy of string; > > fun testcase (x,y) = > case x of > Alpha _ => () > | Beta _ => > case y of > Happy _ => () > | Dopey _ => () > | Grumpy _ => () > | Gamma _ => (); (* Hmmm... *) > -------------------------------------------------------------- > > Any pretty-printer that can correctly indent the last line of testcase > has to be able to discriminate between the constructors of foo and the > constructors of bar!! Does your pretty-printer really do this? If > so, I'm very impressed. Is this legal? NJ/SML gives me a type error. Whether the Gamma choice belongs to case x or case y is purely a matter of syntax, and so I would hope that the pretty printer gets it `wrong', otherwise it would be misleading! To get what you want you have to put the case y in parentheses of a `let in ... end' - fun testcase (x,y) = = case x of = Alpha _ => () = | Beta _ => = case y of = Happy _ => () = | Dopey _ => () = | Grumpy _ => () = | Gamma _ => (); std_in:8.7-12.17 Error: rules don't agree (tycon mismatch) expected: bar -> unit found: foo -> unit rule: Gamma _ => () - I find that the Emacs SML mode by Lars Bo Nielsen (lbn@iesd.auc.dk) adequate (I use version 2.5, this might now be antique, the file has creation date 1989). Occasionally it does things that I don't like but I am prepared to correct them manually (or as often just accept them). -- Stephen Adams Email: S.R.Adams@ecs.soton.ac.uk Electronics and Computer Science Tel: 0703 593649 University of Southampton Fax: 0703 593045 Southampton SO9 5NH, UK
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads