Sensor networks are notoriously difficult to program, given that they encompass the complexities of both distributed and embedded systems. To address this problem, we present the design and implementation of a declarative sensor network platform, DSN: a declarative language, compiler and runtime suitable for programming a broad range of sensornet applications. We demonstrate that our approach is a natural fit for sensor networks by specifying several very different classes of traditional sensor network protocols, services and applications entirely declaratively - these include tree and geographic routing, link estimation, data collection, event tracking, version coherency, and localization. To our knowledge, this is the first time these disparate sensornet tasks have been addressed by a single highlevel programming environment.We address a number of systems challenges that arise when building a generic compiler and runtime environment for the sensornet context; these include not only issues of limited resources, but also the management of asynchrony and requirements of predictable execution. Our results suggest that the declarative approach is well-suited to sensor networks, and that it can significantly improve software productivity and quality while still producing efficient, resourceconstrained code.