| 1 | # lambda.pl -- An example program for Data::Variant | ||
| 2 | # | ||
| 3 | # Copyright (c) 2004 Viktor Leijon (leijon@ludd.ltu.se) All rights reserved. | ||
| 4 | # This program is free software; you can redistribute it and/or modify | ||
| 5 | # it under the same terms as Perl itself. | ||
| 6 | # | ||
| 7 | # | ||
| 8 | # This example program losely follows chapter 5 (Untyped lambda calculus) | ||
| 9 | # in Benjamin C. Pierce, Types and Programing Languages, but any failures are | ||
| 10 | # undoubtedly my own. (Also, he uses call by value, I don't). | ||
| 11 | # | ||
| 12 | use warnings; | ||
| 13 | use strict; | ||
| 14 | use Carp; | ||
| 15 | use Switch; | ||
| 16 | use Data::Variant; | ||
| 17 | use Data::Dumper; | ||
| 18 | |||
| 19 | sub Abstraction; sub Application; sub Variable; | ||
| 20 | |||
| 21 | # First define the terms in lambda calculus. | ||
| 22 | register_variant("Term","Variable <STRING>","Abstraction <STRING> Term", | ||
| 23 | "Application Term Term"); | ||
| 24 | |||
| 25 | # Then a little context | ||
| 26 | our %context; | ||
| 27 | |||
| 28 | # A few builtins (Church coded booleans) | ||
| 29 | # tru = \t.\f.t | ||
| 30 | $context{tru} = Abstraction("t", (Abstraction "f", (Variable "t"))); | ||
| 31 | # fls = \t.\f.f | ||
| 32 | $context{fls} = Abstraction("t", (Abstraction "f", (Variable "f"))); | ||
| 33 | |||
| 34 | |||
| 35 | # | ||
| 36 | # The following clearly illustrates the pain associated with building more | ||
| 37 | # complex structures by hand. This took me 15 minutes, building a parser | ||
| 38 | # would almost have been easier. | ||
| 39 | # | ||
| 40 | # c2 tru fls | ||
| 41 | my $subclause = Application( Application((Variable "c2"), Variable("tru")), | ||
| 42 | Variable "fls"); | ||
| 43 | # AND == \c1.\c2. (c1 (c2 tru fls) fls) | ||
| 44 | $context{and} = Abstraction("c1", | ||
| 45 | (Abstraction("c2", | ||
| 46 | (Application( | ||
| 47 | (Application((Variable "c1"),$subclause)), | ||
| 48 | (Variable "fls")))))); | ||
| 49 | |||
| 50 | |||
| 51 | # Test expressions. | ||
| 52 | # tru branch1 branch2 => branch2 | ||
| 53 | my $exp1 = Application ( | ||
| 54 | Application ((Variable "tru"), (Variable "branch1")) , | ||
| 55 | Variable("branch2")); | ||
| 56 | |||
| 57 | # fls branch1 branch2 => branch2 | ||
| 58 | my $exp2 = Application ( | ||
| 59 | Application ((Variable "fls"), (Variable "branch1")) , | ||
| 60 | Variable("branch2")); | ||
| 61 | |||
| 62 | # and tru fls => fls | ||
| 63 | my $exp3 = Application ( Application (( | ||
| 64 | Variable "and"), (Variable "tru")), (Variable "fls")); | ||
| 65 | |||
| 66 | print "Expression: " . simpl_print($exp1) . "\n"; | ||
| 67 | print "Evaluated : " . simpl_print(simpl_eval($exp1)) . "\n"; | ||
| 68 | print "Expression: " . simpl_print($exp2) . "\n"; | ||
| 69 | print "Evaluated : " . simpl_print(simpl_eval($exp2)) . "\n"; | ||
| 70 | print "Expression: " . simpl_print($exp3) . "\n"; | ||
| 71 | print "Evaluated : " . simpl_print(simpl_eval($exp3)) . "\n"; | ||
| 72 | |||
| 73 | |||
| 74 | # | ||
| 75 | # Simple evaluator | ||
| 76 | # NOTE: No alpha conversion, there are so many ways this could go bad | ||
| 77 | # on anything except carefully crafted testcases. | ||
| 78 | sub simpl_eval { | ||
| 79 | my $term = shift; | ||
| 80 | my ($str, $t1, $t2,$t3,$t4); | ||
| 81 | |||
| 82 | switch($term->match()) { | ||
| 83 | case (mkpat "Application", $t1, $t2) { | ||
| 84 | if (match $t1,"Abstraction",$str,$t3) { | ||
| 85 | my $t = subst($t3,$str,$t2); | ||
| 86 | return simpl_eval($t); | ||
| 87 | } | ||
| 88 | my $new_t1 = simpl_eval($t1); | ||
| 89 | return simpl_eval(Application($new_t1,$t2)); | ||
| 90 | } | ||
| 91 | case (mkpat "Variable", $str) { | ||
| 92 | |||
| 93 | if (exists $context{$str}) { | ||
| 94 | return simpl_eval($context{$str}); | ||
| 95 | } else { | ||
| 96 | warn "Unresolvable variable $str\n"; | ||
| 97 | return $term; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | default { return $term }; | ||
| 101 | } | ||
| 102 | return $term; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub subst { | ||
| 106 | my ($term,$var, $val) = @_; | ||
| 107 | my ($t1,$t2, $str); | ||
| 108 | |||
| 109 | switch($term->match()) { | ||
| 110 | case (mkpat "Application", $t1,$t2) { | ||
| 111 | return Application subst($t1,$var,$val),subst($t2,$var,$val); | ||
| 112 | } | ||
| 113 | case (mkpat "Abstraction", $str,$t1) { | ||
| 114 | if ($str eq $var) { | ||
| 115 | return $term; | ||
| 116 | } else { | ||
| 117 | return Abstraction $str, subst($t1,$var,$val); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | case (mkpat "Variable", $str) { | ||
| 121 | if ($str eq $var) { | ||
| 122 | return $val; | ||
| 123 | } else { | ||
| 124 | return $term; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | } | ||
| 128 | confess "Badness in subst"; | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | |||
| 133 | # | ||
| 134 | # Simple printer | ||
| 135 | # No intelligence about when to use parantheses. | ||
| 136 | # | ||
| 137 | sub simpl_print { | ||
| 138 | my $expr = shift; | ||
| 139 | my ($str,$t1,$t2); | ||
| 140 | switch ($expr->match()) { | ||
| 141 | case (mkpat "Variable", $str) { | ||
| 142 | return "$str"; | ||
| 143 | } | ||
| 144 | case (mkpat "Abstraction", $str, $t1) { | ||
| 145 | return "\\$str .(" . simpl_print($t1) . ")"; | ||
| 146 | } | ||
| 147 | case (mkpat "Application",$t1,$t2) { | ||
| 148 | return "(" . simpl_print($t1) . " " . simpl_print($t2) . ")"; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | } |