Social Networking with Inductive Types
CC-BY Chris Liu, Dustin Tucker, & James B. Wilson, Colorado State University
You are designing as Social Networking. Steps:
-
Gather the semantics
-
Match it to existing technology, e.g. JSON
-
Create a post and convert it a JSON blob
Gather the semantics
Your are designing a Social Network. What types of data will your users create? (3-4 ideas should do.)
Do any of these posts depend on other posts? How many?
(If you have none, go back and add one, e.g. "repost".)
\(\mathbb{P}\) is your post data type.
Use your list to create Inductive Operators (Introduction Rules), e.g.
\[\text{text(``Hello Class'')}:\mathbb{P}^0\to \mathbb{P}\]
E.g. semantics
-
Share a message \(\text{text(msg)}:\mathbb{P}^0\to \mathbb{P}\)
-
Share an image \(\text{image(pic.jpg)}:\mathbb{P}^0\to \mathbb{P}\)
-
A repost and add a text \(\text{repost}(msg):\mathbb{P}^1\to \mathbb{P}\)
-
Compare two posts \(\text{compare}:\mathbb{P}^2\to \mathbb{P}\)
Potential code
enum Post
case Text(msg:String)() // Extra () = No dependence on other Posts.
case Image(img:JPEG)()
case Repost(msg:String)(previous:Post)
case Compare(msg:String)(left:Post, right:Post)Match with a technology:
JSON, Part I
{ "text" : "Hello Class" }
JavaScript Object Notation JSON has its own \(\text{text(msg)}:\mathbb{JSON}^0\to \mathbb{JSON}\)
{ "text" : <msg> }
E.g.
\(\mathbb{P}^0\)
\(\mathbb{JSON}^0\)
\(\mathbb{P}\)
\(\mathbb{JSON}\)
empty array to empty array
text("Hi!")
text("Hi!")
Recursion
Base Case
(\(\mathbb{P}\)-elimination)
[]
empty array to empty array
text("Hi!")
text("Hi!")
[]
Hi!
{ "text": "Hi!" }
Yes "Hi!" is variable but it is of type String, not \(\mathbb{P}\) nor \(\mathbb{JSON}\) so consider it constant for these semantics.
A \(\mathbb{P}\)-introduction rule. Syntax \(\Gamma \vdash \text{text(``Hi!'')}:\mathbb{P}\)
\(\mathbb{P}^0\)
\(\mathbb{JSON}^0\)
\(\mathbb{P}\)
\(\mathbb{JSON}\)
empty array to empty array
text("Hi!")
text("Hi!")
encode
(\(\mathbb{P}\)-elimination)
[]
empty array to empty array
text("Hi!")
text("Hi!")
[]
Hi!
{ "text": "Hi!" }
def encode( p : Post) :JSON =
p match {
case Text(msg)() =>
return "{\"text\": " + msg + " }"
}Potential code
\(\mathbb{P}^0\) case so no posts provided (compare later cases)
Commutative Diagram \(\leadsto\) Computation rule
\[\frac{\Gamma\vdash \text{text(msg)}}{\text{encode}(text(msg)) := \text{\{``text'': msg\}}}\]
Convert all your \(\mathbb{P}^0\to \mathbb{P}\)
to \(\mathbb{JSON}^0\to \mathbb{JSON}\).
Match with a technology:
JSON, Part II
{ repost: {
"text" : <msg>,
"post" : <other post>,
}
}
JSON can capture
\(\text{repost(msg)}:\mathbb{P}^1\to \mathbb{P}\)
\(\mathbb{P}^1\)
\(\mathbb{JSON}^1\)
\(\mathbb{P}\)
\(\mathbb{JSON}\)
encode(p)
repost("Wow!")
repost("Wow!")
Recursion
Inductive Hypothesis
(\(\mathbb{P}\)-elimination)
encode(p)
repost("Wow!")
repost("Wow!")
{image: 'smile.jpg'}
Wow!
{"repost": {
"text": "Wow!",
"post": {...}}}
A \(\mathbb{P}\)-introduction rule. Syntax \(\displaystyle \frac{\Gamma\vdash p:\mathbb{P}}{\Gamma\vdash\text{repost(``Wow!'')}(p):\mathbb{P}}\)
Text
def encode( p : Post) :JSON =
p match {
case Text(msg)() =>
return "{\"text\": " + msg + " }"
case Repost(msg)(prev:Post) =>
return "{\"repost: {\n\t \"text\":"
+ msg +",\n\t\"post\":"
+ encode(p) + "}\n}"
}Potential code
\(\mathbb{P}^1\) as case DOES depend on posts
Commutative Diagram \(\leadsto\) Computation rule
\[\frac{\Gamma\vdash msg:\text{String}, p:\mathbb{P}}{\text{encode}(repost(msg)(p)) := \text{\{``repost'': {...\}}}}\]
\(\mathbb{P}^1\)
\(\mathbb{JSON}^1\)
\(\mathbb{P}\)
\(\mathbb{JSON}\)
encode(p)
repost(msg)
repost(msg)
encode
(\(\mathbb{P}\)-elimination)
encode(p)
repost(msg)
repost(msg)
{"post": <A>}
msg
{"repost": {
"text": msg,
"post": <A>}}}
p
p
Convert all your \(\mathbb{P}^1\to \mathbb{P}\)'s to JSON.
{ op : {
"param" : <arg>,
"blob1" : {first JSON blob},
"blob2" : { second JSON blob},
...
}}
\(op(arg):\mathbb{JSON}^n\to \mathbb{JSON}\)
Convert all your \(\mathbb{P}^n\to \mathbb{P}\)'s to JSON.
Write a post which features all your operators, then convert it to a JSON blob.
Social Networking with Inductive Types
By James Wilson
Social Networking with Inductive Types
Social network posts are a great demonstration of creating inductive types. When it comes to recording the results for transmission one of the most versatile formats is JSON, another inductive type.
- 11