Top Banner
Erlang Type Overview Can we add compile-time types to Erlang? Optional Types Dialyzer Proper Conclusion Optional Type Specification in Erlang Andreas Pauley – @apauley Lambda Luminaries – @lambdaluminary November 11, 2013 Andreas Pauley – @apauley Types in Erlang
29

Optional Type Specification in Erlang

May 12, 2015

Download

Technology

Andreas Pauley

A talk on types in Erlang, presented at Lambda Luminaries on November 11, 2013.

http://www.meetup.com/lambda-luminaries/events/145676142/
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Optional Type Specification in Erlang

Andreas Pauley – @apauley

Lambda Luminaries – @lambdaluminary

November 11, 2013

Andreas Pauley – @apauley Types in Erlang

Page 2: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Table of Contents

1 Erlang Type Overview

2 Can we add compile-time types to Erlang?

3 Optional Types

4 Dialyzer

5 Proper

6 Conclusion

Andreas Pauley – @apauley Types in Erlang

Page 3: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Dynamic, but strong

example({}) ->

"empty tuple";

example(List) when is_list(List) ->

length(List);

example(_Else) ->

another_return_type.

Andreas Pauley – @apauley Types in Erlang

Page 4: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Dynamic, but strong

3> 5 + "6".

** exception error: an error occurred when evaluating

an arithmetic expression

in operator +/2

called as 5 + "6"

Andreas Pauley – @apauley Types in Erlang

Page 5: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Table of Contents

1 Erlang Type Overview

2 Can we add compile-time types to Erlang?

3 Optional Types

4 Dialyzer

5 Proper

6 Conclusion

Andreas Pauley – @apauley Types in Erlang

Page 6: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Yes and No :-/

The Haskell giants Simon Marlow and Philip Wadler spentover a year developing a static type system for Erlang (circa1997).

Only a subset of the language could be type-checked.

Process types and inter-process messages could not betype-checked.

Andreas Pauley – @apauley Types in Erlang

Page 7: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Yes and No :-/

The Haskell giants Simon Marlow and Philip Wadler spentover a year developing a static type system for Erlang (circa1997).

Only a subset of the language could be type-checked.

Process types and inter-process messages could not betype-checked.

Andreas Pauley – @apauley Types in Erlang

Page 8: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Yes and No :-/

The Haskell giants Simon Marlow and Philip Wadler spentover a year developing a static type system for Erlang (circa1997).

Only a subset of the language could be type-checked.

Process types and inter-process messages could not betype-checked.

Andreas Pauley – @apauley Types in Erlang

Page 9: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Table of Contents

1 Erlang Type Overview

2 Can we add compile-time types to Erlang?

3 Optional Types

4 Dialyzer

5 Proper

6 Conclusion

Andreas Pauley – @apauley Types in Erlang

Page 10: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Declaring Types

We can declare union/sum types and product types:

-type suit() :: spades | clubs | hearts | diamonds.

-type value() :: 1..10 | jack | queen | king.

-type card() :: {suit(), value()}.

Andreas Pauley – @apauley Types in Erlang

Page 11: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Declaring Types

Types in record definitions:

-record(rsa_id_number, {birth_date :: calendar:date(),

gender_digit :: 0..9,

sequence_nr :: 0..999,

citizen_digit :: 0 | 1,

digit_a :: 0..9,

checksum_digit :: 0..9}).

Andreas Pauley – @apauley Types in Erlang

Page 12: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Using Types

-spec suit(card()) -> suit().

suit({Suit, _}) -> Suit.

Andreas Pauley – @apauley Types in Erlang

Page 13: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Using Types

-type gender() :: male | female.

-type rsa_id_number() :: #rsa_id_number{}.

-export_type([rsa_id_number/0]).

-spec gender(rsa_id_number()) -> gender().

gender(#rsa_id_number{gender_digit=GenderDigit})

when GenderDigit >= 5 -> male;

gender(#rsa_id_number{gender_digit=GenderDigit})

when GenderDigit < 5 -> female.

Andreas Pauley – @apauley Types in Erlang

Page 14: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Table of Contents

1 Erlang Type Overview

2 Can we add compile-time types to Erlang?

3 Optional Types

4 Dialyzer

5 Proper

6 Conclusion

Andreas Pauley – @apauley Types in Erlang

Page 15: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Discrepency Analyzer for Erlang

A static analysis tool developed by Kostis Sagonas from theUniversity of Uppsala in Sweden.

Type inference based on Success Typing instead ofHindley-Milner.

Doesn’t need type specifications, but you get more usefulresults if you provide them.

Andreas Pauley – @apauley Types in Erlang

Page 16: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Discrepency Analyzer for Erlang

A static analysis tool developed by Kostis Sagonas from theUniversity of Uppsala in Sweden.

Type inference based on Success Typing instead ofHindley-Milner.

Doesn’t need type specifications, but you get more usefulresults if you provide them.

Andreas Pauley – @apauley Types in Erlang

Page 17: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Discrepency Analyzer for Erlang

A static analysis tool developed by Kostis Sagonas from theUniversity of Uppsala in Sweden.

Type inference based on Success Typing instead ofHindley-Milner.

Doesn’t need type specifications, but you get more usefulresults if you provide them.

Andreas Pauley – @apauley Types in Erlang

Page 18: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Dialyzer and Types

-type suit() :: spades | clubs | hearts | diamonds.

-type value() :: 1..10 | jack | queen | king.

-type card() :: {suit(), value()}.

Andreas Pauley – @apauley Types in Erlang

Page 19: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Dialyzer and Types

-spec kind(card()) -> face | number.

kind({_, A}) when A >= 1, A =< 10 -> number;

kind(_) -> face.

main() ->

number = kind({spades, 7}),

face = kind({hearts, king}),

number = kind({rubies, 4}),

face = kind({clubs, queen}).

Andreas Pauley – @apauley Types in Erlang

Page 20: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Dialyzer and Types

Checking whether the PLT .deps_plt is up-to-date... yes

Proceeding with analysis...

src/cards.erl:15: Function main/0 has no local return

src/cards.erl:18: The call cards:kind({’rubies’,4})

breaks the contract (card()) -> ’face’ | ’number’

done in 0m0.54s

done (warnings were emitted)

make: *** [dialyzer] Error 2

Andreas Pauley – @apauley Types in Erlang

Page 21: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Table of Contents

1 Erlang Type Overview

2 Can we add compile-time types to Erlang?

3 Optional Types

4 Dialyzer

5 Proper

6 Conclusion

Andreas Pauley – @apauley Types in Erlang

Page 22: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Discrepency Analyzer for Erlang

A QuickCheck-inspired property-based testing tool for Erlang.

Developed by Manolis Papadakis, Eirini Arvaniti and KostisSagonas from the National Technical University of Athens.

Has the ability to generate random tests based on type specs.

Andreas Pauley – @apauley Types in Erlang

Page 23: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Discrepency Analyzer for Erlang

A QuickCheck-inspired property-based testing tool for Erlang.

Developed by Manolis Papadakis, Eirini Arvaniti and KostisSagonas from the National Technical University of Athens.

Has the ability to generate random tests based on type specs.

Andreas Pauley – @apauley Types in Erlang

Page 24: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Discrepency Analyzer for Erlang

A QuickCheck-inspired property-based testing tool for Erlang.

Developed by Manolis Papadakis, Eirini Arvaniti and KostisSagonas from the National Technical University of Athens.

Has the ability to generate random tests based on type specs.

Andreas Pauley – @apauley Types in Erlang

Page 25: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Property tests from the specs

2> proper:check_specs(rsa_id_number).

Testing rsa_id_number:checksum/1

....................................................................................................

OK: Passed 100 test(s).

Testing rsa_id_number:citizen/1

....................................................................................................

OK: Passed 100 test(s).

Testing rsa_id_number:gender/1

....................................................................................................

OK: Passed 100 test(s).

Andreas Pauley – @apauley Types in Erlang

Page 26: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Table of Contents

1 Erlang Type Overview

2 Can we add compile-time types to Erlang?

3 Optional Types

4 Dialyzer

5 Proper

6 Conclusion

Andreas Pauley – @apauley Types in Erlang

Page 27: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Conclusion

On its own, type declarations is maybe just a way to commentyour code.

With the extra tool support and some effort we’re getting a lotof benefit typically only found in statically typed languages.

It’s far from perfect.

Andreas Pauley – @apauley Types in Erlang

Page 28: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Conclusion

On its own, type declarations is maybe just a way to commentyour code.

With the extra tool support and some effort we’re getting a lotof benefit typically only found in statically typed languages.

It’s far from perfect.

Andreas Pauley – @apauley Types in Erlang

Page 29: Optional Type Specification in Erlang

Erlang Type OverviewCan we add compile-time types to Erlang?

Optional TypesDialyzerProper

Conclusion

Conclusion

On its own, type declarations is maybe just a way to commentyour code.

With the extra tool support and some effort we’re getting a lotof benefit typically only found in statically typed languages.

It’s far from perfect.

Andreas Pauley – @apauley Types in Erlang