diff --git a/notes/20240930224229-concurrent_erlang.org b/notes/20240930224229-concurrent_erlang.org index 1788e61..5a39789 100644 --- a/notes/20240930224229-concurrent_erlang.org +++ b/notes/20240930224229-concurrent_erlang.org @@ -23,7 +23,7 @@ Pid = spawn(ModName, FuncName, [Arg1, Arg2, ..., ArgN]). sep = 1; pid1 [label="Pid 1", fontsize="10pt", style=filled, fillcolor=grey]; - pid2 [label="Pid 2", xlabel="Mod:Func(Args)", fontsize="10pt", style=filled, fillcolor=red]; + pid2 [label="Pid 2", xlabel="Mod:Func(Args)", fontsize="10pt", style=filled, fillcolor=white]; pid1 -> pid2 [label=" Spawn(Mod, Func, Args)", fontsize="10pt", minlen=1, style=dashed]; } @@ -46,7 +46,7 @@ Pid = spawn(ModName, FuncName, [Arg1, Arg2, ..., ArgN]). rankdir="LR"; pid1 [label="Pid 1", xlabel="Pid2 ! { self(), hello }", fontsize="10pt", style=filled, fillcolor=grey]; - pid2 [label="Pid 2", fontsize="10pt", style=filled, fillcolor=red, xlabel="receive (...) end." ]; + pid2 [label="Pid 2", fontsize="10pt", style=filled, fillcolor=white, xlabel="receive (...) end." ]; pid1 -> pid2 [label="{ Pid1, hello }", fontsize="10pt", style=dashed]; } @@ -91,7 +91,7 @@ end pid1 [label="Pid 1", xlabel="Pid 3 ! { foo, ... }", fontsize="10pt", style=filled, fillcolor=grey]; pid2 [label="Pid 2", xlabel="Pid 3 ! { bar, ... }", fontsize="10pt", style=filled, fillcolor=grey]; - pid3 [label="Pid 3", fontsize="10pt", style=filled, fillcolor=red]; + pid3 [label="Pid 3", fontsize="10pt", style=filled, fillcolor=white]; pid1 -> pid3 [label="{foo, ...}", fontsize="10pt", minlen=1, style=dashed]; pid2 -> pid3 [label="{bar, ...}", fontsize="10pt", minlen=1, style=dashed]; @@ -144,7 +144,7 @@ end timeout [label="🕒", fontsize="10pt", style=filled, fillcolor=white]; pid1 [label="Pid 1", fontsize="10pt", style=filled, fillcolor=grey]; - pid2 [label="Pid 2", fontsize="10pt", style=filled, fillcolor=red]; + pid2 [label="Pid 2", fontsize="10pt", style=filled, fillcolor=grey]; timeout -> pid2 [label="Timeout", fontsize="10pt", minlen=1, style=dashed]; pid1 -> pid2 [label="Msg", fontsize="10pt", minlen=1, style=dashed]; diff --git a/notes/20241001192402-error_handling_in_erlang.org b/notes/20241001192402-error_handling_in_erlang.org index 5b3425f..3c0499f 100644 --- a/notes/20241001192402-error_handling_in_erlang.org +++ b/notes/20241001192402-error_handling_in_erlang.org @@ -56,7 +56,7 @@ P3. rankdir="LR"; sep = 1; - pid1 [label="Pid 1", fontsize="10pt", style=filled, fontcolor=white; fillcolor=black]; + pid1 [label="Pid 1", fontsize="10pt", style=filled, fillcolor=red]; pid2 [label="Pid 2", fontsize="10pt", style=filled, fillcolor=grey]; pid1 -> pid2 [label="{'Exit', Pid1, Reason}", dir=both, fontsize="10pt", minlen=1, style=dashed]; @@ -80,7 +80,7 @@ P3. rankdir="LR"; sep = 1; - pid1 [label="Pid 1", fontsize="10pt", style=filled, fontcolor=white; fillcolor=black]; + pid1 [label="Pid 1", fontsize="10pt", style=filled, fillcolor=red]; pid2 [label="Pid 2", fontsize="10pt", style=filled, fillcolor=grey; shape=doublecircle]; pid3 [label="Pid 3", fontsize="10pt", style=filled, fillcolor=grey]; @@ -93,4 +93,24 @@ P3. #+RESULTS: erlang-error-trapping [[file:../static/img/notes/erlang_error_trapping.png]] +#+NAME: erlang-error-propagation +#+BEGIN_SRC dot :file ../static/img/notes/erlang_error_propagation.png :cmdline -Kdot -Tpng :exports results + digraph Trapping { + node [shape=circle]; + rankdir="LR"; + sep = 1; + + pid1 [label="Pid 1", fontsize="10pt", style=filled, fillcolor=red]; + pid2 [label="Pid 2", fontsize="10pt", style=filled, fillcolor=grey; shape=doublecircle]; + pid3 [label="Pid 3", fontsize="10pt", style=filled, fillcolor=grey]; + + + pid1 -> pid2 [label="{'Exit', Pid1, Reason}", dir=both, fontsize="10pt", minlen=1, style=dashed]; + pid2 -> pid3 [dir=both, fontsize="10pt", minlen=2, style=dashed]; + } +#+END_SRC + +#+RESULTS: erlang-error-propagation +[[file:../static/img/notes/erlang_error_propagation.png]] + #+print_bibliography: diff --git a/notes/20241004123538-erlang_shell.org b/notes/20241004123538-erlang_shell.org index c6fd4c8..966a904 100644 --- a/notes/20241004123538-erlang_shell.org +++ b/notes/20241004123538-erlang_shell.org @@ -7,7 +7,7 @@ Some features of the [[id:de7d0e94-618f-4982-b3e5-8806d88cad5d][Erlang]] shell. -* Examples +* Basics ** Compiling and Running "Hello World" in the Shell #+BEGIN_SRC erlang -module(hello). @@ -64,4 +64,22 @@ terminates the Erlang session. [cite:@armstrong2013 p.14] > f(). #+END_SRC +* Modifying the Development Environment + +You can find the value of the current load path by starting an Erlang shell +and giving the command ~code:get_path()~. The two most common functions that we +use to manipulate the load path are as follows: + +#+begin_src erlang +% Add a new directory to the start of the load path. +-spec code:add_patha(Dir) => true | {error, bad_directory} +% Add a new directory to the end of the load path. +-spec code:add_pathz(Dir) => true | {error, bad_directory} +#+end_src + +Alternatively, you can start Erlang with a command like this: +#+begin_src shell +$ erl -pa Dir1 -pa Dir2 ... -pz DirK1 -pz DirK2 +#+end_src + #+print_bibliography: diff --git a/notes/20241009092203-dializer.org b/notes/20241009092203-dializer.org index 5b06d79..08e854d 100644 --- a/notes/20241009092203-dializer.org +++ b/notes/20241009092203-dializer.org @@ -36,13 +36,13 @@ Tuple :: tuple() | {T1, T2, ... Tn} #+END_SRC New types can be defined with the following syntax: -#+begin_src +#+begin_src erlang -type NewTypeName(TVar1, TVar2, ... , TVarN) :: Type. #+end_src ** Specifying the Input and Output Types of a Function -#+BEGIN_SRC +#+BEGIN_SRC erlang -spec file:open(FileName, Modes) -> {ok, Handle} | {error, Why} when FileName :: string(), Modes :: [Mode], @@ -50,3 +50,30 @@ New types can be defined with the following syntax: Handle :: file_handle(), Why :: error_term(). #+END_SRC + +** Exporting Types +A module can export some types to declare that other modules are allowed to +refer to them as remote types. This declaration has the following form: + +#+BEGIN_SRC erlang + -export_type([T1/A1, ..., Tk/Ak]). +#+END_SRC + +** Opaque Types +The main use case for opacity in Erlang is to hide the implementation of a data +type, enabling evolving the API while minimizing the risk of breaking consumers. + +* Running Dializer +** Things that confuse Dializer +The dialyzer can get easily confused. We can help prevent this by following a +few simple rules. [cite:@armstrong2013 p.151]. ++ Avoid using ~-compile(export_all)~. ++ Provide detailed type specifications for all the arguments to the *exported* + functions in the module. ++ Provide default arguments to all elements in a record definition. If you don't + provide a default, the atom ~undefined~ is taken as the default. ++ Using anonymous variables in arguments to a function often results in types + that are far less specific than you had intended; try to constrain variables + as much as possible. + +#+print_bibliography: diff --git a/static/img/notes/erlang_error_exit_signals.png b/static/img/notes/erlang_error_exit_signals.png index 5d1ccb6..d50d260 100644 --- a/static/img/notes/erlang_error_exit_signals.png +++ b/static/img/notes/erlang_error_exit_signals.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:4b0185c38dbfd18ec884100883c61b58ce513e29bb7f08561195b144dd40a40d -size 8034 +oid sha256:54a0724186fde1a4926b9c36a77ce215fef1129e90cad721ebd643bda19b4f5a +size 8474 diff --git a/static/img/notes/erlang_error_propagation.png b/static/img/notes/erlang_error_propagation.png new file mode 100644 index 0000000..6403025 --- /dev/null +++ b/static/img/notes/erlang_error_propagation.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1dd1e41cbe4ed60339f07a8ac5b68004f25a0af9e0171d6ad64aae1cdc27fb98 +size 13651 diff --git a/static/img/notes/erlang_error_trapping.png b/static/img/notes/erlang_error_trapping.png index 6cf4147..6403025 100644 --- a/static/img/notes/erlang_error_trapping.png +++ b/static/img/notes/erlang_error_trapping.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:b36558e516433a928e455e2401fd7869d005ea740e11bb3efdeb2197e52e56cb -size 13246 +oid sha256:1dd1e41cbe4ed60339f07a8ac5b68004f25a0af9e0171d6ad64aae1cdc27fb98 +size 13651 diff --git a/static/img/notes/erlang_selective.png b/static/img/notes/erlang_selective.png index 1abb9e3..47ebee9 100644 --- a/static/img/notes/erlang_selective.png +++ b/static/img/notes/erlang_selective.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:9265f4ab072df08bde617cafe941571cbd6b48527edbc6f278d5c5cc207f8f10 -size 14863 +oid sha256:02389e2d93b936dec595ae31575e307d56cce337a14ec914c0052b344bc1f86a +size 15018 diff --git a/static/img/notes/erlang_send.png b/static/img/notes/erlang_send.png index 6f94aed..056ebc4 100644 --- a/static/img/notes/erlang_send.png +++ b/static/img/notes/erlang_send.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:16bf08ae3885ad4469b3781e93d6ad7cff59e12d79fb53f6f39c76cbe4a8c420 -size 10701 +oid sha256:32c4367a5de35ff652555cbc7a37e05437fc78f14a0d13961226e2c186b20848 +size 10934 diff --git a/static/img/notes/erlang_spawn.png b/static/img/notes/erlang_spawn.png index f60a179..e1a35ee 100644 --- a/static/img/notes/erlang_spawn.png +++ b/static/img/notes/erlang_spawn.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:07ece5f2f2a0bf200c9dda338c5211d9577f591ec5d22c8004051aea3f075eff -size 11120 +oid sha256:5254775b2a489cbd7862c0ffc99491145353bbe0262aeaab3b20c8404a98d46e +size 11495 diff --git a/static/img/notes/erlang_timeout.png b/static/img/notes/erlang_timeout.png index 8dc8695..69466dd 100644 --- a/static/img/notes/erlang_timeout.png +++ b/static/img/notes/erlang_timeout.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:84bb8d76eabd3cd5e9f01dc7401132596f1315e37e2b49ac62307d5da9cc6c34 -size 12202 +oid sha256:e5a04605103edcc37301e51bad867a39a5fad6d81b3ec657cb1c077170529203 +size 12674