Abstract
Over 20 years ago, Peyton Jones et al. embarked on an adventure in financial engineering with their functional pearl on “Composing Contracts”. They introduced a combinator library—a domain-specific language—for precisely describing complex financial contracts and a formal denotational semantics for computing their value, for which they briefly sketched an implementation.
This paper reworks the design of their library to make the central datatype of contracts less ad-hoc by giving it a well-understood algebraic structure: the semiring. Then, interpreting a contract’s worth as a generic semiring homomorphism directly gives rise to a natural semantics for contracts, of which computing the (monetary) value is but one instance.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 1.
The meaning of the obs parameter is explained in Sect. 2.2.
- 2.
Also called the acquisition date.
- 3.
Backquotes turn a function into an infix operator, e.g., x ‘f’ y = f x y.
- 4.
In reality, floating point numbers such as Double do violate semiring axioms due to rounding errors. Here, we stick with Doubles for simplicity.
- 5.
give need not cancel out expired, since the annihilator of the semiring is excluded from the multiplicative group.
- 6.
The partial inv is acceptable since the annihilator, NegInfty, need not be invertible.
- 7.
For instance, their truncate epoch zero is not equal to our expired, because it can still be acquired at the epoch.
- 8.
See the code repository for a gradient-based semantics.
References
van den Berg, B., Schrijvers, T., McKinna, J., Vandenbroucke, A.: Forward- or reverse-mode automatic differentiation: what’s the difference? Sci. Comput. Program. 231, 103010 (2024)
Elliott, C.: The simple essence of automatic differentiation. Proc. ACM Program. Lang. 2(ICFP), 70:1–70:29 (2018)
Erwig, M., Kollmansberger, S.: Functional pearls: probabilistic functional programming in Haskell. J. Funct. Program. 16(1), 21–34 (2006)
Gibbons, J., Wu, N.: Folding domain-specific languages: deep and shallow embeddings (functional pearl). In: Proceedings of the 19th ACM SIGPLAN International Conference on Functional Programming. p. 339-347. ICFP ’14, Association for Computing Machinery, New York, NY, USA (2014). https://doi.org/10.1145/2628136.2628138
Kozen, D.: Kleene algebra with tests. ACM Trans. Program. Lang. Syst. 19(3), 427–443 (1997)
Peyton Jones, S.L., Eber, J., Seward, J.: Composing contracts: an adventure in financial engineering, functional pearl. In: ICFP. pp. 280–292. ACM (2000)
Rall, L.B.: Automatic Differentiation: Techniques and Applications. Lecture Notes in Computer Science, vol. 120. Springer, Heidelberg (1981). https://doi.org/10.1007/3-540-10861-0
Yorgey, B.A.: Monoids: theme and variations (functional pearl). In: Haskell, pp. 105–116. ACM (2012)
Acknowledgments
We are grateful for the helpful feedback of the anonymous reviewers. Part of this work was funded by FWO project 3E221387.
Author information
Authors and Affiliations
Corresponding author
Editor information
Editors and Affiliations
Ethics declarations
Disclosure of Interests
The authors have no competing interests.
Appendix: Deriving Expired
Appendix: Deriving Expired
Let us derive the implementation of expired from the semantics:
The choice of zero for c in the derivation is immaterial; any contract would do. Moreover, there are other forms of contracts that behave like expired. For instance, (get zero) is also nil everywhere, because the getting a contract with an infinite expiry date is ill-defined, i.e., nil. The definition above is preferable because it relies only on the non-finance specific part of the semantics.
Rights and permissions
Copyright information
© 2024 The Author(s), under exclusive license to Springer Nature Singapore Pte Ltd.
About this paper
Cite this paper
Vandenbroucke, A., Schrijvers, T. (2024). Declarative Pearl: Rigged Contracts. In: Gibbons, J., Miller, D. (eds) Functional and Logic Programming. FLOPS 2024. Lecture Notes in Computer Science, vol 14659. Springer, Singapore. https://doi.org/10.1007/978-981-97-2300-3_6
Download citation
DOI: https://doi.org/10.1007/978-981-97-2300-3_6
Publisher Name: Springer, Singapore
Print ISBN: 978-981-97-2299-0
Online ISBN: 978-981-97-2300-3
eBook Packages: Computer ScienceComputer Science (R0)