Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
GHC
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
4,311
Issues
4,311
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
384
Merge Requests
384
Requirements
Requirements
List
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Code Review
Insights
Issue
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Glasgow Haskell Compiler
GHC
Wiki
Type nats
implicit explicit
implicit explicit
· Edit Page
Title
Tip: You can move this page by adding the path to the beginning of the title.
More information
.
Format
Markdown
RDoc
AsciiDoc
Org
Content
Write
Preview
There are two different styles of writing functions which need the integer corresponding to a type level natural. One approach is to use an explicit parameter of type `Nat n`. For example: ```wiki memset :: Storable a => ArrPtr n a -> a -> Nat n -> IO () memset = ... ``` This style is, basically, a more typed version of what is found in many standard C libraries. Callers of this function have to pass the size of the array explicitly, and the type system checks that the size matches that of the array. When defining `memset` we can just use `natToInteger` on the `Nat n` parameter to get the actual value of the array size. Another approach is to let the system infer the parameter by using the class `TypeNat`. For example: ```wiki memsetAuto :: (Storable a, TypeNat n) => ArrPtr n a -> a -> IO () ``` In this style, the caller of the function does not need to provide the type of the array explicitly. Instead, it is computed automatically from the type of the array. When defining `memsetAuto` we can use `nat`, the method of `TypeNat`, to get access to the value corresponding to the type level natural. When using the implicit style, it is important that the type of `nat` is specified precisely. Failing to do so typically results in ambiguity errors (i.e., GHC does not know which integer it should use). Another common mistake is to forget that 'nat' is a polymorphic value and so every time it is used it may refer to a different value. An easy way to avoid such problems is to implement the implicit style functions in terms of the explicit ones. For example, we can implement `memsetAuto` like this: ```wiki memsetAuto arr val = memset arr val nat ```
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
To link to a (new) page, simply type
[Link Title](page-slug)
. More examples are in the
documentation
.
Commit message
Cancel