The document discusses the importance of type systems for configuration management. It explores different concepts of truth and how ambiguity can arise from lack of context or definition. The document advocates for adding rigor to type systems to provide features that allow users to build robust solutions, while avoiding problems caused by ambiguity or lack of definition. It describes Puppet's type system and how types act as patterns that constrain values and prevent accidents.
Similaire à PuppetConf 2016: The Truth, Nothing but the Truth: Why Type Systems are Important to Configuration Management – Henrik Lindberg, Puppet (20)
5. 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
9. 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
11. 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
19. Is my Puppet Logic error free?
Does it do what I intended?
20. 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
21. 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
24. Deus ex machina in Puppet
class a {
}
class b inherits a {
}
class c inherits b { # ERROR ONE LEVEL ONLY
}
24
25. 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
26. Deus ex machina
function successors(Number $x) >> Array[Number] {
[$x, *successors($x+1)]
}
notice successors(1)
... (runs until out of machine resources)
26
38. 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
39. 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
40. 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
42. 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
43. 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
44. 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.
45. 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 = 10
1 + $x
type($x) = type(10)
type(1) + type($x)
Integer + Integer
$x = /.*/
1 + $x
type($x) = type(/.*/)
type(1) + type($x)
Integer + Regexp
46. 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