After more experimenting with GUI in SML, I would like to now try calling an SML module from a C program. That way, the GUI could be built using traditional imperative techniques and the program logic could be written in SML. I see how to call a C function from SML, but and not so clear on how to call an SML function from a C program. I want to build code using polyc and then link with a C main program, but could anyone show me a simple example of calling that function and getting its return value? for example, I have defined:
fun sum i n f = if i > n then 0.0 else f i + sum (i+1) n f ; fun int2real x = real x ;
in linkToC.sml
and
#include <iostream> #include <fstream> std::ofstream ofs; int main() { std::cout << sum(1,10,int2real) << std::endl; ofs.open("table.dat"); for(int i = 0; i < 20; i++) ofs << i << '\t' << sum(1,i,int2real) << std::endl; }
in linkToC.cpp
Looking in the polyc script, I thought adding the -c option might allow me to build the object code which I could then link to the sml functions, but it complains about not having a main.
Could someone point me in the right direction? Thanks, Dave
On Mon, Dec 15, 2014 at 11:44 AM, David Topham <dtopham at gmail.com> wrote:
David, Thank you so much! I really appreciate your help with this. Your sample below builds fine with polyc on my system (PolyML 5.5.2). I am using a minimal Linux distribution named TinyCore ( http://distro.ibiblio.org/tinycorelinux/) which comes with OpenMotif 2.3.3 It runs in a virtual machine within another computer only taking up a few hundred MBs!
I am going to use it to help my students explore Discrete Math next semester (this textbook uses SML for that purpose: http://cs.wheaton.edu/~tvandrun/dmfp/).
I also found a reasonably current project using PolyML and Motif here: http://www.lemma-one.com/ProofPower/index/
...however it was built on PolyML 4 and has not yet been ported to the newer version (e.g. depends on PolyML.commit which no longer seems to be supported).
Happy Holidays to everyone in the PolyML community!
-David Topham
Re: GUI Interface (David Matthews)
I've experimented with building a stand-alone executable with Motif and succeeded in getting it to work for me using polyc. (It required building Poly/ML with --with-x). I took the example from http://www.polyml.org/docs/Motif.html and wrapped it up in a function. I did find a problem, though. It looks as though the function has to suspend itself with something like Posix.Process.pause otherwise nothing happens. I seem to recall that the Motif stuff is handled on a separate thread to allow the REPL to continue to accept commands. With a stand-alone application there isn't a REPL so without the "pause" it terminates immediately.
David
open XWindows ; open Motif ;
fun main() = let val shell = XtAppInitialise "" "xed" "Editor" [] [XmNwidth 400, XmNheight 400] ;
val main = XmCreateMainWindow shell "main" [] ;
val bar = XmCreateMenuBar main "bar" [] ;
val fileMenu = XmCreateCascadeButton bar "file" [XmNlabelString "File"] ; val editMenu = XmCreateCascadeButton bar "edit" [XmNlabelString "Edit"] ; val viewMenu = XmCreateCascadeButton bar "view" [XmNlabelString "View"] ; val helpMenu = XmCreateCascadeButton bar "help" [XmNlabelString "Help"] ;
val command = XmCreateText main "command" [XmNeditMode XmSINGLE_LINE_EDIT] ;
val hscroll = XmCreateScrollBar main "hscroll" [XmNorientation XmHORIZONTAL] ; val vscroll = XmCreateScrollBar main "vscroll" [XmNorientation XmVERTICAL] ;
val work = XmCreateDrawingArea main "work" [] ; in
XtManageChildren [fileMenu, editMenu, viewMenu, helpMenu] ; XtManageChildren [bar, command, hscroll, vscroll, work] ;
XmMainWindowSetAreas main bar command hscroll vscroll work ;
XtManageChild main ; XtRealizeWidget shell; Posix.Process.pause() end;
polyml mailing list polyml at inf.ed.ac.uk http://lists.inf.ed.ac.uk/mailman/listinfo/polyml
End of polyml Digest, Vol 110, Issue 12
-- The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336.
David, This isn't possible. There's no way to build a library from SML using Poly/ML and link it with a main program written in some other language. It is possible to do the reverse, to load a shared library written in, say C, and use that from ML but you need to use the CInterface (FFI) structure.
David
On 15/01/2015 23:54, David Topham wrote:
After more experimenting with GUI in SML, I would like to now try calling an SML module from a C program. That way, the GUI could be built using traditional imperative techniques and the program logic could be written in SML. I see how to call a C function from SML, but and not so clear on how to call an SML function from a C program. I want to build code using polyc and then link with a C main program, but could anyone show me a simple example of calling that function and getting its return value? for example, I have defined:
fun sum i n f = if i > n then 0.0 else f i + sum (i+1) n f ; fun int2real x = real x ;
in linkToC.sml
and
#include <iostream> #include <fstream> std::ofstream ofs; int main() { std::cout << sum(1,10,int2real) << std::endl; ofs.open("table.dat"); for(int i = 0; i < 20; i++) ofs << i << '\t' << sum(1,i,int2real) << std::endl; }
in linkToC.cpp
Looking in the polyc script, I thought adding the -c option might allow me to build the object code which I could then link to the sml functions, but it complains about not having a main.
Could someone point me in the right direction? Thanks, Dave
On Mon, Dec 15, 2014 at 11:44 AM, David Topham <dtopham at gmail.com> wrote:
David, Thank you so much! I really appreciate your help with this. Your sample below builds fine with polyc on my system (PolyML 5.5.2). I am using a minimal Linux distribution named TinyCore ( http://distro.ibiblio.org/tinycorelinux/) which comes with OpenMotif 2.3.3 It runs in a virtual machine within another computer only taking up a few hundred MBs!
I am going to use it to help my students explore Discrete Math next semester (this textbook uses SML for that purpose: http://cs.wheaton.edu/~tvandrun/dmfp/).
I also found a reasonably current project using PolyML and Motif here: http://www.lemma-one.com/ProofPower/index/
...however it was built on PolyML 4 and has not yet been ported to the newer version (e.g. depends on PolyML.commit which no longer seems to be supported).
Happy Holidays to everyone in the PolyML community!
-David Topham
Re: GUI Interface (David Matthews)
I've experimented with building a stand-alone executable with Motif and succeeded in getting it to work for me using polyc. (It required building Poly/ML with --with-x). I took the example from http://www.polyml.org/docs/Motif.html and wrapped it up in a function. I did find a problem, though. It looks as though the function has to suspend itself with something like Posix.Process.pause otherwise nothing happens. I seem to recall that the Motif stuff is handled on a separate thread to allow the REPL to continue to accept commands. With a stand-alone application there isn't a REPL so without the "pause" it terminates immediately.
David
open XWindows ; open Motif ;
fun main() = let val shell = XtAppInitialise "" "xed" "Editor" [] [XmNwidth 400, XmNheight 400] ;
val main = XmCreateMainWindow shell "main" [] ;
val bar = XmCreateMenuBar main "bar" [] ;
val fileMenu = XmCreateCascadeButton bar "file" [XmNlabelString "File"] ; val editMenu = XmCreateCascadeButton bar "edit" [XmNlabelString "Edit"] ; val viewMenu = XmCreateCascadeButton bar "view" [XmNlabelString "View"] ; val helpMenu = XmCreateCascadeButton bar "help" [XmNlabelString "Help"] ;
val command = XmCreateText main "command" [XmNeditMode XmSINGLE_LINE_EDIT] ;
val hscroll = XmCreateScrollBar main "hscroll" [XmNorientation XmHORIZONTAL] ; val vscroll = XmCreateScrollBar main "vscroll" [XmNorientation XmVERTICAL] ;
val work = XmCreateDrawingArea main "work" [] ; in
XtManageChildren [fileMenu, editMenu, viewMenu, helpMenu] ; XtManageChildren [bar, command, hscroll, vscroll, work] ;
XmMainWindowSetAreas main bar command hscroll vscroll work ;
XtManageChild main ; XtRealizeWidget shell; Posix.Process.pause() end;
polyml mailing list polyml at inf.ed.ac.uk http://lists.inf.ed.ac.uk/mailman/listinfo/polyml
End of polyml Digest, Vol 110, Issue 12
-- The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336.
polyml mailing list polyml at inf.ed.ac.uk http://lists.inf.ed.ac.uk/mailman/listinfo/polyml
I am trying to use the debugger in PolyML but always get errors. Here is my code:
PolyML.Compiler.debug := true; open PolyML.Debug; fun member(x,[]) = false | member(x,(y::ys)) = if x = y then true else member(x,(ys)); member(2,[2,3,4]); (* expect true *) member(5,[2,3,4]); (* expect false *) fun subset([],s) = true | subset((x::xs),s) = member(x,s) andalso subset(xs,s); fun equals(s1,s2) = subset(s1,s2) andalso subset(s2,s1); breakIn "subset";
subset([],[1,2,3]); (* expect true *) subset([2],[1,2,3]); (* expect true *)
It does break at the debug prompt, but when typing
dump();
it crashes with: Function subset:Exception- Cast "toAddress" raised
Do you have any suggestions?
I am using Debian 9 64-bit and installed using apt-get
"polyml is already the newest version (5.6-8)"
-Dave
On Thu, Jan 15, 2015 at 3:54 PM David Topham <dtopham at gmail.com> wrote:
After more experimenting with GUI in SML, I would like to now try calling an SML module from a C program. That way, the GUI could be built using traditional imperative techniques and the program logic could be written in SML. I see how to call a C function from SML, but and not so clear on how to call an SML function from a C program. I want to build code using polyc and then link with a C main program, but could anyone show me a simple example of calling that function and getting its return value? for example, I have defined:
fun sum i n f = if i > n then 0.0 else f i + sum (i+1) n f ; fun int2real x = real x ;
in linkToC.sml
and
#include <iostream> #include <fstream> std::ofstream ofs; int main() { std::cout << sum(1,10,int2real) << std::endl; ofs.open("table.dat"); for(int i = 0; i < 20; i++) ofs << i << '\t' << sum(1,i,int2real) << std::endl; }
in linkToC.cpp
Looking in the polyc script, I thought adding the -c option might allow me to build the object code which I could then link to the sml functions, but it complains about not having a main.
Could someone point me in the right direction? Thanks, Dave
On Mon, Dec 15, 2014 at 11:44 AM, David Topham <dtopham at gmail.com> wrote:
David, Thank you so much! I really appreciate your help with this. Your sample below builds fine with polyc on my system (PolyML 5.5.2). I am using a minimal Linux distribution named TinyCore ( http://distro.ibiblio.org/tinycorelinux/) which comes with OpenMotif 2.3.3 It runs in a virtual machine within another computer only taking up a few hundred MBs!
I am going to use it to help my students explore Discrete Math next semester (this textbook uses SML for that purpose: http://cs.wheaton.edu/~tvandrun/dmfp/).
I also found a reasonably current project using PolyML and Motif here: http://www.lemma-one.com/ProofPower/index/
...however it was built on PolyML 4 and has not yet been ported to the newer version (e.g. depends on PolyML.commit which no longer seems to be supported).
Happy Holidays to everyone in the PolyML community!
-David Topham
Re: GUI Interface (David Matthews)
I've experimented with building a stand-alone executable with Motif and succeeded in getting it to work for me using polyc. (It required building Poly/ML with --with-x). I took the example from http://www.polyml.org/docs/Motif.html and wrapped it up in a function. I did find a problem, though. It looks as though the function has to suspend itself with something like Posix.Process.pause otherwise nothing happens. I seem to recall that the Motif stuff is handled on a separate thread to allow the REPL to continue to accept commands. With a stand-alone application there isn't a REPL so without the "pause" it terminates immediately.
David
open XWindows ; open Motif ;
fun main() = let val shell = XtAppInitialise "" "xed" "Editor" [] [XmNwidth 400, XmNheight 400] ;
val main = XmCreateMainWindow shell "main" [] ;
val bar = XmCreateMenuBar main "bar" [] ;
val fileMenu = XmCreateCascadeButton bar "file" [XmNlabelString "File"] ; val editMenu = XmCreateCascadeButton bar "edit" [XmNlabelString "Edit"] ; val viewMenu = XmCreateCascadeButton bar "view" [XmNlabelString "View"] ; val helpMenu = XmCreateCascadeButton bar "help" [XmNlabelString "Help"] ;
val command = XmCreateText main "command" [XmNeditMode XmSINGLE_LINE_EDIT] ;
val hscroll = XmCreateScrollBar main "hscroll" [XmNorientation XmHORIZONTAL] ; val vscroll = XmCreateScrollBar main "vscroll" [XmNorientation XmVERTICAL] ;
val work = XmCreateDrawingArea main "work" [] ; in
XtManageChildren [fileMenu, editMenu, viewMenu, helpMenu] ; XtManageChildren [bar, command, hscroll, vscroll, work] ;
XmMainWindowSetAreas main bar command hscroll vscroll work ;
XtManageChild main ; XtRealizeWidget shell; Posix.Process.pause() end;
polyml mailing list polyml at inf.ed.ac.uk http://lists.inf.ed.ac.uk/mailman/listinfo/polyml
End of polyml Digest, Vol 110, Issue 12
-- The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336.
Dave, It seems to be a bug with printing the value of a polymorphic equality type variable. "s" has type ''a and that upsets the debugger. It works either if there is a type constraint to force the type of "s" to be int list or if "member" is changed so that the equality function is passed as an argument and the functions are fully polymorphic. I need to find out why equality types cause problems.
David
On 09/11/2018 05:29, David Topham wrote:
I am trying to use the debugger in PolyML but always get errors. Here is my code:
PolyML.Compiler.debug := true; open PolyML.Debug; fun member(x,[]) = false | member(x,(y::ys)) = if x = y then true else member(x,(ys)); member(2,[2,3,4]); (* expect true *) member(5,[2,3,4]); (* expect false *) fun subset([],s) = true | subset((x::xs),s) = member(x,s) andalso subset(xs,s); fun equals(s1,s2) = subset(s1,s2) andalso subset(s2,s1); breakIn "subset";
subset([],[1,2,3]); (* expect true *) subset([2],[1,2,3]); (* expect true *)
It does break at the debug prompt, but when typing
dump();
it crashes with: Function subset:Exception- Cast "toAddress" raised
Do you have any suggestions?
I am using Debian 9 64-bit and installed using apt-get
"polyml is already the newest version (5.6-8)"
-Dave