typing aspects for matlab

Post on 22-Feb-2016

28 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Typing Aspects for MATLAB. Laurie Hendren McGill University Leverhulme Visiting Professor Oxford Computing Lab. TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A A A A A A A A A A A A A A A A A A A A A. Why MATLAB? - PowerPoint PPT Presentation

TRANSCRIPT

Typing Aspects for MATLAB

Laurie Hendren McGill University

Leverhulme Visiting Professor Oxford Computing Lab

2

Why MATLAB?

Why is it important?Why are types

important?Adding type checks

MATLAB style!

3

Nature Article, “… Why Scientific Computing does

not compute”• 45% say scientists spend more time programming than 5 years ago.

• 38% of scientists spend at least 1/5th of their time programming.

• Codes often buggy, sometimes leading to papers being retracted. Self-taught programmers.

• Monster codes, poorly documented, poorly tested, and often used inappropriately.

4

MATLABPERLPython

Domain-specific

FORTRANC/C++

JavaAspectJ

5

A lot of MATLAB programmers!

• started as an interface to standard FORTRAN libraries for use by students.... but now

• 1 million MATLAB programmers in 2004, number doubling every 1.5 to 2 years.

• over 1200 MATLAB/Simulink books

• used in many sciences and engineering disciplines

6

7

MATLAB

FORTRAN

Why do scientists choose MATLAB?

8

Implications of choosing a dynamic,

“scripting” language like

MATLAB, which has dynamic types….

9

Interpreted …

Potentially large

runtime overhead in both time

and space

10

No Types and “Flexible” Syntax ....

11

Lack of a formal standard and open source

compilers for MATLAB

12

McLAB – an infrastructure for compilers and VMs for

MATLABWhat if we extend the language to include typing

aspects?

13

1 function [ r ] =Ex1(n )2 %Ex1(n)creates a vector of n values containing3 % the values [sin(1), sin(2), ..., sin(n)]4 for i=1:n5 r(i) =sin(i );6 end7 end

>> Ex1(3)ans = 0.8415 0.9093 0.1411

>> Ex1(2. 3)ans = 0.8415 0.9093

Simple Example MATLAB function

14

>>Ex1( i nt32(3) )??? Undef i ned f unct i on or method ' si n' f or i nput

arguments of type ' i nt32' .Error i n ==> Ex1 at 5r( i ) = si n( i ) ;

>> Ex1( ' c' )??? For col on operator wi th char operands, f i rst

and l ast operands must be char.Error i n ==> Ex1 at 4f or i =1: n

>> Ex1(@si n)??? Undef i ned f unct i on or method ' _col onobj ' f or

i nput arguments of type ' f unct i on_handl e' .Error i n ==> Ex1 at 4f or i =1: n

15

>>Ex1(compl ex(1, 2))Warni ng: Col on operands must be real scal ars.> I n Ex1 at 4ans = 0. 8415

>> Ex1( t rue)Warni ng: Col on operands shoul d not be l ogi cal .> I n Ex1 at 4ans = 0. 8415

>> Ex1( [3, 4, 5] )ans = 0. 8415 0. 9093 0. 1411

16

MATLAB programmers often expect certain types.

1 function y = sturm(X ,BC,F ,G,R )2 % STURM Solve the Sturm¡ L iouville equation:3 % d( F¤dY / dX )/ dX ¡ G¤Y = R using linear ¯ nite elements.4 % INPUT :5 % X ¡ a one¡ dimensional grid¡ point array of length N .6 % BC ¡ is a 2 by 3 matrix [A1, B1, C1 ; An, Bn, Cn]7 ...8 % Alex P letzer: pletzer@pppl.gov (Aug. 97/ J uly 99).9 ...

17

>>Ex1(3)ans = 0. 8415 0. 9093 0. 1411

>> Ex1( ' c' )Type error i n Ex1.m, Li ne 4: Expect i ng ' n' to havetype ' scal ar of f l oat ' , but got the type' scal ar of char' .

1 function [ r ] =Ex1(n )2 %Ex1(n)creates a vector of n values containing3 % the values [sin(1), sin(2), ..., sin(n)]4 atype('n' ,' scalar of Float' );5 for i=1:n6 r(i) =sin(i );7 end8 atype('r' ,'array [n.value] of n.basetype');9 end

18

High-level types in MATLAB

any

fnhandledata

structcellarrayarray

19

*

numeric

complex

° oat:comp double:compsingle:comp

int:comp

unsign:comp

uint64:compuint32:compuint16:compuint8:comp

signed:comp

int64:compint32:compint16:compint8:comp

real

° oat doublesingle

int

unsigned

uint64uint32uint16uint8

signed

int64int32int16int8

logicalchar

20

Atype Syntax

hatype-stmti ::= atype (̀ ' '̀ ' hvarnamei '̀ ' ,̀ ' '̀ ' htype-speci '̀ ' )̀ '

hvarnamei :== hidenti¯er ij hidenti¯er-patterni

hidenti¯er-patterni :== *̀'j *̀' [̀ a- zA-Z0- 9]+'j [̀ a- zA-Z] [a- zA-Z0-9]*' *̀'

htype-speci ::= anyj hdataij fnhandlej hidenti¯er i . type

hdatai ::= dataj harray-typeij hcellarray-typeij hstruct-typei

21

Array Syntax

harray-typei ::= scalar [of hbase-typei]j array [hdimsi] [of hbase-typei]

hbase-typei ::= *̀'j charj logicalj hnumericij hidenti¯er i .̀ ' basetype

hdimsi ::= [̀ ' hdim-listi ]̀ 'j [̀ ' .̀ . . ' ]̀ 'j [̀ ' .̀ . . ' ,̀ ' hdim-listi ]̀ 'j [̀ ' hdim-listi ,̀ ' .̀ . . ' ]̀ 'j [̀ ' hdim-listi ,̀ ' .̀ . . ' ,̀ ' hdim-listi ]̀ 'j [̀ ' hidenti¯er i .̀ ' dims ]̀ '

hdimi ::= *̀'j hinteger-literalij <̀' hidenti¯er i >̀'j hidenti¯er i .̀ ' valuej hidenti¯er i .̀ ' hidenti¯er i

22

Simple Example

1 function [ r ] =foo( a, b, c, d )2 atype('a' , 'array [...] of int' );3 atype('b' , 'array[¤,¤]' );4 atype('c' , 'array [¤,¤,...]) of complex');5 atype('d' , ' scalar of uint32' );6 % ...7 %bodyof foo8 % ...9 atype('r' ,'array[a.dims] of int' );10 end

23

*

numeric

complex

° oat:comp double:compsingle:comp

int:comp

unsign:comp

uint64:compuint32:compuint16:compuint8:comp

signed:comp

int64:compint32:compint16:compint8:comp

real

° oat doublesingle

int

unsigned

uint64uint32uint16uint8

signed

int64int32int16int8

logicalchar

24

Capturing reflective information

1 function [ r ] =foo( a )2 atype('a' ,'any');3 % ...4 %bodyof foo5 % ...6 atype('r' ,'a.type' );7 end

• a.type• a.value• a.dims• a.basetype

25

Capturing dimensions and basetype

1 function [ r ] =foo( a, b )2 atype('a' ,'array[<n>,<m>]ofreal');3 atype('b' ,'array[a.m,<p>]ofa.basetype');4 % ...5 %bodyof foo6 % ...7 atype('r' ,'array[a.m,b.p] of a.basetype');8 end

• <n> can be used as a dimension spec• value of n is instantiated from the runtime dimension• repeated use in same atype statement implies equality

26

*

numeric

complex

° oat:comp double:compsingle:comp

int:comp

unsign:comp

uint64:compuint32:compuint16:compuint8:comp

signed:comp

int64:compint32:compint16:compint8:comp

real

° oat doublesingle

int

unsigned

uint64uint32uint16uint8

signed

int64int32int16int8

logicalchar

27

Example with a cellarray of structs

1 function [ r ] =foo( ca, f )2 atype('ca' , ' cellarray [<n>,<m>]of3 struct with f x: double, y: doubleg' );4 atype(' f' ,'fnhandle');5 % ...6 %bodyof foo7 % ...8 atype('r' ,' cellarray[ca.m,ca.n] of ca.basetype');9 end

28

Semantics: atype(‘name’, ‘type’) Find matching variables, raise error if

none exist. A conservative existance check could be done statically by the compiler.

Check run-time type against static specification, raise error if mismatch.

Bind context and specification free variables. Only bind those which are live after this statement.

29

Implementation Can be implemented at different levels:

Raise error if type mismatchLog warnings for type mismatchesInsert MATLAB comments

Implementation can be via:MATLAB libraryMatcher/Weaver (add to AspectMatlab)

30

Are “Typing Aspects” AOP?

atype statements not in a separate module (could be, but not very useful as documentation)

has matching/weaving observes and captures run-time context

Aspect Ideas MATLAB-styleprogramming

31

How will we use typing aspects? annotate library MATLAB code,

determine if the specification is rich enough

develop static type analyses which can use the information conveyed by the atype statements (useful for MATLAB to FORTRAN compiler)

32

Related Work Static type analysis (ahead-of-time and

JIT compilers). (MaJIC and FALCON -Padua’s group, McJIT and McFOR– my group)

Types for other dynamic scripting languages. (Ruby – Furr, An, Foster & Hicks; Pluggable Types – Papi, Ali, Correa, Perkins & Ernst)

33

Ongoing work

• Complete implementation of typing aspects, both as a library and via a weaver – hopefully summer 2011. (… convince my students they are a good idea …)

• Release of three main tools: front-end/analysis framework, McVM (Virtual Machine) and McFor (MATLAB to FORTRAN). PLDI 2011 tutorial.

• Refactoring tools for MATLAB. How to help programmers convert their programs to better structured, and more efficient codes?

34

Conclusions • MATLAB is an important language.

• Many challenges, types a key challenge.

• Types are dynamic, but important for program understanding and performance.

• Typing aspects one approach.

www.sable.mcgill.ca/mclab

top related