caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* OCaml IDE (Camlp4 for code formatting)
@ 2005-06-20 20:08 Nathaniel J. Gaylinn
  2005-06-21 10:33 ` [Caml-list] " Hendrik Tews
  0 siblings, 1 reply; 7+ messages in thread
From: Nathaniel J. Gaylinn @ 2005-06-20 20:08 UTC (permalink / raw)
  To: caml-list


One feature I want my program to have is autoformatting, that is, the
ability to go through and indent source code for you in some standard way.
At first glance, OCamlp4 seemed like the way to go for this, but under
further inspection I'm not sure this is true.

My main problem with OCamlp4 is that it changes the input source code!
Expressions like "5;;" that are just used for side effect are replaced
with "let _ = 5;;" which is equivalent, but different code. I understand
why it makes this change, but since my program is intended for students
the last thing I want is for it to change their code, even in a trivial
way such as this. Also, I'm pretty sure I've observed OCamlp4 eating
comments, which obviously is no good.

Is there a nice way to get OCamlp4 not to change the source code it's
given in any way except for spacing? Is there some other convenient way to
autoformat OCaml source? Is there a documented standard of what properly
formatted OCaml code should look like? It's hard to derive a set of
indentation rules just from anecdotal code snippets.

On a similar note, is there any reference to the format in which Camlp4
outputs its syntax tree? I might be able to use the syntax tree for
formatting and other purposes in my ide, but I can't at all read the
binary format it spits out. Is this something easy to read, or is it only
meant to be accessed by OCaml itself?

Thanks,

  -- Nate Gaylinn


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] OCaml IDE (Camlp4 for code formatting)
  2005-06-20 20:08 OCaml IDE (Camlp4 for code formatting) Nathaniel J. Gaylinn
@ 2005-06-21 10:33 ` Hendrik Tews
  2005-06-21 13:47   ` Nathaniel J. Gaylinn
  0 siblings, 1 reply; 7+ messages in thread
From: Hendrik Tews @ 2005-06-21 10:33 UTC (permalink / raw)
  To: caml-list

"Nathaniel J. Gaylinn" <ngaylinn@cs.brown.edu> writes:

   One feature I want my program to have is autoformatting, that is, the
   ability to go through and indent source code for you in some standard way.
   At first glance, OCamlp4 seemed like the way to go for this, but under
   further inspection I'm not sure this is true.
   
   My main problem with OCamlp4 is that it changes the input source code!
   Expressions like "5;;" that are just used for side effect are replaced
   with "let _ = 5;;" which is equivalent, but different code. 

If you are sensitive to these kind of changes then you cannot use
camlp4 to format source code. There are many cases where camlp4
uses the same internal representation for different input. For
example 

  let f a b = ...      and      let f = fun a -> fun b ->
  5 + 6                and      (+) 5 6
  module A(B:S) = ...  and      module A = functor(B:S) -> ...
  begin end            and      ()

Further, superfluous parenthesis and comments are not present in
the ast (i.e. (1,2) is the same as 1,2). (Toplevel comments are
preserved if the printer is able to reopen the input file but
they are lost if you feed camlp4 from a pipe.)

>From that it is clear that camlp4's printer must change the input
in many places.

   I understand why it makes this change, 

Could you explain? (Because I don't understand. "let _ = 5" and
"5" are different internally.)

   On a similar note, is there any reference to the format in which Camlp4
   outputs its syntax tree? 

I don't think so. It is defined in the compiler sources and
probably not for the general public. However, you can define your
own camlp4 printers: Write a module that overwrites
Pcaml.print_implem and load this module into camlp4. The function
that you plug into Pcaml.print_implem has to pattern match on
camlp4's ast, which is defined in mLast.mli. You can use the
constructors from mLast.mli or the quotations from q_MLast. See
the source of pr_o or other pretty printers (like in my ocamlp4
package). 

Bye,

Hendrik


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] OCaml IDE (Camlp4 for code formatting)
  2005-06-21 10:33 ` [Caml-list] " Hendrik Tews
@ 2005-06-21 13:47   ` Nathaniel J. Gaylinn
  2005-06-21 15:18     ` Hendrik Tews
  0 siblings, 1 reply; 7+ messages in thread
From: Nathaniel J. Gaylinn @ 2005-06-21 13:47 UTC (permalink / raw)
  To: Hendrik Tews; +Cc: caml-list



On Tue, 21 Jun 2005, Hendrik Tews wrote:

>    I understand why it makes this change,
>
> Could you explain? (Because I don't understand. "let _ = 5" and
> "5" are different internally.)

Heh, I didn't realize that they were handled different internally  ;)  I
thought it was a reasonable thing to do because it is more explicit and
equivalent, but if it isn't actually equivalent...

>
>    On a similar note, is there any reference to the format in which Camlp4
>    outputs its syntax tree?
>
> I don't think so. It is defined in the compiler sources and
> probably not for the general public. However, you can define your
> own camlp4 printers: Write a module that overwrites
> Pcaml.print_implem and load this module into camlp4. The function
> that you plug into Pcaml.print_implem has to pattern match on
> camlp4's ast, which is defined in mLast.mli. You can use the
> constructors from mLast.mli or the quotations from q_MLast. See
> the source of pr_o or other pretty printers (like in my ocamlp4
> package).

Would it be difficult to write a printer that had much direct access with
the original file? That's something that I'd quite obviously need.

In any way, I think I'm going to try to do more research into how Tuareg
does its indentation. It's unfortunately not well documented, but it has
the added benefit of being able to indent one line at a time. I'll take a
look into writing a new printer, but I suspect that that will be the
harder route... correct me if I'm wrong!

  -- Nate


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] OCaml IDE (Camlp4 for code formatting)
  2005-06-21 13:47   ` Nathaniel J. Gaylinn
@ 2005-06-21 15:18     ` Hendrik Tews
  2005-06-21 18:11       ` David Brown
  0 siblings, 1 reply; 7+ messages in thread
From: Hendrik Tews @ 2005-06-21 15:18 UTC (permalink / raw)
  To: caml-list

"Nathaniel J. Gaylinn" <ngaylinn@cs.brown.edu> writes:

   On Tue, 21 Jun 2005, Hendrik Tews wrote:
   
   >    I understand why it makes this change,
   >
   > Could you explain? (Because I don't understand. "let _ = 5" and
   > "5" are different internally.)
   
   Heh, I didn't realize that they were handled different internally  ;)  I
   thought it was a reasonable thing to do because it is more explicit and
   equivalent, but if it isn't actually equivalent...
   
Not sure if you got me right: The two phrases "let _ = 5" and "5"
are semantically equivalent and probably compiled to the same
code. However, they have a different representation as a camlp4
ast. Still the question remains, why camlp4 rewrites one into the
other.

   Would it be difficult to write a printer that had much direct access with
   the original file? That's something that I'd quite obviously need.
   
If you pretty print an camlp4 ast similar to pr_o.cmo then you
will always change the input (ie. rewriting "(1,2)" into "1,2" or
vice versa). I don't think it makes sense to track the input to
recover the information that is not present in camlp4's ast,
because you would have to parse the input.

If you want to output all comments and print "(1,2)" as "(1,2)"
and "1,2" as "1,2" then you need a new parser for ocaml. Camlp4
won't help much in this case. It is probably easier to adjust the
yacc grammar from the ocaml sources.

Bye,

Hendrik


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] OCaml IDE (Camlp4 for code formatting)
  2005-06-21 15:18     ` Hendrik Tews
@ 2005-06-21 18:11       ` David Brown
  2005-06-29 14:54         ` [Caml-list] Keyboard interrupt in Windows Nathaniel J. Gaylinn
  0 siblings, 1 reply; 7+ messages in thread
From: David Brown @ 2005-06-21 18:11 UTC (permalink / raw)
  To: Hendrik Tews, caml-list

On Tue, 21 Jun 2005 08:18:26 -0700, Hendrik Tews  
<tews@tcs.inf.tu-dresden.de> wrote:

> Not sure if you got me right: The two phrases "let _ = 5" and "5"
> are semantically equivalent and probably compiled to the same
> code. However, they have a different representation as a camlp4
> ast. Still the question remains, why camlp4 rewrites one into the
> other.

The phrase "5" has ambituities that aren't present with the "let _ = 5" so  
it makes sense for a canonical representation to use the let form.   
Compare the following:

   let _ = 5
   let _ = 6

vs.

   5
   6

The first is valid, whereas the second causes an error trying to apply '5'.

Dave


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Caml-list] Keyboard interrupt in Windows
  2005-06-21 18:11       ` David Brown
@ 2005-06-29 14:54         ` Nathaniel J. Gaylinn
  2005-06-29 16:13           ` Christopher A. Watford
  0 siblings, 1 reply; 7+ messages in thread
From: Nathaniel J. Gaylinn @ 2005-06-29 14:54 UTC (permalink / raw)
  To: caml-list


In Linux, OCaml uses signals to break out of the current evaluation (when
you press Ctrl+C to cut out of an infinite loop). However, Windows doesn't
support signals. What does OCaml do differently under Windows to make this
work?

  -- Nate Gaylinn


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] Keyboard interrupt in Windows
  2005-06-29 14:54         ` [Caml-list] Keyboard interrupt in Windows Nathaniel J. Gaylinn
@ 2005-06-29 16:13           ` Christopher A. Watford
  0 siblings, 0 replies; 7+ messages in thread
From: Christopher A. Watford @ 2005-06-29 16:13 UTC (permalink / raw)
  To: caml-list

On 6/29/05, Nathaniel J. Gaylinn <ngaylinn@cs.brown.edu> wrote:
> 
> In Linux, OCaml uses signals to break out of the current evaluation (when
> you press Ctrl+C to cut out of an infinite loop). However, Windows doesn't
> support signals. What does OCaml do differently under Windows to make this
> work?
> 
>  -- Nate Gaylinn

Win32 DOES support signals, it just does not send any signal on
CTRL+C. A second thread opens that posts a message to WinMain. The
OCaml Windows IDE has an example of using this to send the interrupt
to the toplevel.

In the following file at the very bottom:
http://dorm.tunkeymicket.com/OCamlWinPlus/Release/src/startocaml.c

// The following sends a CTRL+C/CTRL+BREAK to the console.
GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, pi.dwProcessId)

Note you MUST write to the Win32 Pipe Handle AFTER you call the break,
otherwise your application will have no idea the pipe was interrupted.

As far as how the toplevel itself handles the CTRL_BREAK_EVENT:

// PHANDLER_ROUTINE looks like: BOOL WINAPI HandlerRoutine(DWORD dwCtrlType);
// Add - TRUE to add a handler, FALSE to remove the handler
BOOL SetConsoleCtrlHandler(PHANDLER_ROUTINE HandlerRoutine, BOOL Add);

And the CTRL_*_EVENTs you can handle are:
CTRL_C_EVENT
CTRL_BREAK_EVENT
CTRL_CLOSE_EVENT - [X] clicked
CTRL_LOGOFF_EVENT - user logoff
CTRL_SHUTDOWN_EVENT - machine shutdown or service shutdown

Hope that helps.

-- 
Christopher A. Watford
christopher.watford@gmail.com
http://dorm.tunkeymicket.com


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2005-06-29 16:13 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-06-20 20:08 OCaml IDE (Camlp4 for code formatting) Nathaniel J. Gaylinn
2005-06-21 10:33 ` [Caml-list] " Hendrik Tews
2005-06-21 13:47   ` Nathaniel J. Gaylinn
2005-06-21 15:18     ` Hendrik Tews
2005-06-21 18:11       ` David Brown
2005-06-29 14:54         ` [Caml-list] Keyboard interrupt in Windows Nathaniel J. Gaylinn
2005-06-29 16:13           ` Christopher A. Watford

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).