From 3af9e7e1748ba931efd0f093dd1bcd42e8e3546b Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Sat, 10 Aug 2019 20:42:48 -0700 Subject: [PATCH] map combo --- thun/gnu-prolog/Makefile | 3 ++- thun/gnu-prolog/thun.pl | 30 +++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/thun/gnu-prolog/Makefile b/thun/gnu-prolog/Makefile index 9046d46..3031b1d 100644 --- a/thun/gnu-prolog/Makefile +++ b/thun/gnu-prolog/Makefile @@ -3,4 +3,5 @@ GPLC_OPTIONS="--min-size" thun: thun.pl gplc $(GPLC_OPTIONS) -o thun thun.pl - +defs.pl: meta-defs.pl defs.txt + gprolog --consult-file meta-defs.pl defs.txt diff --git a/thun/gnu-prolog/thun.pl b/thun/gnu-prolog/thun.pl index 0434a8c..61060ca 100644 --- a/thun/gnu-prolog/thun.pl +++ b/thun/gnu-prolog/thun.pl @@ -161,10 +161,38 @@ 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 the map combinator. - +Obviously it would be nice to take advantage of the implied parallelism. +Instead the quoted program, stack, and terms in the input list are +transformed to simple Joy expressions that run the quoted program on +prepared copies of the stack that each have one of the input terms on +top. These expressions are collected in a list and the whole thing is +evaluated (with infra) on an empty list, which becomes the output list. +The chief advantage of doing it this way (as opposed to using Prolog's +map) is that the whole state remains in the pending expression, so +there's nothing stashed in Prolog's call stack. This preserves the nice +property that you can interrupt the Joy evaluation and save or transmit +the stack+expression knowing that you have all the state. +*/ +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).