From 32d5953f2985f8e39454d9ad6891a48c47839656 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Sun, 21 Jul 2019 08:28:56 -0700 Subject: [PATCH] Implement map combinator. --- thun/defs.txt | 5 ++++- thun/thun.pl | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/thun/defs.txt b/thun/defs.txt index e883053..487dfe5 100644 --- a/thun/defs.txt +++ b/thun/defs.txt @@ -1,9 +1,11 @@ -- == 1 - -++ == 1 + ? == dup bool +++ == 1 + anamorphism == [pop []] swap [dip swons] genrec app1 == grba infrst app2 == [grba swap grba swap] dip [infrst] cons ii +app3 == 3 appN +appN == [grabN] cons dip map disenstacken at == drop first average == [sum 1.0 *] [size] cleave / b == [i] dip i @@ -23,6 +25,7 @@ flatten == [] swap [concat] step fork == [i] app2 fourth == rest third gcd == true [tuck mod dup 0 >] loop pop +grabN == [] swap [cons] times grba == [stack popd] dip hypot == [sqr] ii + sqrt ifte == [nullary] dipd swap branch diff --git a/thun/thun.pl b/thun/thun.pl index 3b803ce..5717c31 100644 --- a/thun/thun.pl +++ b/thun/thun.pl @@ -242,6 +242,34 @@ combo(genrec, [R1, R0, Then, If|S], Quoted = [If, Then, R0, R1, genrec], append(R0, [Quoted|R1], Else). +/* +This is a crude but servicable implementation of map combinator. +Obviously it would be nice to take advantage of the implied parallelism. +Instead the quoted program, stack, and each term in the arg list are +transformed to a simple Joy expression that runs the program on a prepared +stack. These expressions are collected in a list and the whole thing is +evaluated with infra on an empty list, so the result is the mapped list. + +The chief advantage of doing it this way (as opposed to using Prolog's map) +is that the whole state remains in the continuation expression. +*/ + +combo(map, [_, []|S], [[]|S], E, E ) :- !. +combo(map, [P, List|S], [Mapped, []|S], E, [infra|E]) :- + prepare_mapping(P, S, List, Mapped). + +% Set up a program for each term in ListIn +% +% [term S] [P] infrst +% +% prepare_mapping(P, S, ListIn, ListOut). + +prepare_mapping(P, S, In, Out) :- prepare_mapping(P, S, In, [], Out). + +prepare_mapping( _, _, [], Out, Out) :- !. +prepare_mapping( P, S, [T|In], Acc, Out) :- + prepare_mapping(P, S, In, [[T|S], P, infrst|Acc], Out). + /* Compiler