Image Unit Processing Interface.
INFO: This is a mirror from GitHub.
github.com/sona-tau/iupi
1#lang plait
2
3(require "utilities.rkt")
4(require "types.rkt")
5(require "combinators.rkt")
6
7; Flips the arguments for function (a)
8(define (flip a)
9 (λ (x y) (a y x)))
10
11;----- char/p implementation -----;
12; Creates a parser that parses a single character (c)
13(define (char/p [c : Char]) : (Parser Char)
14 (λ (s)
15 (type-case (Optionof Char) (first-char s)
16 [(some x) (if (char=? x c) (ok (pair (string-tail s) c)) (err))]
17 [(none) (err)])))
18
19;; char/p monoid implementation ;;
20; Though technically this operation should be commutative, it is not and that is
21; why there (m-prepend) is named (m-prepend)
22
23; Creates a parser that concatenates a (Parser String) with a (Parser Char) so
24; that it parses the string first and then the char
25(define (m-prepend [p1 : (Parser String)] [p2 : (Parser Char)]) : (Parser String)
26 (λ (input)
27 (type-case (ParseResult String) (p1 input)
28 [(ok r1) (type-case (ParseResult Char) (p2 (fst r1))
29 [(ok r2) (ok (pair (fst r2) (string-append (snd r1) (list->string (list (snd r2))))))]
30 [(err) (err)])]
31 [(err) (err)])))
32
33;----- string/p implementation -----;
34; Creates a parser that parses a string (s)
35(define (string/p [s : String]) : (Parser String)
36 (foldr (flip m-prepend) (pure "") (reverse (map char/p (string->list s)))))