Skip to content
GitLab
Projects Groups Topics Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Register
  • Sign in
  • GHC GHC
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributor statistics
    • Graph
    • Compare revisions
    • Locked files
  • Issues 5.6k
    • Issues 5.6k
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 660
    • Merge requests 660
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Artifacts
    • Schedules
    • Test cases
  • Deployments
    • Deployments
    • Releases
  • Packages and registries
    • Packages and registries
    • Model experiments
  • Analytics
    • Analytics
    • CI/CD
    • Code review
    • Insights
    • Issue
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Glasgow Haskell CompilerGlasgow Haskell Compiler
  • GHCGHC
  • Wiki
  • Type nats
  • alternative singletons

alternative singletons · Changes

Page history
Edit TypeNats/AlternativeSingletons authored Jan 16, 2011 by diatchki's avatar diatchki
Hide whitespace changes
Inline Side-by-side
type-nats/alternative-singletons.md 0 → 100644
View page @ 7b8189c3
We use a family of singleton types to related type-level naturals to runtime values.
In our design, we chose to provide as primitive an overloaded "smart" constructor and a polymorphic elimination construct:
```wiki
newtype Nat (n :: Nat) = Nat Integer
class NatI n where
nat :: NatI n
instance NatI 0 where nat = Nat 0
instance NatI 1 where nat = Nat 1
...
natToInteger :: Nat n -> Integer
natToInteger (Nat n) n = n
```
It is also possible to make the dual choice, where we provide a polymorphic constructor and an overloaded elimination construct:
```wiki
data Nat (n :: Nat) = Nat
class NatE n where
natToInteger :: Nat n -> Integer
instance NatE 0 where natToInteger Nat = 0
instance NatE 1 where natToInteger Nat = 1
...
```
We made this choice, at least in part, because it made the implementation simpler: the evidence for class `NatI` is just an integer. Note that our choice does not loose any generality because we can define the alternative design in terms of it:
```wiki
data Nat1 (n :: Nat) = Nat
nat1ToInteger :: NatI n => Nat1 n -> Integer
nat1ToInteger x = natToInteger (cast nat x)
where cast :: Nat n -> Nat1 n -> Nat n
cast x Nat = x
```
\ No newline at end of file
Clone repository
  • Adventures in GHC compile times
  • All things layout
  • AndreasK
  • AndreasPK
  • Back End and Run Time System
  • Backpack refactoring
  • Backpack units
  • Brief Guide for Compiling GHC to iOS
  • Building GHC on Windows with Stack protector support (SSP) (using Make)
  • CAFs
  • CafInfo rework
  • Compiling Case Expressions in ghc
  • Compiling Data.Aeson Error
  • Contributing a Patch
  • Core interface section
View All Pages