-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoly-data.spl
More file actions
86 lines (71 loc) · 1.95 KB
/
poly-data.spl
File metadata and controls
86 lines (71 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
data Maybe a = { Just a | Nothing }
fun testJust : Int := {
let foo = Just 5 in # 0
let asdf = case foo of
Just x => let _ = println("Got Just!") in x
Nothing => let _ = println("ERROR: Got Nothing") in 0
in asdf
}
data List a = { Nil | Cons a (List a) }
fun head (xs : List a) : Maybe a := {
case xs of
Nil => Nothing
Cons h t => Just h
}
# FIXME: passing a function ref w/ unboxed argument to a function
# requiring a function ref w/ boxed argument does not work.
# Passed function ref needs to be automatically "promoted" to a
# boxed version.
#
# See manual boxing example below.
# fun filter (xs : List a, f : a -> Bool) : List a := {
# case xs of
# Nil => Nil
# Cons h t => if @f(h) then Cons h @filter(t, f) else @filter(t, f)
# }
fun filter (xs : List Int, f : Int -> Bool) : List Int := {
case xs of
Nil => Nil
Cons h t =>
let newT = @filter(t, f) in
if @f(h) then Cons h newT else newT
}
fun lte5 (x: Int) : Bool := {
x <= 5
}
fun numberList (n : Int) : List Int := {
if n <= 0 then Nil else Cons n @numberList(n - 1)
}
fun printList (xs : List a, msg : String) : Int := {
case xs of
Nil => 0
Cons h t =>
let _ = println(msg) in
@printList(t, msg)
}
fun main : Int := {
let nums = @numberList(@add(5, 5, @addTable_Int())) in
let x1 = @printList(nums, "numberList") in
let x2 = @printList(@filter(nums, <e5), "filter(numberList, <= 5)") in
x2
}
# Manual boxing example. Note how the function's type signature and
# implementation must be changed to accomodate the boxing.
data Box a = { MkBox a }
data AddTable a = { Add (Box a -> Box a -> Box a) }
fun add (x : a, y : a, table : AddTable a) : a := {
case table of
Add f =>
case @f(MkBox x, MkBox y) of
MkBox res => res
}
fun add_Int (bx : Box Int, by : Box Int) : Box Int := {
case bx of
MkBox x =>
case by of
MkBox y =>
MkBox (x + y)
}
fun addTable_Int : AddTable Int := {
Add &add_Int
}