Replies: 1 comment 3 replies
-
Hi Dan! To do what you want nicely, we'd need algebraic data types or the like, which are not implemented here (but are easy to add). Unlike JavaScript there is no "falsiness" and you can't just use a list as the condition in an if-then-else! We need something like an option type to express the fact that we may have an empty list. One way to emulate an let some = fun x -> fun ifSome -> fun ifNone -> ifSome x
let none = fun ifSome -> fun ifNone -> ifNone{}
let rec map = fun fn -> fun lst ->
lst (fun cons -> some {head=fn cons.head; tail=map fn cons.tail}) (fun nil -> none)
let lst = some {head=1; tail=none}
let addLst = map succ lst This works, though the types are quite ugly to read due to the option encoding: val some: 'a -> ('a -> 'b) -> ⊤ -> 'b
val none: ⊤ -> ({} -> 'a) -> 'a
val map: ('a -> 'b) -> (({head: 'a, tail: 'c} -> ({head: 'b, tail: 'd} -> 'e) -> ⊤ -> 'e) -> (⊤ -> ⊤ -> ({} -> 'f) -> 'f) -> 'd) as 'c -> 'd
val lst: ({head: int, tail: ⊤ -> ({} -> 'a) -> 'a} -> 'b) -> ⊤ -> 'b
val addLst: (({head: int, tail: 'a} -> 'b) -> ({} -> 'b) -> 'b) as 'a In a practical language, we'd provide algebraic data types (so no need to encode them) and type synonyms to reduce the verbosity of complicated types still. For the record, here is the example in another prototype I made (whose source is currently private) which handles data types in a more first-class way: let rec map = fun f -> fun ls -> match ls with
Cons{head = h; tail = t} -> Cons{head = f h; tail = map f t}
| Nil{} -> Nil{}
let lst = Cons{head=1; tail=Nil{}}
let addLst = map succ lst val map: ('a -> 'b) -> (Cons{head: 'a, tail: 'c} ∨ Nil{}) as 'c -> (Cons{head: 'b, tail: 'd} ∨ Nil{}) as 'd
val lst: Cons{head: Int, tail: Nil{}}
val addLst: (Cons{head: Int, tail: 'a} ∨ Nil{}) as 'a |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello! I enjoyed the paper and playing around with the demo. I tried to define a
map
function:and get this:
So that's not quite right. How would you define this?
Beta Was this translation helpful? Give feedback.
All reactions