Changes between Version 25 and Version 26 of TypeNats/Basics


Ignore:
Timestamp:
Mar 21, 2012 1:11:07 AM (3 years ago)
Author:
diatchki
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TypeNats/Basics

    v25 v26  
    9191to get the concrete integer associated with the singleton type.
    9292
    93 Another approach is to let the system infer the parameter by using the class {{{NatI}}}.  For example:
     93While the explicit {{{TNat}}} parameter is convenient when we define the function, it is a bit
     94tedious to have to provide it all the time---it is easier to let the system infer the value,
     95based on the type of the array:
    9496{{{
    9597memset :: (Storable a, NatI n) => ArrPtr n a -> a -> IO ()
    96 memset ptr a = withTNat (memset_c arr a)
     98memset ptr a = withTNat (memset_c ptr a)
    9799}}}
    98100
    99 In this style, the caller of the function does not need to provide the size of the array explicitly.
    100 Instead, it is computed automatically from the type of the array.
    101 When defining {{{memsetAuto}}} we can use {{{{tNat}}}, the method of {{{NatI}}}, to get access to the value
    102 corresponding to the type level natural.
    103 
    104 When using the implicit style, it is important that the type of {{{tNat}}} is specified precisely.  Failing to do so typically results in ambiguity errors
    105 (i.e., GHC does not know which integer it should use).  Another common mistake is to forget that 'tNat' is a polymorphic value and so every time it is used it may refer to a different value.
    106 
     101The function {{{withTNat}}} is useful when converting from the "explicit" to the "implicit" style
     102because it avoids ambiguity errors, scoped type-variables and other complications.