Skip to content

Commit 98d6afe

Browse files
committed
Manual: Translation part
1 parent 46afe01 commit 98d6afe

File tree

7 files changed

+41
-35
lines changed

7 files changed

+41
-35
lines changed

manual/LowLevel/Translation/01-GeneratedNames.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Haskell name will be, and also is less likely to result in name clashes.
1919

2020
By default, we use the name in the C header wherever one exists. An obvious
2121
example is function definitions, where the candidate name for a function `foo`
22-
is simply `foo`. Similarly, if a struct is given a tag or a typedef,
22+
is simply `foo`. Similarly, if a `struct` is given a tag or a `typedef`,
2323

2424
```c
2525
struct foo { .. };

manual/LowLevel/Translation/02-Structs.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,16 @@ data Triple = Triple
2222
, triple_b :: CInt
2323
, triple_c :: CInt
2424
}
25+
deriving stock (Eq, Show)
2526

2627
instance F.Storable Triple where ...
27-
deriving stock instance Show Triple
28-
deriving stock instance Eq Triple
2928
```
3029

3130
## Structures with and without `typedef`
3231

3332
Adding a `typedef` matching the name of the structure, or using a `typedef` in
3433
place of a structure name, does not change the generated bindings. For more
35-
details, see the [section on name
36-
generation](/manual/LowLevel/GeneratedNames.md).
34+
details, see the [section on name generation](01-GeneratedNames.md).
3735

3836
```c
3937
typedef struct triple triple;
@@ -47,10 +45,8 @@ typedef struct triple triple_t;
4745

4846
```haskell
4947
newtype Triple_t = Triple_t { un_Triple_t :: Triple }
50-
51-
deriving newtype instance F.Storable Triple_t
52-
deriving stock instance Eq Triple_t
53-
deriving stock instance Show Triple_t
48+
deriving stock (Eq, Show)
49+
deriving newtype (Storable)
5450
```
5551

5652
## Nested structures
@@ -82,7 +78,7 @@ creates the following bindings (instances omitted for brevity):
8278
```haskell
8379
data Door = Door
8480
{ door_height :: CFloat
85-
, door_width :: CFloat
81+
, door_width :: CFloat
8682
}
8783

8884
data Room = Room
@@ -135,7 +131,7 @@ struct aula2 {
135131
```haskell
136132
data Aula2_door = Aula2_door
137133
{ aula2_door_height :: CFloat
138-
, aula2_door_width :: CFloat
134+
, aula2_door_width :: CFloat
139135
}
140136

141137
data Aula2 = Aula2
@@ -166,11 +162,11 @@ structure in their `Storable` instance:
166162

167163
```haskell
168164
data Aula_setup = Aula_setup
169-
{ aula_setup_window_id :: CChar
170-
, aula_setup_tilt :: CInt
165+
{ aula_setup_window_id :: CChar
166+
, aula_setup_tilt :: CInt
171167
, aula_setup_close_blinds :: CInt
172168
, aula_setup_projector_id :: CChar
173-
, aula_setup_power_mode :: CInt
169+
, aula_setup_power_mode :: CInt
174170
}
175171

176172
instance F.Storable Aula_setup where
@@ -304,5 +300,5 @@ foreign import ccall safe "Structs_create_square" create_square
304300
:: CDouble -> IO (Ptr Square)
305301
```
306302

307-
Note that opaque types do not get a Storable instance, and therefore can not be
303+
Note that opaque types do not get a `Storable` instance, and therefore can not be
308304
used by value.

manual/LowLevel/Translation/03-Enums.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ class Integral (CEnumZ a) => CEnum a where
124124
toCEnum :: CEnumZ a -> a
125125
fromCEnum :: a -> CEnumZ a
126126

127-
declaredValues :: proxy a -> DeclaredValues a
128-
showsUndeclared :: proxy a -> Int -> CEnumZ a -> ShowS
127+
declaredValues :: proxy a -> DeclaredValues a
128+
showsUndeclared :: proxy a -> Int -> CEnumZ a -> ShowS
129129
readPrecUndeclared :: ReadPrec a
130130
```
131131

@@ -143,7 +143,7 @@ instance CEnum Index where
143143
, (1, NonEmpty.singleton "B")
144144
, (2, NonEmpty.singleton "C")
145145
]
146-
showsUndeclared = showsWrappedUndeclared "Index"
146+
showsUndeclared = showsWrappedUndeclared "Index"
147147
readPrecundeclared = readPrecWrappedUndeclared "Index"
148148
```
149149

@@ -250,7 +250,7 @@ showCursorKind :: CXCursorKind -> String
250250
showCursorKind = \case
251251
CXCursor_UnexposedExpr -> "CXCursor_UnexposedExpr"
252252
CXCursor_UnexposedStmt -> "CXCursor_UnexposedStmt"
253-
kind -> show kind
253+
kind -> show kind
254254
```
255255

256256
We also provide a helper function `showCEnum` for C enums without a specialized

manual/LowLevel/Translation/06-Globals.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,11 @@ x_ptr = unsafePerformIO fe8f4js8
193193

194194
Memory layout:
195195

196-
| type | name | address | value |
197-
| ------------------------ | ------------- | ------- | ------- |
198-
| int | x | 1000 | 17 |
199-
| | | ... | |
200-
| int* ; Ptr CInt | x_ptr | 2000 | 1000 |
196+
| type | name | address | value |
197+
|-------------------|-------|---------|-------|
198+
| `int` | x | 1000 | 17 |
199+
| | | ... | |
200+
| `int* ; Ptr CInt` | x_ptr | 2000 | 1000 |
201201

202202
Constant:
203203

@@ -380,13 +380,13 @@ x_elem_ptr = IncompleteArray.toFirstElemPtr x_ptr
380380

381381
Memory layout:
382382

383-
| type | name | address | value |
384-
| ------------------------------------- | ------------- | ------- | ------- |
385-
| int[] | x | 1000 | 1 |
386-
| | | 1004 | 2 |
387-
| | | 1008 | 3 |
388-
| | | ... | |
389-
| (*int)[] ; Ptr (IncompleteArray CInt) | x_ptr | 2000 | 1000 |
383+
| type | name | address | value |
384+
|-----------------------------------------|-------|---------|-------|
385+
| `int[]` | x | 1000 | 1 |
386+
| | | 1004 | 2 |
387+
| | | 1008 | 3 |
388+
| | | ... | |
389+
| `(*int)[] ; Ptr (IncompleteArray CInt)` | x_ptr | 2000 | 1000 |
390390

391391
Constant:
392392

manual/LowLevel/Usage/06-BindingSpecifications.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ module Vector.Types where
123123

124124
newtype Length = UnsafeWrap { unwrap :: Double }
125125

126-
instance Show Length where ..
126+
instance Show Length where ...
127127

128128
pattern Length :: Double -> Length
129129
pattern Length x <- (unwrap -> x)

manual/c/structs.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
#include <stdio.h>
22

3+
/* -------------------------------------------------------------------------- */
4+
/* Structures with and without typedef. */
5+
6+
typedef struct triple {
7+
int a;
8+
int b;
9+
int c;
10+
} triple;
11+
12+
typedef struct triple triple_t;
13+
14+
void mk_triple(int a, int b, int c, triple *triple);
15+
316
/* -------------------------------------------------------------------------- */
417
/* Nested structures. */
518

manual/hs/manual/app/Manual/Types/Structs.hs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ import HsBindgen.Runtime.FlexibleArrayMember qualified as FLAM
1414
import HsBindgen.Runtime.IncompleteArray qualified as IA
1515

1616
import Manual.Tools
17-
18-
import Example
19-
import Example.Unsafe
2017
import Structs
2118
import Structs.Safe
2219

0 commit comments

Comments
 (0)