modular module systems
TRANSCRIPT
![Page 1: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/1.jpg)
Modular Module Systems:a survey
Christopher LeagueLIU Brooklyn
Northeast Scala Symposium
March
![Page 2: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/2.jpg)
What is a module?
© Miran Lipovača, Learn You a Haskell for Great Good!http://learnyouahaskell.com/ (By-NC-SA)
![Page 3: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/3.jpg)
What is a module?
![Page 4: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/4.jpg)
What is a module?. Separate compilation. Namespace management. Hiding / abstraction
![Page 5: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/5.jpg)
Separate compilation in CI “module” == file
#include <stdio.h>
I declarations
extern int foo;
I vs. definitions
int foo = 40;
![Page 6: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/6.jpg)
Namespace management in CI Hiding only, to limit namespace pollution
static void dont_export_me_bro(){
//...}
![Page 7: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/7.jpg)
Namespace management in C++I Nesting, scope operator, imports, limited renaming
namespace greenfield { }using namespace std;using greenfield::cout;namespace magick = future::tech;magick::dwim();
![Page 8: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/8.jpg)
Abstract data types in CI Opaque type declaration (∃), void* (∀?)
struct stack;stack* new_stack();void push (stack*, void*);
![Page 9: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/9.jpg)
Type abstraction in C++I Generics (∀ T such that ???)
template<class T>T& findMin(T* array, int size);
![Page 10: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/10.jpg)
Hiding in C++I Member access control
class Foo {private:int x;
};
![Page 11: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/11.jpg)
Hiding in C++I Privacy via subsumption
struct stack {virtual void push(int) = 0;virtual int pop() = 0;static stack* create();
};struct stackImpl : public stack {
int a[SIZE];int k;// ...
};
![Page 12: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/12.jpg)
Modules in Haskell“Haskell’s module design is relatively conservative”
—A Gentle Introduction to Haskellmodule Utility.StatFS(
FileSystemStats(..),getFileSystemStats) where
import Data.ByteStringimport Data.ByteString.Char8 (pack)import qualified Foreign as F
getFileSystemStats ::String -> IO (Maybe FileSystemStats)
getFileSystemStats path = {- ... -}
data FileSystemStats = {- ... -}
![Page 13: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/13.jpg)
Type classes in Haskellclass Arbitrary a wherearbitrary :: Gen a
instance Arbitrary Bool wherearbitrary = choose (False,True)
instance (Arbitrary a, Arbitrary b) => Arbitrary (a,b)wherearbitrary = liftM2 (,) arbitrary arbitrary
prn :: (Arbitrary a, Show a) => a -> IO ()
![Page 14: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/14.jpg)
![Page 15: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/15.jpg)
SignaturesI Type declarations and value specifications
signature COLLECTION = sigtype ’a tval empty: ’a tval isEmpty: ’a t -> bool
endsignature QUEUE = sig
type ’a tval enqueue: ’a * ’a t -> ’a tval dequeue: ’a t -> (’a t * ’a) option
endsignature DEQUE = sig
include COLLECTIONstructure Front: QUEUE where type ’a t = ’a tstructure Rear: QUEUE where type ’a t = ’a t
end
![Page 16: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/16.jpg)
StructuresI Nested collections of defs, constrained by sigs
structure Deque :> DEQUE = structtype ’a t = ’a list * ’a listval empty = (nil, nil)fun isEmpty (nil, nil) = true| isEmpty _ = false
structure Front = structtype ’a t = ’a tfun enqueue (x, (rs,fs)) = (rs, x::fs)fun dequeue (nil, nil) = NONE| dequeue (rs, x::fs) = SOME ((rs,fs), x)| dequeue (rs, nil) = dequeue (nil, rev rs)
endstructure Rear = struct (* ... *) end
end
![Page 17: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/17.jpg)
FunctorsI Structures parameterized by structuresI Not the thing from category theory, Haskell
functor TestDeque(D: DEQUE) = structval q1 = D.emptyval q2 = D.Front.enqueue (3, q1)val q3 = D.Front.enqueue (2, q2)val q4 = D.Rear.enqueue (4, q3)val q5 = D.Rear.enqueue (5, q4)(* ... *)
end
structure T = TestDeque(Deque)
![Page 18: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/18.jpg)
‘Functorized’ style in MLI Lift most structure dependencies
to functor parameters
functor CompileF(M : CODEGENERATOR): COMPILE0 = ...functor EvalLoopF(Compile: TOP_COMPILE) : EVALLOOP = ...functor Interact(EvalLoop : EVALLOOP) : INTERACT = ...
I Instantiate dependencies at ‘link time’
structure Interact =Interact(EvalLoopF(CompileF(X86MC)))
![Page 19: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/19.jpg)
A signature for mutable graphsI Parameterize by type representing Vertex, Edge.
trait DirectedGraphSig {trait Graph[V,E] {def vertices: Iterator[V]def successors(v: V): Iterator[(V,E)]def add(v: V)def contains(v: V): Booleandef add(v1: V, v2: V, e: E)def get(v1: V, v2: V): Option[E]
}def create[V,E]: Graph[V,E]
}
![Page 20: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/20.jpg)
Yes, mutable — sorryimport scala.collection.generic.MutableMapFactoryimport scala.collection.generic.MutableSetFactoryimport scala.collection.mutable._
![Page 21: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/21.jpg)
Representing graphsI Adjacency list vs. adjacency matrix
I In general: V → V → E
![Page 22: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/22.jpg)
Building graphs from mapsclass DirectedGraphFun[M1[A,B] <: Map[A,B] with MapLike[A,B,M1[A,B]],M2[A,B] <: Map[A,B] with MapLike[A,B,M2[A,B]]](MF1: MutableMapFactory[M1],MF2: MutableMapFactory[M2])
extends DirectedGraphSig{class GraphImpl[V,E] extends Graph[V,E] {private val rep: M1[V,M2[V,E]] = MF1.empty// ...
}def create[V,E] = new GraphImpl[V,E]
}
![Page 23: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/23.jpg)
Instantiating the ‘functor’object AdjacencyListextends DirectedGraphFun[HashMap, ListMap](HashMap, ListMap)
object AdjacencyMatrixextends DirectedGraphFun[HashMap, HashMap](HashMap, HashMap)
I Easily build new modules with different space-timecharacteristics
![Page 24: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/24.jpg)
Inspiration from C++ STL
#include <algorithm>
![Page 25: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/25.jpg)
Graph search implementationclass GraphSearchFun[S[A] <: Set[A] with SetLike[A,S[A]]](S: MutableSetFactory[S],WL: WorkListSig)
{type Path[V,E] = List[(V,E)]def search[V,E](g: DirectedGraphSig#Graph[V,E],
origin: V,f: (V, Path[V,E]) => Unit) {
val visited = S.empty[V]val work = WL.create[(V,Path[V,E])]work.put((origin, Nil))while(!work.isEmpty) {val (v1, path1) = work.take// ...
}}
}
![Page 26: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/26.jpg)
Graph search relies on a work listtrait WorkListSig {trait WorkList[T] {def isEmpty: Booleandef put(x: T)def take: T
}def create[T]: WorkList[T]
}
![Page 27: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/27.jpg)
Various work list strategiesobject LIFO extends WorkListSig {trait StackAsWorkList[T]extends Stack[T] with WorkList[T] {def put(x: T) = push(x)def take: T = pop
}def create[T] = new Stack[T] with StackAsWorkList[T]
}object FIFO extends WorkListSig {trait QueueAsWorkList[T]extends Queue[T] with WorkList[T] {def put(x: T) = enqueue(x)def take: T = dequeue
}def create[T] = new Queue[T] with QueueAsWorkList[T]
}
![Page 28: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/28.jpg)
Voilà — different search algorithmsobject BFSextends GraphSearchFun[Set](Set, FIFO)
object DFSextends GraphSearchFun[Set](Set, LIFO)
![Page 29: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/29.jpg)
class ExampleFun[G <: DirectedGraphSig](G: G) {def example: G#Graph[String,Int] = {val g = G.create[String,Int]g.add(”A”, ”B”, 2); g.add(”A”, ”C”, 3)g.add(”A”, ”D”, 1); g.add(”G”, ”C”, 4)g.add(”D”, ”H”, 3); g.add(”D”, ”I”, 2)g.add(”E”, ”F”, 3); g.add(”G”, ”F”, 2)g.add(”H”, ”G”, 5); g.add(”I”, ”J”, 1)g.add(”J”, ”K”, 6); g.add(”K”, ”G”, 2)g.add(”K”, ”L”, 1)g
}}
![Page 30: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/30.jpg)
BFS on AdjacencyListA from List()D from List((A,1))C from List((A,3))B from List((A,2))I from List((D,2), (A,1))H from List((D,3), (A,1))J from List((I,1), (D,2), (A,1))G from List((H,5), (D,3), (A,1))K from List((J,6), (I,1), (D,2), (A,1))F from List((G,2), (H,5), (D,3), (A,1))L from List((K,1), (J,6), (I,1), (D,2), (A,1))
DFS on AdjacencyMatrixA from List()B from List((A,2))D from List((A,1))I from List((D,2), (A,1))J from List((I,1), (D,2), (A,1))K from List((J,6), (I,1), (D,2), (A,1))G from List((K,2), (J,6), (I,1), (D,2), (A,1))F from List((G,2), (K,2), (J,6), (I,1), (D,2), (A,1))C from List((G,4), (K,2), (J,6), (I,1), (D,2), (A,1))L from List((K,1), (J,6), (I,1), (D,2), (A,1))H from List((D,3), (A,1))
![Page 31: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/31.jpg)
Untyped→ typedI Traits from Smalltalk, SelfI Flavors, mixins from CLOS
![Page 32: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/32.jpg)
![Page 33: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/33.jpg)
Untyped→ typed“It was not obvious how to combine the C++ strong statictype checking with a scheme flexible enough to supportdirectly the ‘mixin’ style of programming used in someLISP dialects.”
![Page 34: Modular Module Systems](https://reader034.vdocuments.us/reader034/viewer/2022042715/559220c71a28abe4598b46af/html5/thumbnails/34.jpg)
Untyped→ typed ?