- 1
- 2
- 3
- 4
- 5
- 6
Ltac2 make_match fields :=
destruct x;
iter (fun a => focus 1 1 (fun () =>
let a := a ()
in refine (fun () => '((w_rep $a) _)))
) fields.
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
Всего: 91
+2
Ltac2 make_match fields :=
destruct x;
iter (fun a => focus 1 1 (fun () =>
let a := a ()
in refine (fun () => '((w_rep $a) _)))
) fields.
Итерация по конструкторам индуктивного типа данных.
+1
(** Set of all possible interleaving of two traces is a trace
ensemble. As we later prove in [interleaving_to_permutation], this
definition is dual to [Permutation]. *)
Inductive Interleaving : list TE -> list TE -> TraceEnsemble :=
| ilv_cons_l : forall te t1 t2 t,
Interleaving t1 t2 t ->
Interleaving (te :: t1) t2 (te :: t)
| ilv_cons_r : forall te t1 t2 t,
Interleaving t1 t2 t ->
Interleaving t1 (te :: t2) (te :: t)
| ilv_nil : Interleaving [] [] [].
Попытка оптимизации:
(* Left-biased version of [Interleaving] that doesn't make
distinction between schedulings of commuting elements: *)
Inductive UniqueInterleaving : list TE -> list TE -> TraceEnsemble :=
| uilv_cons_l : forall l t1 t2 t,
UniqueInterleaving t1 t2 t ->
UniqueInterleaving (l :: t1) t2 (l :: t)
| uilv_cons_r1 : forall l r t1 t2 t,
~trace_elems_commute l r ->
UniqueInterleaving (l :: t1) t2 (l :: t) ->
UniqueInterleaving (l :: t1) (r :: t2) (r :: l :: t)
| uilv_cons_r2 : forall r1 r2 t1 t2 t,
UniqueInterleaving t1 (r1 :: t2) (r1 :: t) ->
UniqueInterleaving t1 (r2 :: r1 :: t2) (r2 :: r1 :: t)
| uilv_nil : forall t, UniqueInterleaving [] t t.
Сложный говнокод. Почему вторая "оптимизированная" версия работает хуже первой?
+1
waiting_for_data(info, {Driver,Socket,Data},
#state{socket=Socket, driver=Driver, driver_mod=DriverMod, peer=Peer, control=Control, list=List} = State) ->
%% The meat of the whole project: process a function call and return
%% the data
try erlang:binary_to_term(Data) of
{{CallType,M,F,A}, Caller} when CallType =:= call; CallType =:= async_call ->
{ModVsnAllowed, RealM} = check_module_version_compat(M),
case check_if_module_allowed(RealM, Control, List) of
true ->
case ModVsnAllowed of
true ->
WorkerPid = erlang:spawn(?MODULE, call_worker, [CallType, RealM, F, A, Caller, Socket, Driver, DriverMod]),
?log(debug, "event=call_received driver=~s socket=\"~s\" peer=\"~s\" caller=\"~p\" worker_pid=\"~p\"",
[Driver, gen_rpc_helper:socket_to_string(Socket), gen_rpc_helper:peer_to_string(Peer), Caller, WorkerPid]),
{keep_state_and_data, gen_rpc_helper:get_inactivity_timeout(?MODULE)};
false ->
?log(debug, "event=incompatible_module_version driver=~s socket=\"~s\" method=~s module=~s",
[Driver, gen_rpc_helper:socket_to_string(Socket), CallType, RealM]),
waiting_for_data(info, {CallType, Caller, {badrpc,incompatible}}, State)
end;
false ->
?log(debug, "event=request_not_allowed driver=~s socket=\"~s\" control=~s method=~s module=~s",
[Driver, gen_rpc_helper:socket_to_string(Socket), Control, CallType, RealM]),
waiting_for_data(info, {CallType, Caller, {badrpc,unauthorized}}, State)
end;
{cast, _M, _F, _A} = Cast ->
handle_cast(Cast, State),
{keep_state_and_data, gen_rpc_helper:get_inactivity_timeout(?MODULE)};
BatchCast when is_list(BatchCast) ->
[handle_cast(Cast, State) || Cast <- BatchCast],
{keep_state_and_data, gen_rpc_helper:get_inactivity_timeout(?MODULE)};
{abcast, Name, Msg} ->
_Result = case check_if_module_allowed(erlang, Control, List) of
true ->
?log(debug, "event=abcast_received driver=~s socket=\"~s\" peer=\"~s\" process=~s message=\"~p\"",
[Driver, gen_rpc_helper:socket_to_string(Socket), gen_rpc_helper:peer_to_string(Peer), Name, Msg]),
Msg = erlang:send(Name, Msg);
false ->
?log(debug, "event=request_not_allowed driver=~s socket=\"~s\" control=~s method=~s",
[Driver, gen_rpc_helper:socket_to_string(Socket), Control, abcast])
end,
{keep_state_and_data, gen_rpc_helper:get_inactivity_timeout(?MODULE)};
{sbcast, Name, Msg, Caller} ->
Reply = case check_if_module_allowed(erlang, Control, List) of
true ->
?log(debug, "event=sbcast_received driver=~s socket=\"~s\" peer=\"~s\" process=~s message=\"~p\"",
[Driver, gen_rpc_helper:socket_to_string(Socket), gen_rpc_helper:peer_to_string(Peer), Name, Msg]),
case erlang:whereis(Name) of
undefined -> error;
Pid -> Msg = erlang:send(Pid, Msg), success
end;
false ->
?log(debug, "event=request_not_allowed driver=~s socket=\"~s\" control=~s method=~s",
[Driver, gen_rpc_helper:socket_to_string(Socket), Control, sbcast]),
error
end,
waiting_for_data(info, {sbcast, Caller, Reply}, State);
ping ->
?log(debug, "event=ping_received driver=~s socket=\"~s\" peer=\"~s\" action=ignore",
[Driver, gen_rpc_helper:socket_to_string(Socket), gen_rpc_helper:peer_to_string(Peer)]),
{keep_state_and_data, gen_rpc_helper:get_inactivity_timeout(?MODULE)};
OtherData ->
?log(debug, "event=erroneous_data_received driver=~s socket=\"~s\" peer=\"~s\" data=\"~p\"",
[Driver, gen_rpc_helper:socket_to_string(Socket), gen_rpc_helper:peer_to_string(Peer), OtherData]),
{stop, {badrpc,erroneous_data}, State}
catch
error:badarg ->
{stop, {badtcp,corrupt_data}, State}
end;
%% Handle the inactivity timeout gracefully
waiting_for_data(timeout, _Undefined, #state{socket=Socket, driver=Driver} = State) ->
?log(info, "message=timeout event=server_inactivity_timeout driver=~s socket=\"~s\" action=stopping",
[Driver, gen_rpc_helper:socket_to_string(Socket)]),
{stop, normal, State};
waiting_for_data(info, {DriverClosed, Socket} = Msg, #state{socket=Socket, driver_closed=DriverClosed} = State) ->
handle_event(info, Msg, waiting_for_data, State);
waiting_for_data(info, {DriverError, Socket, _Reason} = Msg, #state{socket=Socket, driver_error=DriverError} = State) ->
handle_event(info, Msg, waiting_for_data, State).
Срочно требуется учитель литературы, чтобы объяснить, что хотел сказать автор.
0
make_process_name("client", {Node,Key}) when is_atom(Node) ->
%% This function is going to be called enough to warrant a less pretty
%% process name in order to avoid calling costly functions
KeyStr = erlang:integer_to_list(erlang:phash2(Key)),
NodeStr = erlang:atom_to_list(Node),
erlang:list_to_atom("gen_rpc.client." ++ NodeStr ++ "/" ++ KeyStr);
Самый страшный грех, который только возможен в Erlang.
+2
% Totoro sitting in the snow
% By Noa Hoffmann and Pascal Günthner, 21.12.2020
\documentclass[tikz,11pt]{{standalone}}
\usepackage{calligra}
\usepackage[T1]{fontenc}
\usetikzlibrary{%
shapes, shadows, patterns, calc,
decorations.shapes,
decorations.fractals,
decorations.markings,
decorations.pathmorphing
}
\colorlet{bodycolor}{black!35!gray!60!brown!98!green}
\colorlet{bellycolor}{yellow!70!white!92!green}
\tikzset{
furspot/.pic = {
\path [draw = black, thick, fill] (0,0)
.. controls +(0.3,0) and +(0.25,-0.05) .. ++(0.35,-.45)
.. controls +(-0.45,0.25) and +(0.1,0) .. ++(-0.85,-0.05)
.. controls +(-0.3,0.1) and +(-0.4, 0) .. cycle;
},
claw/.pic = {
\path [fill = bodycolor!70, draw] (0,0) arc (0:45:0.2 and 0.8)
arc (135:180:0.2 and 0.8)
arc (180:360:0.059) -- cycle;
},
whiskers/.pic = {
\path [fill = bodycolor!70,draw] (0,0) arc (0:45:0.05 and 2.3)
arc (135:180:0.3 and 2.3)
to[out=-90,in=-90] cycle;
},
snowflake/.pic = {
\fill [decoration = Koch snowflake, white] decorate{ decorate{
decorate{ (-0.5,-0.3) -- ++(60:1) -- ++(-60:1) -- cycle }}};
\foreach \i in {30, 90, 150, 210, 270, 330} {
\draw[blue!50!white,very thin] (0,0) -- +(\i:0.3);
}
\draw[decoration = Koch snowflake, blue!50!white, very thin]
decorate{($(0,0)+(60:0.2)$) -- ($(0,0)+(300:0.2)$) --
($(0,0)+(180:0.2)$) -- cycle};
}
}
\tikzset{
snow/.style = {decoration = {random steps, segment length = 2mm,
amplitude = 0.4mm}, decorate},
plush/.style = {decoration = {random steps, segment length = 1mm,
amplitude = 0.5mm},decorate}
}
\begin{document}
\begin{tikzpicture}[color = bodycolor, draw = black, thick]
%---------------------background and tail----------------------
% blue sky
\fill[blue!30!white] (-8cm,-11cm) rectangle (8cm,10cm);
% random snowflakes
\foreach \i in {0.1,0.11,...,1}{
\pic [scale = \i, opacity = 0.9] at (rand*7.5, rnd*18-10.5) {snowflake};}
% more tiny snowflakes
%\foreach \i in {0.1,0.11,...,0.5}{
%\pic [scale = \i, opacity = 0.9] at (rand*7.5, rnd*18-10.5) {snowflake};}
% cloud with merry christmas
\node [cloud,aspect = 6.5, cloud puff arc = 120, cloud puffs = 12.9, fill = white,
color = white] at (0,7) {\Huge M \hspace{9.8cm}.};
\node [color = red] at (0,7) {\fontsize{50}{80}
\textbf{Merry Christmas \quad }};
% tail
\path [draw, fill, rotate = 50] (-4,-7.5) circle (1.5 and 2.2);
% snowhill
\fill [draw, gray!6, snow] (-8,-11) to[in=200, out=0] (-3,-7.5) to (3,-7.5)
to[out=-20, in=180] (8,-11);
%--------------------body-----------------------------------------
% right ear
\path [fill, draw] (0.6,2.3)+(-45:1) arc (-60:35:1 and 1.5)
arc (115:210:1 and 1.5);
% left ear
\path [fill, draw] (-0.6,2.3)+(-135:1) arc (-120:-215:1 and 1.5)
arc (65:-30:1 and 1.5);
% head
\path [draw, fill] ($(0,0)+(170:2.5 and 2)$) arc (170:10:2.5 and 2)
arc(35:-20: 3 and 2)
-- ($(0,-0.8)+(200:3 and 2)$) arc (200:145:3 and 2) -- cycle;
% body
\path[fill] ($(0,-4)+(200:4 and 4.5)$) arc (200:-20:4 and 4.5);
%----------------------face----------------------------------------
% left eye
\path [draw, fill = white] (-1.4,0.7) circle (0.45 and 0.4);
\fill [black] (-1.2,0.7) circle (0.16);
\fill [white] (-1.24,0.74) circle (0.03);
% right eye
\path [draw, fill = white, thick] (1.4,0.7) circle (0.4);
\fill [black] (1.25,0.7) circle (0.16);
\fill [white] (1.20,0.74) circle (0.03);
% nose
\path [draw] (0.35, 0.7) .. controls (0.2,0.8) and (-0.2, 0.8)
.. (-0.35, 0.7);
Какое аниме ))) https://texample.net/tikz/examples/totoro/
0
-module(pqueue).
-export([ in/3
, out/1
, new/0
, close/1
]).
-type prio() :: non_neg_integer().
-record(priority_queue,
{ tab :: ets:tid()
}).
-define(size, {size, size}).
-define(seqno(PRIO), {seqno, PRIO}).
-opaque t() :: #priority_queue{}.
-export_type([ prio/0
, t/0
]).
-spec new() -> t().
new() ->
Tab = ets:new(pqueue_tab, [ordered_set]),
ets:insert(Tab, {?size, 0}),
#priority_queue{tab = Tab}.
-spec close(t()) -> ok.
close(#priority_queue{tab = Tab}) ->
true = ets:delete(Tab),
ok.
-spec in(term(), prio(), t()) -> ok.
in(Val, Prio, #priority_queue{tab = Tab}) when Prio >= 0 ->
Key = {get_next_seqno(Tab, Prio), Prio},
true = ets:insert_new(Tab, {Key, Val}),
ets:update_counter(Tab, ?size, {2, 1}, {?size, 0}),
ok.
-spec out(t()) -> {value, term()} | empty.
out(#priority_queue{tab = Tab}) ->
case ets:first(Tab) of
Key = {SeqNo, _Prio} when is_integer(SeqNo) ->
Val = ets:lookup_element(Tab, Key, 2),
ets:update_counter(Tab, ?size, {2, -1}),
ets:delete(Tab, Key),
{value, Val};
_ ->
empty
end.
%% This function generates keys that go in sequence for each
%% individual priority level, but interleave for different priority
%% levels. Keys with lower priority are more sparse, so they are
%% consumed less often in the total sequence
get_next_seqno(Tab, Prio) ->
Delta = Prio + 1,
Key = ?seqno(Prio),
ets:update_counter(Tab, Key, {2, Delta}, {Key, 0}).
Творение безумца или гения.
+2
#!/bin/bash
set -euo pipefail
host() {
echo "n${1}.local"
}
node() {
echo "erl@$(host $1)"
}
build() {
[ "${1}" -eq "1" ] && echo "build: ."
}
container() {
cat <<EOF
worker${1}:
$(build $1)
image: worker
hostname: $(host $1)
networks:
backplane:
aliases:
- $(host $1)
environment:
- "NODE_NAME=$(node $1)"
- ... прочая питушня
EOF
}
main() {
cat <<EOF
version: '3.3'
networks:
backplane:
services:
$(node 1)
$(node 2)
...
EOF
}
main > docker-compose.yml
docker-compose $@
Как тебе такое, Helm?
+1
Ltac destruct_mint_ H :=
match type of H with
(MInt_ _ ?z ?t) =>
lazymatch goal with
|- ?GOAL =>
refine (match H in (MInt_ _ z0 t0) return (z = z0 -> t = t0 -> GOAL) with
| mint_nil _ =>
fun Heq_z Heq_tt_ =>
ltac:(destruct_mint_common Heq_tt_ Heq_z H)
| mint_cons _ te rest l r t H =>
fun Heq_z Heq_tt_ =>
ltac:(destruct_mint_common Heq_tt_ Heq_z H)
| mint_cons_l _ te rest l r z t Hz H =>
fun Heq_z Heq_tt_ =>
ltac:(destruct_mint_common Heq_tt_ Heq_z H)
| mint_cons_r _ te te' rest l r z t Hz Hcomm H =>
fun Heq_z Heq_tt_ =>
ltac:(destruct_mint_common Heq_tt_ Heq_z H)
end (eq_refl z) (eq_refl t))
end
end.
Наебавшись с inversion в механизированным доказательстве, закрыл я очи.
+2
...
fun([N1, _N2], Trace) ->
?assert(
?strict_causality( #{?snk_kind := "Adding table to a shard", shard := _Shard, live_change := true}
, #{?snk_kind := "Shard schema change"}
, ?of_node(N1, Trace)
)),
?assert(
?strict_causality( #{?snk_kind := "Shard schema change", shard := _Shard}
, #{?snk_kind := "Restarting shard server", shard := _Shard}
, ?of_node(N1, Trace)
)),
%% Schema change must cause restart of the replica process and bootstrap:
{_, Rest} = ?split_trace_at(#{?snk_kind := "Shard schema change"}, Trace),
?assert(
?strict_causality( #{?snk_kind := "Restarting shard server", shard := _Shard}
, #{?snk_kind := state_change, to := bootstrap}
, Rest
))
end).
Немного galaxy-brain тестов
+1
dirty_boostrap_test() ->
SourceTab = ets:new(source, [public, named_table]),
ReplicaTab = ets:new(replica, [public, named_table]),
%% Insert some initial data:
ets:insert(source, {1, 1}),
ets:insert(source, {2, 2}),
ets:insert(source, {3, 3}),
try
register(testcase, self()),
Replica = spawn_link(fun replica/0),
register(replica, Replica),
%% "importer" process emulates mnesia_tm:
spawn_link(fun importer/0),
%% "bootstrapper" process emulates bootstrapper server:
spawn_link(fun bootstrapper/0),
receive
done ->
SrcData = lists:sort(ets:tab2list(source)),
RcvData = lists:sort(ets:tab2list(replica)),
?assertEqual(SrcData, RcvData)
end
after
ets:delete(SourceTab),
ets:delete(ReplicaTab)
end.
importer() ->
Ops = [ {write, 3, 3}
, {write, 4, 4}
, {write, 4, 5}
, {delete, 2}
],
lists:map(fun(OP) ->
import_op(source, OP),
%% Imitate mnesia event (note: here we send it
%% directly to the replica process bypassing
%% the agent):
replica ! {tlog, OP}
end,
Ops),
replica ! last_trans.
replica() ->
receive
{bootstrap, K, V} ->
ets:insert(replica, {K, V}),
replica();
bootstrap_done ->
replay()
end.
replay() ->
receive
{tlog, Op} ->
import_op(replica, Op),
replay();
last_trans ->
testcase ! done
end.
import_op(Tab, {write, K, V}) ->
ets:insert(Tab, {K, V});
import_op(Tab, {delete, K}) ->
ets:delete(Tab, K).
bootstrapper() ->
{Keys, _} = lists:unzip(ets:tab2list(source)),
[replica ! {bootstrap, K, V} || K <- Keys, {_, V} <- ets:lookup(source, K)],
replica ! bootstrap_done.
Follow-up к треду про то, как делать снепшоты.