introduc on to standard ml
play

Introduc)on To Standard ML general purpose programming language. - PowerPoint PPT Presentation

The ML Programming Language ML (Meta Language) was developed by Robin Milner in 1975 for specifying theorem provers. It since has evolved into a Introduc)on To Standard ML general purpose programming language. Important features of ML: sta?c


  1. The ML Programming Language ML (Meta Language) was developed by Robin Milner in 1975 for specifying theorem provers. It since has evolved into a Introduc)on To Standard ML general purpose programming language. Important features of ML: sta?c typing : catches type errors at compile-)me. • type reconstruc?on : infers types so programmers don’t have to • SOLUTIONS write them explicitly polymorphism : func)ons and values can be parameterized over • types (think Java generics, but much beRer). CS251 Programming Languages func?on-oriented (func?onal) : encourages a composi)on-based • Spring 2019 style of programming and first-class func)ons Lyn Turbak sum-of-products dataypes with paIern-matching: simplifies the • manipula)on of tree-structured data Department of Computer Science These features make ML an excellent language for mathema)cal calcula)on, data structure implementa)on, and programming Wellesley College language implementa)on (= metaprogramming). Introduction to Standard ML 2 Two ways to run sml ML Dialects Way #1 : Run sml on the csenv or wx Virtual box appliances from CS240 There are several different dialects of ML. The two we use at Wellesley are: (see following slides). Standard ML (SML) : Version developed at AT&T Bell Labs. • Way #2 : Run sml within a terminal window on the new CS server, cs.wellesley.edu . We’ll use this in CS251. The par)cular implementa)on we’ll use is (It is no longer necessary to used the old server, old-tempest.wellesley.edu.) Standard ML of New Jersey (SMLNJ): Begin by connec)ng to your CS server account via ssh. • hRp://www.smlnj.org/ On a Mac, you can do this in your terminal window. • On a Windows PC, you’ll need to use a terminal emulator like puRy • Objec?ve CAML : Version developed at INRIA (France). We have • [fturbak@Franklyns-MBP ~]$ ssh gdome@cs.wellesley.edu some)mes used this in other Wellesley courses. gdome@cs.wellesley.edu's password: Last login: Sun Mar 31 22:19:28 2019 from … These dialects differ in minor ways (e.g., syntac)c conven)ons, library This is the new virtual server running CentOS 7 func)ons). See the following for a comparison: New CentOS 7 [gdome@tempest ~]$ which sml /usr/local/smlnj/bin/sml hRp://www.mpi-sws.mpg.de/~rossberg/sml-vs-ocaml.html New CentOS 7 [gdome@tempest ~]$ sml Standard ML of New Jersey v110.85 [built: Tue Mar 26 16:24:43 2019] - 1 + 2; val it = 3 : int Introduction to Standard ML 3 Introduction to Standard ML 4

  2. SML and csenv/wx Two ways to run sml Way #1 : Run sml on the csenv or wx Virtual box appliances from CS240 (see following slides). Way #2 : Run sml within a terminal window the new CS server, cs.wellesley.edu . (It is no longer necessary to used the old server, old-tempest.wellesley.edu.) Begin by connec)ng to your CS server account via ssh. • On a Mac, you can do this in your terminal window. • On a Windows PC, you’ll need to use a terminal emulator like puRy • [fturbak@Franklyns-MBP ~]$ ssh gdome@cs.wellesley.edu gdome@cs.wellesley.edu's password: Last login: Sun Mar 31 22:19:28 2019 from … This is the new virtual server running CentOS 7 We will use SML inside the csenv Virtual Machine appliance. Details on how to install New CentOS 7 [gdome@tempest ~]$ which sml csenv and install SML within csenv are available on the CS251 schedule page. /usr/local/smlnj/bin/sml For ini)al examples, it’s easiest to run SML in a terminal window, as shown above. New CentOS 7 [gdome@tempest ~]$ sml But we’ll soon see (slides 19-21) running it in Emacs is much beRer! Standard ML of New Jersey v110.85 [built: Tue Mar 26 16:24:43 2019] - 1 + 2; val it = 3 : int Introduction to Standard ML 5 Introduction to Standard ML 6 Learning SML by Interac)ve Examples Naming Values Solu)ons Try out these examples. (Note: many answers are missing in these slides so you can predict them. See the solu;on slides for answers.) - val a = 2 + 3; val a = 5 : int [wx@wx ~] sml Standard ML of New Jersey v110.78 [built: Wed Jan 14 12:52:09 2015] - a * a; - 1 + 2; val it = 25 : int val it = 3 : int - 3+4; - it + a; val it = 7 : int val it = 30 : int - 5+6 = ; val it = 11 : int - 7 = + = 8; val it = 15 : int Introduction to Standard ML 7 Introduction to Standard ML 8

  3. Nega)ve Quirks Division Quirks - 2 - 5; - 7 / 2; val it = ~3 : int stdIn:1.1-1.6 Error: operator and operand don't agree [literal] - -17; operator domain: real * real stdIn:60.1 Error: expression or pattern begins with infix operand: int * int identifier "-" in expression: stdIn:60.1-60.4 Error: operator and operand don't agree 7 / 2 [literal] operator domain: 'Z * 'Z - 7.0 / 2.0; operand: int in expression: val it = 3.5 : real - 17 - 7 div 2; (* integer division *) - ~17; val it = 3 : int val it = ~17 : int (* For a description of all top-level operators, see: - 3 * ~1; http://www.standardml.org/Basis/top-level-chapter.html *) val it = ~3 : int Introduction to Standard ML 9 Introduction to Standard ML 10 When Parentheses MaRer Simple Func)ons Solu)ons - dbl(5); (* parens are optional here *) val it = 10 : int - val inc = fn x => x + 1; val inc = fn : int -> int (* SML figures out type! *) - (dbl 5); (* parens are optional here *) val it = 10 : int - inc a; - inc (dbl 5); (* parens for argument subexpressions are required! *) val it = 6 : int val it = 11 : int - (inc dbl) 5; - fun dbl y = y * 2; stdIn:1.2-2.2 Error: operator and operand don't agree [tycon mismatch] (* Syntactic sugar for val dbl = fn y => y * 2 *) operator domain: int val dbl = fn : int -> int operand: int -> int in expression: inc dbl - dbl 5; val it = 10 : int - inc dbl 5; (* default left associativity for application *) stdIn:22.1-22.10 Error: operator and operand don't agree [tycon - (fn x => x * 3) 10; (* Don � t need to name function mismatch] operator domain: int to use it *) operand: int -> int val it = 30 : int in expression: inc dbl Introduction to Standard ML 11 Introduction to Standard ML 12

  4. Booleans Solu)ons Condi)onals Solu)ons - 1 = 1; - fun f n = if n > 10 then 2 * n else n * n; val it = true : bool val f = fn : int -> int - 1 > 2; val it = false : bool - f 20; - (1 = 1) andalso (1 > 2); val it = 40 : int val it = false : bool - (1 = 1) orelse (1 = 2); - f 5; val it = true : bool val it = 25 : int - (3 = 4) andalso (5 = (6 div 0)); (* short-circuit evaluation *) val it = false : bool - fun isEven n = (n mod 2) = 0; val isEven = fn : int -> bool (* SML figures out type! *) - isEven 17; val it = false : bool - isEven 6; val it = true : bool Introduction to Standard ML 13 Introduction to Standard ML 14 Recursion Solu)ons Easier to Put Your Code in a File (* This is the contents of the file - fun fact n = ~cs251/download/sml/mydefns.sml ‘ = if n = 0 then (* By the way, comments nest properly in SML! *) = 1 It defines integers a and b and functions named sq, hyp, and fact *) = else val a = 2 + 3 = n * (fact (n - 1)); (* fun names have recursive scope *) val fact = fn : int -> int val b = 2 * a (* simpler than Java definition b/c no explicit types! *) fun sq n = n * n (* squaring function *) - fact 5; (* calculate hypotenuse of right triangle with sides a and b *) val it = 120 : int fun hyp a b = Math.sqrt(Real.fromInt(sq a + sq b)) fun fact n = (* a recursive factorial function *) - fact 12; if n = 0 then val it = 479001600 : int 1 else n * (fact (n - 1)) - fact 13; uncaught exception Overflow [overflow] • File is a sequence of value/func)on defini)ons. raised at: <file stdIn> • Defini)ons are not followed by semi-colons in files! (* SML ints have limited size L *) • There are no con?nua?on characters (equal signs) for mul)ple-line defini)ons. Introduction to Standard ML 15 - Introduction to Standard ML 16

  5. Using Code From a File Another File Example - Posix.FileSys.getcwd(); (* current working directory *) val it = "/students/gdome" : string (* This is the contents of the file test-fact.sml *) - Posix.FileSys.chdir("/students/gdome/cs251/sml"); val fact_3 = fact 3 (* change working directory *) val it = () : unit val fact_a = fact a - Posix.FileSys.getcwd(); val it = "/students/gdome/cs251/sml" : string - use "test-fact.sml"; [opening test-fact.sml] - use "mydefns.sml"; (* load defns from file as if *) [opening mydefns.sml] (* they were typed manually *) val fact_3 = 6 : int val a = 5 : int val fact_a = 120 : int val b = 10 : int val it = () : unit val sq = fn : int -> int val hyp = fn : int -> int -> real val fact = fn : int -> intval it = () : unit - fact a val it = 120 : int Introduction to Standard ML 17 Introduction to Standard ML 18 Use Emacs within csenv/wx for all your SML edi)ng/tes)ng Nested File Uses Launch Emacs by clicking on the icon, or executing emacs & � (to create a new Emacs window) or emacs –nw (to run Emacs directly in the shell..) (* The contents of the file load-fact.sml *) use "mydefns.sml"; (* semi-colons are required here *) use “test-fact.sml"; Emacs editor buffer in - use "load-fact.sml"; SML mode. [opening load-fact.sml] Edit your [opening mydefns.sml] SML code here. val a = 5 : int val b = 10 : int *sml* interpreter val sq = fn : int -> int buffer. val hyp = fn : int -> int -> real Evaluate SML val fact = fn : int -> intval expressions here. [opening test-fact.sml] Create this via val fact_3 = 6 : int M-x sml or val fact_a = 120 : int C-c C-b or C-c C-s val it = () : unit (see next slide). val it = () : unit Introduction to Standard ML 19 Introduction to Standard ML 20

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend