Skip to content
Snippets Groups Projects
Commit d2e009c2 authored by Miroslav Kratochvil's avatar Miroslav Kratochvil :bicyclist:
Browse files

Merge branch 'mk-julia6' into 'develop'

julia 6

See merge request R3/school/courses!172
parents 127ab9ca baf6109d
No related branches found
No related tags found
No related merge requests found
<div class=leader>
<i class="twa twa-locomotive"></i>
<i class="twa twa-helicopter"></i>
<i class="twa twa-police-car"></i>
<i class="twa twa-trolleybus"></i>
<br>
Data and their types
</div>
# What are types?
Types are a small domain-specific metalanguage that allows programs to *prove facts* about other programs
- originally from set theory (after Russel's disaster, preceding the Gödel's disaster)
- the classes of provable facts vary:
- in λ-calculi, types are theorems that often uniquely determine code
- in <i class="twa twa-snake"></i>, types are more like comments that do not prove anything
- in many languages, types determine the format of data interfaces and data
Julia types:
- determine the format of data
- may carry additional information!
- may be materialized ("reified") and used at runtime
# Types in Julia
Primitive types: `Int32`, `UInt64`, `Float32`, `Char`, `Nothing`, `Function`, ...
Compound types:
```julia
struct FloatComplex
re::Float64
im::Float64
end
```
# Types in Julia
Parametrized types: `Tuple{...}`, `Array{...}`, `NTuple{N, ...}`, ...
Type aliases:
```julia
const Vector{X} = Array{X, 1}
```
Abstract types (organized with `<:`):
```julia
abstract type Anything end
struct Something <: Anything
...
end
```
`Union` and `UnionAll` types:
```julia
const Maybe{X} = Union{Nothing, X}
const VectorOfReals = (Vector{T} where {T<:Real})
```
# How does dispatch work?
Upon a function call, Julia selects the *least generic* method of the function
- the ordering is defined by `<:` on parameter types
- dispatch on multiple parameters is possible and (quite) efficient
- this is the main difference from many other languages!
- dispatch on multiple parameters may be ambiguous!
```julia
julia> f(::Real, ::Int) = 1
f (generic function with 1 method)
julia> f(::Int, ::Real) = 2
f (generic function with 2 methods)
julia> f(1,2)
ERROR: MethodError: f(::Int64, ::Int64) is ambiguous.
```
# How does dispatch work?
Upon a function call, Julia selects the *least generic* method of the function.
- the call is specialized
- size of primitive types MAY be known (their data may be placed directly into registers or arrays)
- size of abstract types may NOT be known (their data must be *boxed*)
Compare:
- `sizeof(['a','b'])`, `sizeof([1.1f0, 2.2f0])`, `sizeof([1.1f0, 'b'])`
- function specialization possibilities:
- `function f(x::Any)`
- `function f(x::Int)`
- `function f(x)`, which is internally `function f(x::T) where {T}`
# Exercise: let's make a ZOO
Zoo has:
- <i class="twa twa-elephant"></i> (with a numeric weight)
- <i class="twa twa-penguin"></i> (each with a name)
- <i class="twa twa-lion"></i> (each of which may be hungry or not)
These are hierarchically organized:
- there may be <i class="twa twa-soft-ice-cream"></i> stands
- some things are grouped at the same place
- there may be signposted <i class="twa twa-information"></i><i class="twa twa-up-right-arrow"></i> paths that lead to a farther thing
- some things are placed in <i class="twa twa-construction"></i>enclosures<i class="twa twa-construction"></i>
"You arrive at the zoo. There's a satiated lion and a small elephant, and also a signpost saying `penguins this way`. If you go that way, there are 5 penguins in an enclosure and an ice cream stand."
# Exercises: ZOO
Make functions that:
- count all penguins
- compute the weight of everything in the zoo
- make all animals in the zoo do their typical sound (`nudge(myZoo)`)
- find the most hidden animals in the zoo (they are behind the most signs)
- find all paths to hungry lions that are not in an enclosure
- print out a text description of the zoo
- convert a Julia datatype to your custom zoo datatype
- `(:penguin, "El Jefe")`, `:hungry_lion`, `:satiated_lion`, `(:elephant, 50.0)`, `:ice_cream_stand`
- groups of things are in a vector
- signposted paths are made of Pairs (`"Penguins go here" => [(:penguin, ...), ....]`)
- `throw` a good error if some data cannot be processed
2023/2023-03-23_ProgrammingWithJulia-6/slides/img/favicon.ico

116 KiB

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 153.14 98.64"><defs><style>.cls-1{fill:#1a1a1a}.cls-2{fill:#4d64ae}.cls-3{fill:#ca3c32}.cls-4{fill:#9259a3}.cls-5{fill:#399746}</style></defs><title>Asset 2</title><g id="Layer_2" data-name="Layer 2"><g id="Layer_1-2" data-name="Layer 1"><g id="layer1"><g id="g3855"><g id="g945"><g id="g984"><g id="g920"><path id="path3804" d="M93.14,80.94h-13V21.13l13-3.58Z" class="cls-1"/><g id="g898"><g id="g893"><path id="path19" d="M22.17,36.33a8.9,8.9,0,1,1,8.9-8.9A8.91,8.91,0,0,1,22.17,36.33Z" class="cls-2"/></g><path id="path3819" d="M29.14,80.83A26.48,26.48,0,0,1,27.83,90a12.12,12.12,0,0,1-3.62,5.4A12.33,12.33,0,0,1,18.57,98a36.64,36.64,0,0,1-7.32.67,22.47,22.47,0,0,1-4.81-.47A13,13,0,0,1,2.9,96.93,6,6,0,0,1,.76,95.07,3.62,3.62,0,0,1,0,92.88,4.26,4.26,0,0,1,1.59,89.5a6.47,6.47,0,0,1,4.33-1.35,5,5,0,0,1,1.87.32,6,6,0,0,1,1.43.79,12,12,0,0,1,1.16,1.07c.31.4.59.77.83,1.12A7.58,7.58,0,0,0,12.72,93a2.3,2.3,0,0,0,1.15.4,1.85,1.85,0,0,0,1-.28,2,2,0,0,0,.71-1,7.18,7.18,0,0,0,.4-1.91,23.12,23.12,0,0,0,.16-3.06V40.48l13-3.58Z" class="cls-1"/></g><path id="path3802" d="M48.14,37.94V68a6.14,6.14,0,0,0,.47,2.39A6.45,6.45,0,0,0,50,72.24a7,7,0,0,0,2,1.27,6.12,6.12,0,0,0,2.4.48,4.2,4.2,0,0,0,1.61-.4,8.42,8.42,0,0,0,1.8-1.12,13.27,13.27,0,0,0,1.81-1.66,12.92,12.92,0,0,0,1.61-2.11V37.94h13v43h-13v-4a22.47,22.47,0,0,1-5.43,3.53,13.62,13.62,0,0,1-5.59,1.28,16.52,16.52,0,0,1-5.9-1,15.59,15.59,0,0,1-4.76-2.89,13.56,13.56,0,0,1-3.17-4.28,12.41,12.41,0,0,1-1.15-5.29V37.94Z" class="cls-1"/><g id="g905"><g id="g890"><path id="path13" d="M105.79,36.33a8.9,8.9,0,1,1,8.91-8.9A8.91,8.91,0,0,1,105.79,36.33Z" class="cls-3"/><path id="path25" d="M127.18,36.33a8.9,8.9,0,1,1,8.91-8.9A8.91,8.91,0,0,1,127.18,36.33Z" class="cls-4"/><path id="path31" d="M116.49,17.8a8.9,8.9,0,1,1,8.9-8.9,8.89,8.89,0,0,1-8.9,8.9Z" class="cls-5"/></g><path id="path3823" d="M100.14,40.6l13-3.58V80.94h-13Z" class="cls-1"/></g><path id="path3808" d="M140.14,58.77a37.64,37.64,0,0,0-3.77,1.87,21.89,21.89,0,0,0-3.46,2.3,12.77,12.77,0,0,0-2.55,2.67,5.12,5.12,0,0,0-1,2.94,8.53,8.53,0,0,0,.32,2.34,7,7,0,0,0,.87,1.91,5.15,5.15,0,0,0,1.23,1.27,2.67,2.67,0,0,0,1.51.48,6.3,6.3,0,0,0,3.18-1,41.31,41.31,0,0,0,3.62-2.47Zm13,22.17h-13V77.52c-.71.61-1.42,1.17-2.11,1.67a14.2,14.2,0,0,1-2.3,1.35,13.56,13.56,0,0,1-2.82.88,19.75,19.75,0,0,1-3.78.31,16,16,0,0,1-5.33-.83,12.23,12.23,0,0,1-4-2.31,10.23,10.23,0,0,1-2.51-3.53,11,11,0,0,1-.87-4.37,10.27,10.27,0,0,1,.91-4.42,13.11,13.11,0,0,1,2.55-3.57,19.36,19.36,0,0,1,3.77-2.86,40.26,40.26,0,0,1,4.65-2.31c1.67-.69,3.4-1.32,5.17-1.91l5.25-1.71,1.43-.31V49.34a11.91,11.91,0,0,0-.44-3.45,5.82,5.82,0,0,0-1.15-2.31,4,4,0,0,0-1.79-1.31,6.6,6.6,0,0,0-2.34-.4,7.38,7.38,0,0,0-2.59.4,4.37,4.37,0,0,0-1.67,1.11,3.94,3.94,0,0,0-.91,1.59,6.52,6.52,0,0,0-.28,2,9.51,9.51,0,0,1-.28,2.35,4.85,4.85,0,0,1-.91,2A4.47,4.47,0,0,1,126,52.6a6.84,6.84,0,0,1-2.9.52,7.51,7.51,0,0,1-2.51-.4,6.16,6.16,0,0,1-1.91-1.15,6,6,0,0,1-1.27-1.75,5.59,5.59,0,0,1-.44-2.18,6.42,6.42,0,0,1,1.51-4.1,13.16,13.16,0,0,1,4.06-3.3,23.45,23.45,0,0,1,5.92-2.14,31.07,31.07,0,0,1,7.12-.8,32.21,32.21,0,0,1,7.87.84,16.37,16.37,0,0,1,5.49,2.34,9.55,9.55,0,0,1,3.18,3.66,10.91,10.91,0,0,1,1,4.81Z" class="cls-1"/></g></g></g></g></g></g></g></svg>
\ No newline at end of file
# Programming with Julia
## March 2023
<div style="top: 6em; left: 0%; position: absolute;">
<img src="theme/img/lcsb_bg.png">
</div>
<div style="top: 1em; left: 60%; position: absolute;">
<img src="slides/img/julia.svg" height="200px">
<h1 style="margin-top:3ex; margin-bottom:3ex;">6: Types and dispatch</h1>
<h4>
Miroslav Kratochvíl<br>
Laurent Heirendt<br>
LCSB, DSSE<br>
</h4>
</div>
<link rel="stylesheet" href="https://lcsb-biocore.github.io/icons-mirror/twemoji-amazing.css">
<style>
code {border: 2pt dotted #f80; padding: .4ex; border-radius: .7ex; color:#444; }
.reveal pre code {border: 0; font-size: 18pt; line-height:27pt;}
em {color: #e02;}
li {margin-bottom: 1ex;}
div.leader {font-size:400%; line-height:120%; font-weight:bold; margin: 1em;}
section {padding-bottom: 10em;}
</style>
[
{ "filename": "index.md" },
{ "filename": "6a-types.md" }
]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment