standard ml
play

Standard ML ML - historically stands for Meta Language. ML CSCI: - PDF document

Standard ML ML - historically stands for Meta Language. ML CSCI: 4500/6500 Programming was a meta language for expressing and manipulating logical proofs. Languages General purpose, modular functional programming language


  1. Standard ML � � ML - historically stands for Meta Language. ML CSCI: 4500/6500 Programming was a meta language for expressing and manipulating logical proofs. Languages � � General purpose, modular functional programming language developed a team in the 1970s at the Functional Programming Languages University of Edinburgh, headed by Robin Milner (polymorphism paper in reading list). Part 4: Standard Meta Language (SML) & Haskell » � Polymorphism – one behavior for different types » � First language to include “polymorphic type inference” (functions with multiple different types – different input parameters) together with a type-safe exception handling mechanism. 1 2 Maria Hybinette, UGA Maria Hybinette, UGA Preview: Polymorphism Polymorphism � � One behavior (e.g., a function definition) for multiple � � Universal polymorphism : Allows writing code that works with different types (e.g., a function handles different types of input different types parameters). � � Ad-hoc polymorphism : Selecting the right implementation code to be executed � � Ad-Hoc Polymorphism: range of actual types is finite and the combinations must be specified individually prior to use so Parametric (e.g., C multiple definitions (overloading, coercision) -> compiler ++ templates) calls the right definition � Universal � � Parametric Polymorphism ( first type of polymorphism to appear in an actual programming language – ML in 1976) � Inclusion(subtyping) » � NO explicit type definition (e.g., the append function of a list) � Polymorphism » � Used transparently with any number of types � » � Generic Programming (arguably): Only one definition (e.g., Overloading/Overriding (e.g., C ++, Java) templates, macro, note instantiation is laziness, “not Ad hoc evaluated until needed” characteristics) � � � Suptyping Polymorphism (inheritance) – classes related by Coercion (automatic -multi-method, supertype. � 3 single dispatch) 4 Maria Hybinette, UGA Maria Hybinette, UGA Standard ML Standard ML (cont) � � Uses type declarations, but also does type inferencing to � � Standard ML is a domain-specific language determine the types of undeclared variables » � type of all variables can be determined at compile time. that is appropriate for building compilers » � function Foo( a b ) = a + b � � Support for � � Static-scoped � � Syntax is closer to Pascal than to LISP » � Complex data structures (abstract syntax, compiler intermediate forms) » � e.g., infix arithmetic expressions instead of Cambridge postfix � � Restrictions on how data types are intermixed (more later): » � Memory management like Java » � Example: integer division may not be used on strings » � Large projects with many modules » � ML is strongly typed (whereas Scheme is essentially typeless) and has no type coercions (talk more about this later in the » � Advanced type system for error detection Semester) � � Includes exception handling � � Module facility for implementing abstract data types. � � Permits side-effect (therefore an impure functional language) 5 6 Maria Hybinette, UGA Maria Hybinette, UGA

  2. Learn more details Installation Distribution (SML of New Jersey): � � Today we will cover the basics so you can get started. � � http://www.smlnj.org/dist/working/110.69/index.html � � Resources: � � Developed at Bell laboratories and Princeton University » � Robert Harper’s (pdf book) � � Installation (straight forward): Set your PATH variable where – � http://www.cs.cmu.edu/~rwh/smlbook/online.pdf you install it. » � Peter Lee’s: – � http://www.cs.cmu.edu/afs/cs/local/sml/common/smlguide/smlnj.htm » � Forgot? » � SML/NJ Literature: – � find / -name sml –print � – � http://www.smlnj.org/doc/literature.html#tutorials – � /usr/local/smlnj-110.69/bin/sml # default on a MAC � – � export PATH=$PATH:/usr/local/smlnj-110.69/bin � – � Runs on Microsoft Windows, MacOS X (yay!), UNIX, – � setenv PATH $PATH:/usr/local/smlnj-110.69/bin � » � Short & concise tutorial (not available) � � Run: – � http://cs.wwc.edu/Environment/SML-Tutorial.html {saffron:ingrid:219} sml Standard ML of New Jersey v110.69 [built: Tue Feb 3 22:24:07 2009] - 7 8 Maria Hybinette, UGA Maria Hybinette, UGA ML Hello word! in SML Interactive - print("Hello world!\n"); � � Type in expressions Hello world � � Evaluate and print type and result val it = () : unit � � End with ; - � � Exit ( enter end of file ) “it” is the default name of the Compileable expression. 9 10 Maria Hybinette, UGA Maria Hybinette, UGA Preliminaries Preliminaries � � Read – Eval – Print – Loop: � � Read – Eval – Print – Loop: - 3+2; - 3+2; val it = 5 : int 11 12 Maria Hybinette, UGA Maria Hybinette, UGA

  3. Preliminaries Preliminaries � � Read – Eval – Print – Loop: � � Read – Eval – Print – Loop: - 3+2; - 3+2; val it = 5 : int val it = 5 : int - it + 7 ; - it + 7 ; val it = 12 : int 13 14 Maria Hybinette, UGA Maria Hybinette, UGA Preliminaries Preliminaries � � Read – Eval – Print – Loop: � � Read – Eval – Print – Loop: - 3+2; - 3+2; val it = 5 : int val it = 5 : int - it + 7 ; - it + 7 ; val it = 12 : int val it = 12 : int - it - 3 ; - it - 3 ; val it = 9 : int 15 16 Maria Hybinette, UGA Maria Hybinette, UGA Preliminaries Preliminaries � � Read – Eval – Print – Loop: Read – Eval – Print – Loop: � � - 3+2; - 3+2; val it = 5 : int val it = 5 : int - it + 7 ; - it + 7 ; val it = 12 : int val it = 12 : int - it - 3 ; - it - 3 ; val it = 9 : int - 4 + true val it = 9 : int = ; - 4 + true stdIn:14.1-14.9 Error: operator and operand don't agree [literal] operator domain: int * int operand: int * bool in expression: 4 + true 17 18 Maria Hybinette, UGA Maria Hybinette, UGA

  4. � � Copy and paste the following text into a - 4/3 ; Standard ML window: stdIn:20.1-20.4 Error: operator and operand don't agree [literal] operator domain: real * real operand: int * int 2+2; (* note semicolon at end*) in expression: 3*4; 4/3; (* an error! *) 4 / 3 6 div 2; (* integer division *) - 4.0 / 3.0 ; 7 div 3; val it = 1.33333333333 : real 19 20 Maria Hybinette, UGA Maria Hybinette, UGA List functions � � [1,2,3,4]; � � Includes lists and list operations -val it : [1,2,3,4] : int list � � The val statement binds a name to a value (similar to DEFINE in Scheme) � � val myList = [1,2,3,4]; � � Function declaration form: -val myList : [1,2,3,4] : int list fun function_name (formal_parameters) = � � 0 :: [1,2, 3]; function_body_expression; -val it = [0,1,2,3] : int list - 213 :: 0 :: [1,2, 3];val it = [213,0,1,2,3] : int list e.g., fun cube(x : int) = x * x * x ; fun square( x: int) : int = x * x ; 21 22 Maria Hybinette, UGA Maria Hybinette, UGA Haskell Haskell � � Similar to ML (syntax, static scoped, strongly � � Most Important features typed, type inferencing) » � Uses lazy evaluation (evaluate no subexpression until the value is needed) � � Different from ML (and most other functional » � Has list comprehensions, which allow it to deal with languages) in that it is purely functional (e.g., infinite lists no variables, no assignment statements, and no side effects of any kind) 23 24 Maria Hybinette, UGA Maria Hybinette, UGA

  5. Our First Program Function Definition � � ghci » � let fac n = if n == 0 then 1 else n * fac (n-1) � � “Hello World “ � � fac 10 � � putStrLn "Hello World” Compile: ghc -o hello hello.hs ./hello 25 26 Maria Hybinette, UGA Maria Hybinette, UGA Haskell Concurrent Haskell � � Next project: you can choose Haskell or SML. � � ghc spare --make -threaded � � For Haskell you are expected to use the � � Enable threads: Glasgow compiler (co-dependencies – perl, » � time ./primes-test +RTS -N2 gcc) -> compiles like C ( ghc –o main main.hs) � � Run Example Program in threaded and unthreaded mode � � Other compilers: Glasgow (Glorious), Helium, » � “True Parellelism” Hugs, Omega (is strict). » � Running threads in parallel & multiple processors – � Pitfalls? 27 28 Maria Hybinette, UGA Maria Hybinette, UGA Applications of Functional Comparing Functional and Languages Imperative Languages � � Imperative Languages: � � APL is used for throw-away programs » � Efficient execution � � LISP is used for artificial intelligence » � Complex semantics » � Knowledge representation » � Complex syntax » � Concurrency is programmer designed » � Machine learning » � Natural language processing � � Functional Languages: » � Modeling of speech and vision » � Simple semantics � � Scheme is used to teach introductory » � Simple syntax programming at a significant number of » � Inefficient execution universities » � Programs can automatically be made concurrent 29 30 Maria Hybinette, UGA Maria Hybinette, UGA

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