In CPython, the global interpreter lock, or GIL is a mutex that prevents multiple threads from executing Python bytecodes at once. The lock is necessary mainly because CPython’s memory management is not thread-safe.
0 import time
1 import threading
2 import multiprocessing
3
4 NUM = 10000000
5
6 def count(n):
7 while n > 0:
8 ¦ n -= 1
9
10 t1 = threading.Thread(target=count, args=(NUM,))
11 t2 = threading.Thread(target=count, args=(NUM,))
12 start = time.time()
13 t1.start()
14 t2.start()
15 t1.join()
16 t2.join()
17 print "multithread:"
18 print time.time() - start
19
20 start = time.time()
21 count(NUM)
22 count(NUM)
23 print "single thread:"
24 print time.time() - start
25
26 p1 = multiprocessing.Process(target=count, args=(NUM,))
27 p2 = multiprocessing.Process(target=count, args=(NUM,))
28 start = time.time()
29 p1.start()
30 p2.start()
31 p1.join()
32 p2.join()
33 print "multi process:"
34 print time.time() - start
Here’s the output:
multithread:
1.70929884911
single thread:
1.03298616409
multi process:
0.507339954376
Why do I get those performance results?
We can think of category theory as a generalized set theory, where in set theory we have sets and $\in$, but in category theory we have objects and arrows, where arrows are just any kinds of mappings.
So we have a kind of composition structure, where ther order of composition doesn’t matter, but the configuration matters.
And rather than reasoning structurally like PL does, it reasons “behaviorally”.
The main question is, how do we define two programs are equal, and how do we prove it.
So for setup we have type $int$, and $k$ which is a $int$, and $e_1+e_2$.
Family of types is a generalization of the concept of a predicate/relation.
Formally, given a type $A:U$ in a universe of types $U$, one may havea family of types $B:A\rightarrow U$, which assigns to each term $a:A$ a type $B(a):U$. We say that the type $B(a)$ varies with $a$.
e.g.
the following proposition
\[x:\mathbb{N}\vdash even(x)\]is a predicate / propositional function / a family of types (indexed by \(\mathbb{N}\)) / fibration. We can rewrite it as
\[\{even(x)\ prop/type\}_{x:\mathbb{N}}\]The idea is that we are exhibiting a family of types/proofs in that if you give me any particular choice of number, it’s going to give me back a proposition, which may or maynot be inhabitied, but they are all types.
For example, \(even(3)\) will be uninhabited, and \(even(2)\) will be inhabitited.
We can think of Type Theory as being a catalog of a variety of notions of computation. The type structure determines the “programming language features”. For example, whether you have higher order functions amounts to saying “do you have exponential types”; whether you have structs or tuples amounts to saying “do you have Cartesian product types”; whether you have choices or multiple classes of data corresponds to “whether you have sum types”. A programming language is really just a collection of types. So type theory is just a theory of construction. From that point of view, logic is just an application of type theory, because there are particular constructions which correspond to proofs. Other constructions like natural numbers or geometric objects don’t correspond to proofs of particular propositions. They are just mathematical constructions. What type theory is intersted in is the general concept of what is a mathematical construction. That’s why intuitionistic logic is also called constructive logic. From this point of view, we can say that logic is just a corner of mathematics, and mathematics is just a corner of computer science ;)
“Logic as if people matters”. We are talking about communication of knowledge. We treat proofs as mathematical objects, or programs. We claim that $A$ is true, we actually mean that we have a proof of $A$. $M:A$ means that $M$ is a proof of $A$, or $M$ is of type $A$, they are the same thing. There are many strong connections among proof theory, type theory, and category theory.
\(A\uparrow\) means $A$ has a verification.
\(A\downarrow\) means $A$ ay be used
A conversion rule ($\downarrow\uparrow$):
\[\frac{P\downarrow} {P\uparrow}\]where $P$ is atomic