Top Banner
The Truth, Nothing But The Truth. Why Type Systems are important to Configuration Management
48

PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

Jan 10, 2017

Download

Technology

Puppet
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: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

The Truth, Nothing But The Truth.Why Type Systems are important to Configuration Management

Page 2: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

About [email protected] @hel helindbe http://puppet-on-the-edge.blogspot.se

Page 3: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

What is Truth?Do we know it when we see it?

Page 4: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

4

Page 5: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

The Truth, Nothing but the Truth

Truth and Philosophy

Coherence of Belief Witches float.

Divine Truth Do not jeer at a bald

man or you will be torn to pieces by bears.

- 2 KINGS 2:23-24

Logical Truth 0x2b || ! 0x2b

Scientific Observable reality

Are there as many truths as there are scents of shampoo?

5

Page 6: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

Distortions of Truth0.5 x truth + 0.5 x truth != truth

Page 7: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

Pseudoscience

Pseudoscience - Science applied to non Science, or just pure nonsense

astrology, alchemy, medical quackery, the occult

7

Page 8: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

Ambiguity

Ambiguity - lack of context or definition - or simply clashing - the source of puns

8

Page 9: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

Paradoxes

“This sentence is false.”

“This sentenc contains three erors.”

“Can you publish a book containing all published books?”

“Can an almighty deity create a stone it cannot lift?”

9

Page 10: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

10

Page 11: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

Escape to a higher level

“This sentenc contains three erors.”

A “sentenc" is a kind of english sentence where the word “three” means 4, and and “eror” is a white space - all other words are English.

11

Page 12: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

CommunicationWhat’s in a word?

Page 13: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

Communication is based on Language

A shared understanding of syntax and semantics

Built out of words

Words are symbols

Symbols are abstractions

13

Page 14: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

What is this?SHOUT IT OUT… (ready….)

14

Page 15: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet
Page 16: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

16

Page 17: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

Communication

17

Page 18: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

18

What’s Love Got to do With It?Tina Turner

Page 19: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

Is my Puppet Logic error free?Does it do what I intended?

Page 20: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

How do we deal with the problems of Truth?

Constraining the space to what matters ?

Cheat ?

Be sloppy / ambiguous and use “bad math” and rely on Folklore to prevent accidents?

Add rigor ?

Provide features users can build robust solutions on?

20

Page 21: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

What we do not want

Microsoft Word:

moves an image 1mm to the left

all text and images shift

4 new pages appear

in the distance, sirens

@gossipgirl on twitter

21

Page 22: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

22

“The system works in mysterious

ways…”

“…it must be a Deity!”

Page 23: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

Deus Ex Machina“God arrives via Machinery”

Page 24: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

Deus ex machina in Puppet

class a {

}

class b inherits a {

}

class c inherits b { # ERROR ONE LEVEL ONLY

}

24

Page 25: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

Deus ex machina

42.type

=> Integer.type

=> Type[Integer].type

=> Type[Type[Integer]].type

=> Type[Type[Type[Integer]]].type

=> Type[Type[TypeType[[Integer]]]].type

=> Type[Type[Type[Type[Type[Integer]]]]].type

... (until out of machine resources)

25

Page 26: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

Deus ex machina

function successors(Number $x) >> Array[Number] {

[$x, *successors($x+1)]

}

notice successors(1)

... (runs until out of machine resources)

26

Page 27: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

Puppet’s Type System(because only a Deity writes perfect code)

Page 28: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

28

Type is a Kind of PatternAn Abstraction

Page 29: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

29

3.14158 7

5

12

3

0.5 1.23E3

“red”

“blue”“green”

“hello”

Page 30: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

3.14158 7

5

12

3

0.5 1.23E3

30

String

“red”

“blue”“green”

“hello”

Page 31: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

31

Number

“red”

“blue”“green”

“hello”

3.14158 7

5

12

3

0.5 1.23E3

Page 32: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

32

Integer

“red”

“blue”“green”

“hello”

3.14158 7

5

12

3

0.5 1.23E3

Page 33: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

33

Integer[5,7]

“red”

“blue”“green”

“hello”

3.14158

12

3

0.5 1.23E37

5

Page 34: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

34

Enum[red, green, blue]

“red”

“blue”“green”

“hello”

3.14158

12

3

0.5 1.23E37

5

Page 35: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

35

type Color = Enum[red, green, blue]

“red”

“blue”“green”

“hello”

3.14158

12

3

0.5 1.23E37

5

Page 36: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

36

Color

“red”

“blue”“green”

“hello”

3.14158

12

3

0.5 1.23E37

5

Page 37: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

37

Variant[Color, Integer]

“red”

“blue”“green”

“hello”

3.14158

12

3

0.5 1.23E37

5

Page 38: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

Many Built in Data Types

Any

Integer, Float, String, Boolean, Regexp, Pattern, Enum, Array, Hash, Tuple, Struct, Iterable, Binary, Sensitive, SemVer, SemVerRange, TimeSpan, TimeStamp, Variant, Optional, Undef, CatalogEntry, HostClass, Resource, Callable, Runtime

Type

Experimental:

Object, TypeSet

38

Page 39: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

Create your Own

Simple Alias

type Color = Enum[red, blue, green]

Recursive Structure

type IntegerTree = Variant[Integer, Array[IntegerTree, 1]]

Types are Name-spaced, and Autoloaded

type MyModule::Color = Enum[red, blue, green]

4.x Ruby functions can have (additional) local types

39

Page 40: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

And can be used in many ways

function myfunc(MyType $x) >> Boolean { }

define myresource(MyType $x) { }

class myclass(MyType $x) { }

$value =~ MyType

case $value {

MyType : { }

}

assert_type(MyType, $value)

40

Page 41: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

Type TheoryWhat is it?

Page 42: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

Strongly vs. Weakly Typed

A scale from:

Strongly typed - no implicit conversions even if harmless

Performing conversions

Type inference

Do not care - will blow up later…

42

Page 43: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

Type Systems

Strongly or Weakly typed - fuzzy concepts

Memory Protection - allocation/layout of data in memory

Union Types - (OR) Variant type in Puppet

Intersection Types - (AND) Integer[-128, 127] and Integer[0,255] => Integer[0,127]

Existential Types - “has interface” similar to Duck typing

Dependant Types -

function shorten(Array[$T, $n 1] $arr) >> Array[$T, $n-1]

Duck typing (Ruby, JS)

43

Page 44: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

When we have a strong type system - it is like having two programs in one - both the concrete level that we observe when running it, and a program that defines its meaning.

Page 45: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

Type Inference

Static transformation of the program to Type Equations that are then solved.

Can statically find known to be impossible operations.

Can only determine runtime safety if the system is completely typed (statically or by inference).

45

1 + 1 type(1) + type(1) Integer + Integer$x = 101 + $x

type($x) = type(10)type(1) + type($x)

Integer + Integer

$x = /.*/1 + $x

type($x) = type(/.*/)type(1) + type($x)

Integer + Regexp

Page 46: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

Type Inference in Puppet

Precise - all information is retained, unless asked for a reduction or generalization:

$value = [1, ”hello”, 3.14]

notice $value.type

=> Tuple[Integer[1,1], Enum[“hello”], Float[3.14, 3.14]]

notice $value.type(reduced)

=> Array[Scalar, 3, 3]

notice $value.type(generalized)

=> Array[Scalar]

46

Page 47: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet

47

CODE CLEAN & TYPE STRONGLYa reinforcing loop of truth

Type your inputs Do not over-type

Test Fail early

Page 48: PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet