# Using Haskell's 'newtype' in C

DevFeed: [Using Haskell's 'newtype' in C](<https://devfeed.tech/articles/using-haskell-s-newtype-in-c-21922.md>)

Original publisher: [Read original article](<https://blog.nelhage.com/2010/10/using-haskells-newtype-in-c/>)

Author: Nelson Elhage

Published: 2010-10-11T13:11:25Z

Content type: tutorial

Language: en

Sources: [Nelson Elhage](<https://devfeed.tech/sources/nelson-elhage.md>)

Topics: [Haskell](<https://devfeed.tech/topics/haskell.md>), [C](<https://devfeed.tech/topics/c.md>), [Software Engineering](<https://devfeed.tech/topics/software-engineering.md>), [Kernel](<https://devfeed.tech/topics/kernel.md>), [Operating system](<https://devfeed.tech/topics/operating-system.md>), [x86](<https://devfeed.tech/topics/x86.md>)

Tags: [c](<https://devfeed.tech/tags/c.md>), [code](<https://devfeed.tech/tags/code.md>), [kernel](<https://devfeed.tech/tags/kernel.md>), [operating-systems](<https://devfeed.tech/tags/operating-systems.md>), [software-engineering](<https://devfeed.tech/tags/software-engineering.md>), [types](<https://devfeed.tech/tags/types.md>), [x86](<https://devfeed.tech/tags/x86.md>)

## AI overview

The article explains how to use a C singleton-based wrapper to emulate Haskell's newtype and make the compiler distinguish values such as physical and virtual addresses. It presents the approach in the context of paging code for a toy x86 operating system and discusses the tradeoff of added accessor and constructor functions.

## Source excerpt

A common problem in software engineering is avoiding confusion and errors when dealing with multiple types of data that share the same representation. Classic examples include differentiating between measurements stored in different units, distinguishing between a string of HTML and a string of plain text (one of these needs to be encoded before it can safely be included in a web page!), or keeping track of pointers to physical memory or virtual memory when writing the lower layers of an operating system's memory management.