Fakultas Ilmu Komputer UI
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
ariangganugraha-funpro2020-lambda
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ari Angga Nugraha
ariangganugraha-funpro2020-lambda
Commits
56c90b9e
Commit
56c90b9e
authored
8 years ago
by
Sean Gillespie
Browse files
Options
Downloads
Patches
Plain Diff
Add a Type type
parent
006a76b6
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Language/SystemF/Expression.hs
+30
-0
30 additions, 0 deletions
src/Language/SystemF/Expression.hs
test/Language/SystemF/ExpressionSpec.hs
+15
-1
15 additions, 1 deletion
test/Language/SystemF/ExpressionSpec.hs
with
45 additions
and
1 deletion
src/Language/SystemF/Expression.hs
+
30
−
0
View file @
56c90b9e
{-# LANGUAGE FlexibleInstances #-}
module
Language.SystemF.Expression
where
import
Data.Monoid
import
Language.Lambda.Util.PrettyPrint
data
SystemFExpr
name
ty
...
...
@@ -14,10 +16,18 @@ data SystemFExpr name ty
-- x [X]
deriving
(
Eq
,
Show
)
data
Ty
name
=
TyVar
name
-- Type variable (T)
|
TyArrow
(
Ty
name
)
(
Ty
name
)
-- Type arrow (T -> U)
deriving
(
Eq
,
Show
)
-- Pretty printing
instance
(
PrettyPrint
n
,
PrettyPrint
t
)
=>
PrettyPrint
(
SystemFExpr
n
t
)
where
prettyPrint
=
prettyPrint
.
pprExpr
empty
instance
PrettyPrint
n
=>
PrettyPrint
(
Ty
n
)
where
prettyPrint
=
prettyPrint
.
pprTy
empty
-- Same as prettyPrint, but we assume the same type for names and types. Useful
-- for testing.
prettyPrint'
::
PrettyPrint
n
=>
SystemFExpr
n
n
->
String
...
...
@@ -73,6 +83,26 @@ pprAbs pdoc name ty body = between vars' lambda' ". " (pprExpr pdoc body')
pprArg
n
t
=
prettyPrint
n
++
":"
++
prettyPrint
t
lambda'
=
[
lambda
,
space
]
-- Pretty print types
pprTy
::
PrettyPrint
n
=>
PDoc
String
->
Ty
n
->
PDoc
String
pprTy
pdoc
(
TyVar
n
)
=
prettyPrint
n
`
add
`
pdoc
pprTy
pdoc
(
TyArrow
a
b
)
=
pprTyArrow
pdoc
a
b
pprTyArrow
::
PrettyPrint
n
=>
PDoc
String
->
Ty
n
->
Ty
n
->
PDoc
String
pprTyArrow
pdoc
a
@
(
TyVar
_
)
b
=
pprTyArrow'
pdoc
a
b
pprTyArrow
pdoc
a
@
(
TyArrow
_
_
)
b
=
pprTyArrow'
pdoc
a'
b
where
a'
=
betweenParens
(
pprTy
empty
a
)
empty
pprTyArrow'
pdoc
a
b
=
between
arrow
(
prettyPrint
a
)
(
prettyPrint
b
)
pdoc
where
arrow
=
" -> "
`
add
`
empty
-- Pretty print a type abstraction
pprTAbs
::
(
PrettyPrint
n
,
PrettyPrint
t
)
=>
PDoc
String
...
...
This diff is collapsed.
Click to expand it.
test/Language/SystemF/ExpressionSpec.hs
+
15
−
1
View file @
56c90b9e
...
...
@@ -14,7 +14,7 @@ spec = describe "prettyPrint" $ do
it
"prints simple applications"
$
prettyPrint'
(
App
(
Var
"a"
)
(
Var
"b"
))
`
shouldBe
`
"a b"
it
"prints simple abstrac
c
tions"
$
it
"prints simple abstractions"
$
prettyPrint
(
Abs
"x"
"T"
(
Var
"x"
))
`
shouldBe
`
"λ x:T. x"
it
"prints simple type abstractions"
$
...
...
@@ -44,3 +44,17 @@ spec = describe "prettyPrint" $ do
prettyPrint
(
App
(
Abs
"f"
"F"
(
Var
"f"
))
(
Abs
"g"
"G"
(
Var
"g"
)))
`
shouldBe
`
"(λ f:F. f) (λ g:G. g)"
it
"prints simple types"
$
prettyPrint
(
TyVar
"X"
)
`
shouldBe
`
"X"
it
"print simple arrow types"
$
prettyPrint
(
TyArrow
(
TyVar
"A"
)
(
TyVar
"B"
))
`
shouldBe
`
"A -> B"
it
"prints chained arrow types"
$
prettyPrint
(
TyArrow
(
TyVar
"X"
)
(
TyArrow
(
TyVar
"Y"
)
(
TyVar
"Z"
)))
`
shouldBe
`
"X -> Y -> Z"
it
"prints nested arrow types"
$
prettyPrint
(
TyArrow
(
TyArrow
(
TyVar
"T"
)
(
TyVar
"U"
))
(
TyVar
"V"
))
`
shouldBe
`
"(T -> U) -> V"
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment