Skip to content

Commit c58a661

Browse files
committed
Add a guide to writing Huddle
1 parent 528bd55 commit c58a661

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,5 @@ Obviously, this comes with the downside of needing to sensibly mesh the
6969
different abstraction facilities offered by Haskell and CDDL. We have tried to
7070
find a balance where the Huddle code roughly matches the CDDL but gains many
7171
of the advantages of writing in Haskell.
72+
73+
[A guide to writing Huddle](docs/huddle.md) goes into more details.

docs/huddle.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Defining CDDL in Huddle
2+
3+
The Huddle modules in Haskell provide a way to define CDDL (Concise Data
4+
Definition Language) using Haskell's higher-level capabilities. This guide,
5+
based on the provided sources, will cover the key concepts and syntax for
6+
defining CDDL in Huddle.
7+
8+
## Core Types
9+
Huddle utilizes several core types to represent CDDL constructs:
10+
● Huddle: The top-level type representing a collection of rules.
11+
● HuddleItem: Represents individual items within a Huddle, such as rules, groups, or generic rules.
12+
● Rule: A named type definition.
13+
● Named: A type wrapper for associating a name, value, and optional description with an item.
14+
● Value: A type representing primitive CBOR values.
15+
● Group: Represents a collection of entries within a map or array.
16+
17+
## Rules and Assignment
18+
Rules are defined using the =:= operator. The left-hand side of the operator is
19+
the rule name (a T.Text value), and the right-hand side is the type definition.
20+
21+
ruleName =:= typeDefinition
22+
23+
### Example:
24+
`age =:= VUInt`
25+
26+
## Maps
27+
Maps are defined using the mp function and the ==> operator to specify key-value
28+
pairs.
29+
30+
mapName =:= mp [ key1 ==> value1, key2 ==> value2 ]
31+
32+
### Example:
33+
```haskell
34+
location =:= mp [
35+
"latitude" ==> float,
36+
"longitude" ==> float
37+
]
38+
```
39+
40+
## Arrays
41+
Arrays are defined using the arr function and the a function to indicate array elements.
42+
43+
arrayName =:= arr [ a element1, a element2 ]
44+
45+
### Example:
46+
point =:= arr [ a int, a int ]
47+
## Groups
48+
Groups are collections of entries within maps or arrays. They can be named using
49+
the =:~ operator.
50+
51+
groupName =:~ grp [ entry1, entry2 ]
52+
### Example:
53+
```haskell
54+
personalinfo =:~ grp [
55+
"name" ==> tstr,
56+
"age" ==> uint
57+
]
58+
```
59+
## Choices
60+
Huddle represents choices between types using the / operator.
61+
### Example:
62+
value =:= int / tstr
63+
64+
Huddle does not have a direct equivalent for the CDDL // operator (group
65+
choice). Instead, choices within arrays are represented by creating separate
66+
array definitions and combining them using the / operator.
67+
68+
### Example:
69+
```haskell
70+
optionA =:= arr [ a int, a tstr ]
71+
optionB =:= arr [ a tstr, a int ]
72+
choice =:= optionA / optionB
73+
```
74+
75+
## Quantifiers
76+
Huddle provides functions to specify occurrence quantifiers for group entries
77+
and array elements:
78+
● <+: Lower bound
79+
● +>: Upper bound
80+
● opt: Optional (0 or 1 occurrences)
81+
82+
### Example:
83+
```haskell
84+
dat =:= arr [ 1 <+ a int +> 10 ] -- Array of 1 to 10 integers
85+
```
86+
87+
## Comments
88+
The comment function can be used to add descriptions to rules and group entries,
89+
which will be included as comments in the generated CDDL.
90+
91+
### Example:
92+
```haskell
93+
person =:= comment "Represents a person" $ mp [
94+
comment "Person's name" $ "name" ==> VBytes,
95+
"age" ==> VUIntf
96+
]
97+
```
98+
99+
## Generic Rules
100+
Huddle supports defining generic rules using the binding function.
101+
102+
### Example:
103+
```haskell
104+
message = binding $ \t -> "message" =:= {
105+
"type" ==> t,
106+
"payload" ==> any
107+
}
108+
```
109+
110+
## Converting to CDDL
111+
The toCDDL and toCDDLNoRoot functions convert a Huddle definition to CDDL.
112+
toCDDL generates a top-level root element, while toCDDLNoRoot skips the root
113+
element.
114+
115+
## Example File (Conway.hs)
116+
The Conway.hs example file showcases a practical application of Huddle to define
117+
the CDDL for a specific data structure. The file defines numerous rules and
118+
groups using the Huddle syntax and functions described above.

0 commit comments

Comments
 (0)