changeset 38:a0ca5e29a9dc

Call subtype using user-defined cast operator
author atton <atton@cr.ie.u-ryukyu.ac.jp>
date Tue, 03 Jan 2017 05:19:04 +0000
parents 60e604972f30
children d8312361afdc
files cbc/named-product.agda
diffstat 1 files changed, 31 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/cbc/named-product.agda	Mon Jan 02 07:00:55 2017 +0000
+++ b/cbc/named-product.agda	Tue Jan 03 05:19:04 2017 +0000
@@ -1,8 +1,12 @@
 module named-product where
 
+open import Function
 open import Data.Nat
 open import Data.String
 open import Data.Vec
+open import Relation.Binary.PropositionalEquality
+
+
 
 record DataSegment (n : ℕ) : Set₁ where
   field
@@ -13,11 +17,19 @@
 ids {a} {zero}  record { name = name ; ds = (x ∷ []) } = x a
 ids {a} {suc n} record { name = name ; ds = (x ∷ ds) } = x a -> ids {a} {n} record { name = name ; ds = ds }
 
-record LoopCounter (A : Set) : Set where
+
+record _<<<_ (A : Set) (B : Set) : Set where
+  field 
+    get : B -> A
+    set : B -> A -> B
+
+open _<<<_
+
+
+record LoopCounter : Set where
   field
-    counter : ℕ
-    name    : String
-
+    count : ℕ
+    name  : String
 
 record Context : Set where
   field
@@ -25,23 +37,29 @@
     led   : String
     signature : String
 
+
 instance
-  contextHasLoopCounter : {A : Set} -> Context -> LoopCounter Context
-  contextHasLoopCounter c = record { counter = Context.cycle c ; name = Context.led c}
+  contextHasLoopCounter : LoopCounter <<< Context
+  contextHasLoopCounter = record {get = (\c   -> record {count = Context.cycle c ;
+                                                         name  = Context.led   c });
+                                  set = (\c l -> record {cycle     = LoopCounter.count l;
+                                                         led       = LoopCounter.name  l;
+                                                         signature = Context.signature c})
+                                  }
 
-inc : {A :  Set} -> LoopCounter A -> LoopCounter A
-inc c = record c { counter =  (LoopCounter.counter c) + 1}
+
+inc :  LoopCounter -> LoopCounter
+inc  l = record l {count = suc (LoopCounter.count l)}
 
 firstContext : Context
 firstContext = record { cycle = 3 ; led = "q" ; signature = "aaa" }
 
-incContextCycle : {{_ : Context -> LoopCounter Context}} -> Context -> Context
-incContextCycle {{lp}} c = record c { cycle = incrementedCycle }
-  where
-    incrementedCycle = LoopCounter.counter (inc (lp c))
+incContextCycle : {{_ : LoopCounter <<< Context }} -> Context -> Context
+incContextCycle {{l}} c = set l c (inc (get l c))
 
 
-
+equiv : incContextCycle firstContext ≡ record { cycle = 4 ; led = "q" ; signature = "aaa" }
+equiv = refl