I’d like to be able to load commands into ghci and have the commands verbosely printed.

Likely you have a lot of .ghci files

1
locate .ghci

Case in point

This file is actually a .ghci file as it contains commands such as :t (>)

https://github.com/bishboria/learnyouahaskell/blob/master/02/2%5Ftypes.hs

I created an expect script for this purpose using my expect-generation script

 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
hs|ghci) {
    cmd="$1"
    shift

    if stdin_exists; then
        tfin="$(cat | tf txt)"

        inpara="-sf $(aqf "$tfin")"
    else
        : ${cmd:="foldl (/) 64 [4,2,4]"}
    fi

    # x -cd "$(pwd)" -sh "ghci" -r "^.*[^ ]>" -s "$cmd" -c m "$@" -o

    exec <&1
    # echo "$cmd" 1>&2
    # exit 0

    if test -n "$cmd"; then
        incmd="-s $(aqf "$cmd")"
    fi

    fullcmd="x -cd \"$(pwd)\" -sh \"ghci\" -r \"^.*[^ ]>\" $inpara $incmd -c m \"\$@\" -o"
    # echo "$fullcmd"
    # exit 0
    eval "$fullcmd"
}
1
cat $MYGIT/bishboria/learnyouahaskell/01/1_baby.hs | xs -n ghci | tf sh | v
 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
#!/usr/bin/expect -f
#trap sigwinch and pass it to the child we spawned
trap {
    set rows [stty rows]
    set cols [stty columns]
    stty rows $rows columns $cols < $spawn_out(slave,name)
} WINCH

proc getctrl {char} {
    set ctrl [expr ("$char" & 01xF)]
    return $ctrl
}

set force_conservative 0
if {$force_conservative} {
    set send_slow {1 .1}
    proc send {ignore arg} {
        sleep .1
        exp_send -s -- $arg
    }
}

# For send -h
set send_human {.4 .4 .2 .5 100}
set timeout -1
match_max 100000
spawn ghci
expect -re "^.*\[^ ]>"
set fp [ exec cat "/tmp/file_tempfile_Ik2Ox6_rand-30767_pid-9872.txt" ]
send -- $fp
send -- \015
interact
expect eof
close

Demonstration of xs ghci

asciinema recording

Add to spacemacs, ranger and compile-run

spacemacs

1
(add-to-list 'auto-mode-alist '("\\.ghci.*\\'" . ghci-script-mode))

rifle.conf

1
2
# Open with spacemacs
name .ghci*      = sp "$@"

compile-run

1
2
3
4
5
6
.ghci*) {
    cd "$dn"
    xs ghci "$fn"
    exit 0
}
;;

Demonstration of ranger, spacemacs and compile-run working together

asciinema recording

Other examples of automating ghci

haskell one-liners

1
ghciol '[ x | x <- [50..100], x `mod` 7 == 3]'
[52,59,66,73,80,87,94]