3.3 A Type System for Introducing Parallelism
3.3.1 The Structure-Annotated Type System
The program structure abstraction, σ ∈Σ, is defined below.
σ ∈Σ ::= σs | parF σp
σs∈Σs ::= a | σ◦σ | hyloF σ σ
σp∈Σp ::= fun σs | dcn,F σs σs
| σpkσp | farmn σp | fb σp
Figs. 3.5a– 3.5b define the annotated type system that extends our type system from Figs. 3.2– 3.3, and that associates expressions e ∈ E with structures σ ∈ Σ. As before, the global environment, ρ, maps primitive functions to their types. The annotated arrow, e : A −→σ B, states that e
has exactly the structure σ. In order to define A 7−→σ B, we need to extend the type system further with a convertibility relation.
Convertibility
We extend our type system with a non-structural rule that captures the
convertibility relation, ≡, for Σ.
e : A σ1
−→B σ1 ≡σ2
e : A σ2
7−→B
The relation ≡ is defined in terms of the relations ≡s∈ Σs ×Σs and ≡p∈ Σp×Σp, plus a rule that links the Σs and Σp levels, par-equiv.
σ1 ≡s σ2
σ1 ≡σ2
σ1 ≡pσ2
parF σ1 ≡parF σ2
parF (fun σ)≡mapF σ (par-equiv)
The structures map and iter are defined in Section 3.4, and represent the
structures of the hylomorphism representations ofmap anditer. We define a number of equivalences, starting with ≡p. A parallel pipeline structure (k) is functionally equivalent to a function composition; a task farm farm can
be introduced for any structure; and divide-and-conquer dc and feedback fb can be derived from hylomorphisms.
fun σ1 kfun σ2 ≡p fun (σ2◦σ1) (pipe-equiv)
dcn,F σ1 σ2 ≡p fun (hyloF σ1 σ2) (dc-equiv)
farmn σ ≡p σ (farm-equiv)
These equivalences, plus reflexivity, symmetry and transitivity, define an equational theory that allows conversion between different parallel forms, as well as conversion between structured expressions and parallel forms, as required by our type system. In these equivalences, we implicitly assume the necessary well-formedness constraints: any structure under a funordc
must be in Σs, and the structure under farm must be in Σp. Note that, thanks to the transitivity of≡, we can use these equivalences to derive inter- esting properties of our parallel structures. For example, the associativity of parallel pipelines does not need to be defined explicitly, since it can be derived from the associativity of composition. We defer the definition of ≡s to Section 3.3.2.
3.3.3 Definition | Convertibility in E. For all convertibility rules inΣ, there is an equivalent rule in E . We define the equivalence relation E ∈E×E to be the relation ≡ lifted to E .
An example that illustrates this is that the pipe-equiv rule corresponds to
the rule (fun s1 kfun s2) Ep (fun (s2◦s1)).
3.3.1 Lemma Semantic equivalence.
∀e1,e2 ∈E, e1 E e2 ⇒[[e1]] =ext [[e2]]
Proof Straightforward by induction on the structure of the equivalence re- lation E, using the denotational semantics of P, and the laws of hylomor-
phisms (Section 3.4).
As a consequence of Lemma 3.3.1, the E relation can be used to define the families Es. Any extension to ≡ and E may expose more opportunities for
parallelisation in Es. There remains only the definition of the function φs.
We defer this to Section 3.4, together with the decision procedure for≡and
E.
Soundness and Completeness
Since the annotated type system is an extension of that from Figs 3.2–3.3 and since the convertibility rule only applies to structures, it is trivial to
3.3. A TYPE SYSTEM FOR INTRODUCING PARALLELISM 83
hyloF inF outF = idµF Hylo-Reflex
hyloF (f ◦η)g = hyloG f (η◦g)
⇐ η : F →G Hylo-Shift
(hyloF f h1) ◦ (hyloF h2 g)
⇐ h1◦h2 =id Hylo-Compose
f1◦(hyloF g1 g2)◦f2 = hyloF g10 g20 Hylo-Fusion
⇐ f1 strict ∧ f1 ◦g1 =g10 ◦F f1 ∧ g2◦f2 =F f2◦g20
hyloF f g strict
⇐ f,g strict Hylo-Strict
Figure 3.6: Hylomorphism Laws
show that the new type system is both sound and complete wrt the orig- inal system, once structure is removed, since ∀e ∈ E, σ ∈ Σ, e : A 7−→σ B =⇒ e : A → B. We therefore omit these proofs. Our main soundness and completeness theorems for convertibility ensure that the type system derives only functionally equivalent parallel structures from structured ex- pressions. The proofs of these properties build on a number of details that are introduced in Section 3.4.
3.3.1 Theorem Soundness of Conversion.
∀s∈S, σ ∈Σ, s:A→7−σ B ⇒ sσ ∈Es
Proof Sincesσ is a synonym forφ
s(σ),sσ is in Es if φs is defined for σ. This
property follows directly from the definition of φs(σ) (Def. 3.4.1) and from
Thm 3.4.1 in Section 3.4.
A consequence of the soundness of the conversion and of Lemma 3.3.1 is that if a structured expression typechecks with type A 7−→σ B, then there always exists a functionally equivalent e whose structure is σ.
3.3.1 Corollary ∀s ∈S, σ ∈Σ, s :A→7−σ B ⇒ ∃e∈E such that e : A −→σ B and [[e]] =ext [[s]].
3.3.2 Theorem Completeness of Conversion.
∀s ∈S;σ, σ0 ∈Σ; s : A−σ→0 B∧sσ ∈Es ⇒ s :A
σ
7− →B
Proof This follows directly from the definition of φs(σ) (Def. 3.4.1) and
Thm 3.4.1 in Sec. 3.4.
Remark Note that the rules presented in Figure 3.5 could be implemented as a deep-embedded EDSL in a dependently typed language. This was our original approach in [CH14]. However, the possibility of non-termination makes reasoning about the semantics of these programs quite a hard task. We chose to first prototype these ideas as a standalone DSL, implemented in Haskell, but it would be interesting to implement these ideas as a depen- dently typed, deeply embedded EDSL. An important challenge would be to implement the typechecking algorithm, and the rewriting system in Sec- tion 3.4. In a dependently typed programming, it may be worth exploring whether some of the algebraic properties that are used for proving equiv- alences can be captured by an algebraic structure in Slama and Brady’s hierarchy of provers [SB17].