From 33e5dcd49bc91a3f84cf61895d4fabee9068b460 Mon Sep 17 00:00:00 2001 From: sebastianselander Date: Tue, 23 May 2023 12:29:43 +0200 Subject: [PATCH] examples ready for demonstration --- sample-programs/working/quicksort.crf | 38 ++++-------------------- src/Main.hs | 42 ++++++++++++++++++++++----- test_program.crf | 35 +--------------------- 3 files changed, 41 insertions(+), 74 deletions(-) diff --git a/sample-programs/working/quicksort.crf b/sample-programs/working/quicksort.crf index d37651e..d15e0e7 100644 --- a/sample-programs/working/quicksort.crf +++ b/sample-programs/working/quicksort.crf @@ -1,21 +1,3 @@ -toStr : List Int -> List Char -toStr xs = case xs of - Cons a as => Cons (toChar a) (toStr as) - Nil => Nil - -toChar : Int -> Char -toChar x = case x of - 0 => '0' - 1 => '1' - 2 => '2' - 3 => '3' - 4 => '4' - 5 => '5' - 6 => '6' - 7 => '7' - 8 => '8' - 9 => '9' - filter : (a -> Bool) -> List a -> List a filter p xs = case xs of Nil => Nil @@ -23,22 +5,14 @@ filter p xs = case xs of True => Cons x (filter p xs) False => filter p xs -.++ : List a -> List a -> List a -.++ as bs = case as of - Nil => bs - Cons x xs => Cons x (xs ++ bs) - quicksort : List Int -> List Int quicksort xs = case xs of Nil => Nil - Cons a as => let smaller = quicksort (filter (\y. y < a) xs) - in let bigger = quicksort (filter (\y. a < y) xs) - in smaller ++ (Cons a bigger) - -descList : Int -> Int -> List Int -descList from to = case to < from of - False => Cons to (descList from (to - 1)) - True => Nil + Cons a as => let smaller = quicksort (filter (\y. y < a) xs) in + let bigger = quicksort (filter (\y. a < y) xs) in + smaller ++ (Cons a bigger) +-- [5, 2, 8, 9, 6, 0, 1] main = let list = Cons 5 (Cons 2 (Cons 8 (Cons 9 (Cons 6 (Cons 0 (Cons 1 Nil)))))) in - printStr (toStr (quicksort list)) + printList (quicksort list) + diff --git a/src/Main.hs b/src/Main.hs index 6aa4dad..2e61ec9 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -9,7 +9,7 @@ import Control.Monad (when, (<=<)) import Data.List.Extra (isSuffixOf) import Data.Maybe (fromJust, isNothing) import Desugar.Desugar (desugar) -import Expander (expand) +-- import Expander (expand) import GHC.IO.Handle.Text (hPutStrLn) import Grammar.ErrM (Err) import Grammar.Layout (resolveLayout) @@ -227,18 +227,44 @@ prelude = , "const : a -> b -> a" , "const x y = x" , "\n" + -- Printing as a list for the demonstration , "printStr : List Char -> Unit" , "printStr xs = case xs of" , " Nil => Unit" , " Cons x xs => flipConst (printChar x) (printStr xs)" , "\n" - , "data List a where" - , " Nil : List a" - , " Cons : a -> List a -> List a" - , "\n" - , "data Pair a b where" - , " Pair : a -> b -> Pair a b" - , "\n" , "asciiCode : Char -> Int" , "asciiCode x = case x of { 'a' => 97; 'b' => 98; 'c' => 99; 'd' => 100; 'e' => 101; 'f' => 102; 'g' => 103; 'h' => 104; 'i' => 105; 'j' => 106; 'k' => 107; 'l' => 108; 'm' => 109; 'n' => 110; 'o' => 111; 'p' => 112; 'q' => 113; 's' => 114; 't' => 115; 'u' => 116; 'v' => 117; 'w' => 118; 'x' => 119; 'y' => 120; 'z' => 121; }" + , "toChar : Int -> Char" + , "toChar x = case x of {0 => '0'; 1 => '1'; 2 => '2'; 3 => '3'; 4 => '4'; 5 => '5'; 6 => '6'; 7 => '7'; 8 => '8'; 9 => '9'; }" + , "\n" + , "toStr : List Int -> List Char" + , "toStr xs = case xs of" + , " Cons a as => Cons (toChar a) (toStr as)" + , " Nil => Nil" + , "\n" + , "interleave : Char -> List Char -> List Char" + , "interleave c ls = case ls of" + , " Nil => Nil" + , " Cons a as => case as of" + , " Nil => Cons a Nil" + , " Cons y ys => Cons a (Cons c (interleave c as))" + , "\n" + , "bracketify : List Char -> List Char" + , "bracketify xs = Cons '[' (xs ++ (Cons ']' Nil))" + , "\n" + , "printList : List Int -> Unit" + , "printList xs = printStr (bracketify (interleave ',' (toStr xs)))" + , "\n" + , ".++ : List a -> List a -> List a" + , ".++ as bs = case as of" + , " Nil => bs" + , " Cons x xs => Cons x (xs ++ bs)" + , "\n" + , "data List a where" + , " Nil : List a" + , " Cons : a -> List a -> List a" + , "\n" + , "data Pair a b where" + , " Pair : a -> b -> Pair a b" ] diff --git a/test_program.crf b/test_program.crf index 13ea2cf..ac089a3 100644 --- a/test_program.crf +++ b/test_program.crf @@ -1,34 +1 @@ -data Maybe a where - Nothing : Maybe a - Just : a -> Maybe a - -data Either a b where - Left : a -> Either a b - Right : b -> Either a b - -data List a where - Nil : List a - Cons : a -> List a -> List a - -data Foo a where - Foo : List a -> Foo a - -data Bar a where - Bar : Foo a -> Bar a - -test = \x . case x of - Left (Just (Bar (Foo (Cons 1 Nil)))) => 1 - _ => 0 - -main = 0 - --- pattern1 = PInj (UIdent "Foo") [PInj (UIdent "Cons") [PLit (LInt 1), PEnum (UIdent "Nil")]] --- env = Env { sigs = mempty, count = 0, nextChar = 'a', declaredBinds = mempty, takenTypeVars = mempty, injections = M.insert (T.Ident "Foo") (TFun list foo) $ M.insert (T.Ident "Nil") list $ M.singleton (T.Ident "Cons") (TFun a (TFun list list))} --- list = TData (UIdent "List") [a] --- foo = TData (UIdent "Foo") [a] --- a = TVar $ MkTVar "a" --- pattern2 = PInj (UIdent "Foo") [PInj (UIdent "Cons") [PLit (LInt 1),PEnum (UIdent "Nil")]] - --- (snd . fst) <$> run' env initCtx (inferPattern pattern1) --- (snd . fst) <$> run' env initCtx (inferPattern pattern2) - +main = let f x = x in f 123