Quick actions

cmd+k|ctrl+k

Navigation

Languages

Bug: Template and Tail-call Optimization

Snippet info

Language

Ats

Visibility

public

Author

steinwaywhw

Created

2016-08-15T17:07:02Z

Updated

2016-08-15T21:44:35Z

#include "share/atspre_staload.hats"

extern fun {a:t@ype} show (a): void 
implement {a} show (x) = gprint_val<a> x

datatype maybe (a:t@ype) = 
| Nothing (a) of ()
| Just (a) of a

implement (a) show<maybe a> (m) = 
    case+ m of 
    // | Nothing () => gprint_val<string> "Nothing"
    | Nothing () => ignoret (show<string> "Nothing")
    // | Just x     => gprint_val<a> x
    | Just x     => ignoret (show<a> x)
    
    // this is a bug due to tail-call optimization
    // use ignoret, or use patscc --tlcalopt-disable
    
extern fun {a:t@ype} cmp (a, a): int 
implement {a} cmp (x, y) = gcompare_val_val<a> (x, y)
implement (a) cmp<maybe a> (x, y) = 
    case+ (x, y) of 
    | (Nothing _, Nothing _) => 0
    | (Nothing _, Just _)    => ~1
    | (Just _, Nothing _)    => 1
    | (Just x, Just y)       => cmp<a> (x, y)
    
    
implement main0 () = {
    val _ = show<maybe int> (Nothing{int} ())
    val _ = show<maybe char> (Just ('a'))
    val _ = cmp<maybe int> (Just 3, Just 5)
}
INFO