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

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

  • Upload
    puppet

  • View
    26

  • Download
    0

Embed Size (px)

Citation preview

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

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

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

4

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

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

Pseudoscience

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

astrology, alchemy, medical quackery, the occult

7

Ambiguity

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

8

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

10

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

CommunicationWhat’s in a word?

Communication is based on Language

A shared understanding of syntax and semantics

Built out of words

Words are symbols

Symbols are abstractions

13

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

14

16

Communication

17

18

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

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

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

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

22

“The system works in mysterious

ways…”

“…it must be a Deity!”

Deus Ex Machina“God arrives via Machinery”

Deus ex machina in Puppet

class a {

}

class b inherits a {

}

class c inherits b { # ERROR ONE LEVEL ONLY

}

24

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

Deus ex machina

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

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

}

notice successors(1)

... (runs until out of machine resources)

26

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

28

Type is a Kind of PatternAn Abstraction

29

3.14158 7

5

12

3

0.5 1.23E3

“red”

“blue”“green”

“hello”

3.14158 7

5

12

3

0.5 1.23E3

30

String

“red”

“blue”“green”

“hello”

31

Number

“red”

“blue”“green”

“hello”

3.14158 7

5

12

3

0.5 1.23E3

32

Integer

“red”

“blue”“green”

“hello”

3.14158 7

5

12

3

0.5 1.23E3

33

Integer[5,7]

“red”

“blue”“green”

“hello”

3.14158

12

3

0.5 1.23E37

5

34

Enum[red, green, blue]

“red”

“blue”“green”

“hello”

3.14158

12

3

0.5 1.23E37

5

35

type Color = Enum[red, green, blue]

“red”

“blue”“green”

“hello”

3.14158

12

3

0.5 1.23E37

5

36

Color

“red”

“blue”“green”

“hello”

3.14158

12

3

0.5 1.23E37

5

37

Variant[Color, Integer]

“red”

“blue”“green”

“hello”

3.14158

12

3

0.5 1.23E37

5

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

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

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

Type TheoryWhat is it?

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

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

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.

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

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

47

CODE CLEAN & TYPE STRONGLYa reinforcing loop of truth

Type your inputs Do not over-type

Test Fail early