Making ml developing environment with emacs
ML언어로 프로그래밍 하기 위한 개발 환경 구축한다.
편집기는 emacs를 사용하고, emacs에 sml-mode를 설치하여 쉽게 컴파일할 수 있는 환경을 만든다.

ML 컴파일러 설치

http://www.smlnj.org/install/index.html 에서 smlnj.exe 다운 가능.

The installation updates the system PATH environment variable to point to the location of the SML/NJ executables, and adds a CM_PATH environment variable to point to the location of the libraries. Because of these updates, it is necessary to reboot the machine after the installation to ensure proper behavior of the compiler (Windows NT/2000 users can logoff instead of rebooting).

이 부분의 환경변수들은 자동으로 등록되는 것 같다. 혹시나 등록되어 있지 않다면 SMLNJ_HOME 이라는 환경변수를 등록시켜 주시기 바란다. 환경변수가 무엇인지, 어떻게 등록하는지 따로 자세히 설명하지 않겠다. 윈도우즈XP를 사용하는 경우 아래 나온대로 환경변수를 등록시킬 수는 있다.

After installing an SML/NJ compiler, you can start top-level interpreter by selecting the short cut “Standard ML of New Jersey 110.0.7” in the program group of the same name, or by running “sml.exe” in [SMLNJ_HOME]/bin on a command line.

  • smlnj.exe실행
  • 환경변수의 PATH에 [SMLNJ_HOME]/bin 추가 (내컴퓨터 오른쪽 클릭->속성->고급탭->환경변수->PATH편집->마지막에 [SMLNJ_HOME]/bin; 을 추가해준다.
  • 이제 cmd창에서 sml을 치면 ml언어 컴파일이 가능.

일반 C/C++/Java를 사용했던 분들이라면 이 부분에서 혼동을 느끼실 것이다. 코드를 모두 써놓고 컴파일하면 실행파일이나 .class파일이 나오는것이 정상인데, ml은 그렇지 않다. 인터프리터언어이기 때문인데, 예시에서와 같이 한줄한줄 써서 그에대한 결과값을 화면에 보여준다. 익숙치 않겠지만, 배우다보면 차차 그 의미를 깨닫게 될 것이다.

  • 실행에서 cmd를 쳐 콘솔창을 연다.
  • sml이라고 치면 ml 인터프리터가 실행된다.
  • val abc = 11; 이라고 친 후 엔터를 누르면,
  • val it = 11 : int 라고 뜨면 설치가 제대로 된 것임.

emacs설치

http://www2.lib.uchicago.edu/~keith//tcl-course/emacs-tutorial.html

  • ntemacs22-bin-20070819.exe를 실행하여 emacs를 설치.
  • 설치폴더의 bin안에 emacs.exe를 실행하여 emacs를 실행.
  • 실행하면 창이 뜬다. 키보드를 누르면 글자가 입력된다.
  • 새 파일 처럼 생긴 툴바의 아이콘을 누르면 find file 대화창이 뜬다. (새로운 파일의 저장경로를 적는것이다. 일반 에디터와 사용방법이 다르다. 대화창 제목인 find file과 기능과의 매치가 잘 안되는 것이 사실이다.)
  • 이 창에서 경로 지정한 후 이름을 쓰면, 그 파일이 없는 경우 그 이름으로 새 파일을 만든다.
  • Ctrl+h 누른후 t를 누르면 튜토리얼로 emacs 사용법을 알아볼 수 있다. (emacs를 처음 사용한다면 한번 쯤은 보는 것이 좋을 듯. emacs를 처음 시작하고 나서 나오는 아래 링크 중 Emacs Tutorial를 눌러도 됩니다.)

emacs에 sml-mode 설치

http://www.smlnj.org/doc/Emacs/sml-mode.html

With luck your system administrator will have installed SML mode somewhere convenient, so it will just magically all work?you can skip the rest of this getting started section. Otherwise you will need to tell Emacs where to find all the SML mode .el files, and when to use them. The where is addressed by locating the Lisp code on your Emacs Lisp load path? you may have to create a directory for this, say /home/mjm/elisp, and then insert the following lines in your /home/mjm/.emacs file:

 (add-to-list ‘load-path “/home/mjm/elisp”)
 (autoload ‘sml-mode “sml-mode” “Major mode for editing SML.” t)
 (autoload ‘run-sml “sml-proc” “Run an inferior SML process.” t)

The first line adjusts Emacs’ internal search path so it can locate the Lisp source you have copied to that directory; the second and third lines tell Emacs to load the code automatically when it is needed. You can then switch any Emacs buffer into SML mode by entering the command

 M-x sml-mode

It is usually more convenient to have Emacs automatically place the buffer in SML mode whenever you visit a file containing ML programs. The simplest way of achieving this is to put something like

 (add-to-list ‘auto-mode-alist ‘(“\\.\\(sml\\|sig\\)\\’” . sml-mode))

also in your .emacs file. Subsequently (after a restart), any files with these extensions will be placed in SML mode buffers when you visit them.

You may want to pre-compile the sml-*.el files (M-x byte-compile-file) for greater speed?byte compiled code loads and runs somewhat faster.

환경변수에 HOME이라는 변수를 새로 만든후, .emacs 파일을 저장할 위치의 경로를 써준다. 파일 이름은 없고 확장자만 emacs라고 생각하면 편하다. 기본으로 제공하는 메모장에서는 이런 형식으로 저장을 할 수가 없다. 따라서 아래서 소개한 에디터를 이용하기 바란다. 아무위치에나 만들어도 상관은없지만, emac설치폴더에 만드는 것이 편리하다. .emacs 파일에 들어갈 내용은 다음과 같다.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; sml-mode
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(add-to-list 'load-path "C:/ntemacs22/sml-mode") <br />
;; the directory where the sml-mode is installed<br />
(autoload 'sml-mode "sml-mode" "Major mode for editing SML." t)
(autoload 'run-sml "sml-proc" "Run an inferior SML process." t)

(add-to-list ‘auto-mode-alist ‘(“\\.\\(sml\\|sig\\)\\’” . sml-mode))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; syntax coloring
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; M-x font-lock-mode         ;; turn it on in the current buffer
(add-hook 'sml-mode-hook 'turn-on-font-lock) ;;turn it on in all sml-mode buffers
;; (global-font-lock-mode 1)  ;; turn it on everywhere

이름은 없고 확장자만 emacs인 파일 이다. 메모장에선 이런 형태로 저장이 불가능하므로 다른 에디터를 이용해야 할 듯. 에디트플러스나 울트라에디터를 사용하면 편하다. 혹은 emacs를 이용하면 된다.

(add-to-list 'load-path "C:/ntemacs22/sml-mode")에 sml-mode의 압축이 풀린 폴더의 경로만 써주면 된다. 주의할 점은 경로를 쓸때 \ 가 아니라 / 로 구분자 표시를 해줘야한다는 것이다. (이것때문에 고생했다)

이제 emacs에서 ml 프로그래밍을 할 수 있다. emacs에 간단한 ml 코드를 짜보고 컴파일 해보는 것으로 확인 할 수 있다. 이제 이맥스를 켜고 새로운 test.sml 이라는 새로운 파일을 만들고 다음 코드를 짜보자. 보면 알겠지만, 인수로 a와 b중 큰 수를 return하는 함수이다. 이렇게 설명하면 엄밀히 말해서 틀린설명인데, 자세한 것은 ML문법을 참고하기 바란다.

fun max2 a b =
    if a&gt;b then a else b;

syntax coloring[이 제대로 된다면 설치된 것이다. (syntax coloring은 문법에 맞게 highlight 되는 것을 말한다. 예약어따위에 일반 문자와 구별되는 색이 입혀지는 것이 보통이다) syntax cloloring이 제대로 되어 있지 않다면 .emac파일을 확인하여 철자가 틀리지 않았는지 본다. 이제 컴파일을 해보자.

Alt+x를 누르고 run-sml이라고 친 후 엔터를 누르자. M+x와 동일. (여기서 M은 영문자 M이 아니라 Emacs에서 사용하는 특수키이다. 윈도우 시스템에선 Alt에 대응된다. 실제 Emacs 튜토리얼 문서 같은 것에 M+x라고 적혀 있다) 그러면 sml이라고 적혀진 문자열이 출력되는데 다시 엔터를 누르면 컴파일러가 실행된다.

여기에 use "test.sml"; 이라고 치면 컴파일이 된다. 에러가 뜬다면 소스를 다시 확인하기 바란다. 컴파일이 되면 max2 3 4; 를 친 후 엔터를 누르면 함수가 실행이 된다. val it = 4:int 라고 뜬다면 컴파일이 제대로 된 것이다.

다른 방법이 또 있는데, 상단 메뉴 중 tool에서 compile을 누르면 하단에 make -k 와 같은 문자열이 뜰 것이다. 모두 지우고 sml test.sml이라고 치면 컴파일이 된다. 이 때 주의할 점은, 컴파일이 끝나고 컴파일러가 그냥 종료된다는 것이다. 따라서, 드라이버 코드를 소스코드에 포함시켜줘야 할 것이다. 드라이버코드란 테스트코드를 말한다. 위와 같은 경우엔 max2 5 3; 이것이 드라이버 코드가 될 것이다.

결어

ml 프로그래밍 언어는 functional programming language 중 하나이다. 기존에 사용되는 c, c++, java와 같은 언어에는 없는 여러가지 프로그래밍 언어에는 없는 몇 가지 개념들이 많이 사용된다. Emacs로 편리한 ml 프로그래밍 개발 환경을 만들어 쉽게 컴파일하자!